summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2000-07-05 11:24:13 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2000-07-05 11:24:13 (GMT)
commit5f0e29eb45c0ee5913e33b7f94b7bdc5f5b17b79 (patch)
tree4ccd7c1faff28d905d8b213d5882611760589d30
parentadf63d6c246ca0356b633e995402b8e4a32f8c0a (diff)
downloadcpython-5f0e29eb45c0ee5913e33b7f94b7bdc5f5b17b79.zip
cpython-5f0e29eb45c0ee5913e33b7f94b7bdc5f5b17b79.tar.gz
cpython-5f0e29eb45c0ee5913e33b7f94b7bdc5f5b17b79.tar.bz2
Modified the encode interfaces of all builtin codecs to accept
any object which can be converted to Unicode by means of PyUnicode_FromObject().
-rw-r--r--Modules/_codecsmodule.c155
1 files changed, 105 insertions, 50 deletions
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 4f368f8..a7be915 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -20,8 +20,14 @@
<encoding>_decode(char_buffer_obj[,errors='strict']) ->
(Unicode object, bytes consumed)
+ <encoding>_encode() interfaces also accept non-Unicode object as
+ input. The objects are then converted to Unicode using
+ PyUnicode_FromObject() prior to applying the conversion.
+
These <encoding>s are available: utf_8, unicode_escape,
- raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit)
+ raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
+ mbcs (on win32).
+
Written by Marc-Andre Lemburg (mal@lemburg.com).
@@ -344,17 +350,22 @@ static PyObject *
utf_8_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|z:utf_8_encode",
+ if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors),
- PyUnicode_GET_SIZE(str));
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors),
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
/* This version provides access to the byteorder parameter of the
@@ -368,147 +379,186 @@ static PyObject *
utf_16_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
int byteorder = 0;
- if (!PyArg_ParseTuple(args, "U|zi:utf_16_encode",
+ if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
&str, &errors, &byteorder))
return NULL;
- return codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- byteorder),
- PyUnicode_GET_SIZE(str));
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors,
+ byteorder),
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
utf_16_le_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|zi:utf_16_le_encode",
+ if (!PyArg_ParseTuple(args, "O|zi:utf_16_le_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors,
-1),
PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
utf_16_be_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|zi:utf_16_be_encode",
+ if (!PyArg_ParseTuple(args, "O|zi:utf_16_be_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str),
- errors,
- +1),
- PyUnicode_GET_SIZE(str));
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str),
+ errors,
+ +1),
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
unicode_escape_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|z:unicode_escape_encode",
+ if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeUnicodeEscape(
- PyUnicode_AS_UNICODE(str),
- PyUnicode_GET_SIZE(str)),
- PyUnicode_GET_SIZE(str));
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeUnicodeEscape(PyUnicode_AS_UNICODE(str),
+ PyUnicode_GET_SIZE(str)),
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
raw_unicode_escape_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|z:raw_unicode_escape_encode",
+ if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeRawUnicodeEscape(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str)),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
latin_1_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|z:latin_1_encode",
+ if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeLatin1(
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeLatin1(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
ascii_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|z:ascii_encode",
+ if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeASCII(
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeASCII(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
static PyObject *
charmap_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
PyObject *mapping = NULL;
- if (!PyArg_ParseTuple(args, "U|zO:charmap_encode",
+ if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
&str, &errors, &mapping))
return NULL;
if (mapping == Py_None)
mapping = NULL;
- return codec_tuple(PyUnicode_EncodeCharmap(
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeCharmap(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
mapping,
errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
#ifdef MS_WIN32
@@ -517,18 +567,23 @@ static PyObject *
mbcs_encode(PyObject *self,
PyObject *args)
{
- PyObject *str;
+ PyObject *str, *v;
const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "U|z:mbcs_encode",
+ if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
&str, &errors))
return NULL;
- return codec_tuple(PyUnicode_EncodeMBCS(
+ str = PyUnicode_FromObject(str);
+ if (str == NULL)
+ return NULL;
+ v = codec_tuple(PyUnicode_EncodeMBCS(
PyUnicode_AS_UNICODE(str),
PyUnicode_GET_SIZE(str),
errors),
- PyUnicode_GET_SIZE(str));
+ PyUnicode_GET_SIZE(str));
+ Py_DECREF(str);
+ return v;
}
#endif /* MS_WIN32 */