summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2000-09-21 21:09:45 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2000-09-21 21:09:45 (GMT)
commitb425f5e35b92ed0ab600c4ab500c7b04f112b68c (patch)
treefb6fa1dac7fcee60d4798ebba3d8385ad2de38d2
parent0afff388ce38cc0a42ef6c8b9a3f815997ee0103 (diff)
downloadcpython-b425f5e35b92ed0ab600c4ab500c7b04f112b68c.zip
cpython-b425f5e35b92ed0ab600c4ab500c7b04f112b68c.tar.gz
cpython-b425f5e35b92ed0ab600c4ab500c7b04f112b68c.tar.bz2
Added a true unicode_internal_encode function and fixed the
unicode_internal_decode function to support Unicode objects directly rather than by generating a copy of the object.
-rw-r--r--Modules/_codecsmodule.c52
1 files changed, 43 insertions, 9 deletions
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 53e63c5..37d89e9 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -102,17 +102,24 @@ static PyObject *
unicode_internal_decode(PyObject *self,
PyObject *args)
{
+ PyObject *obj;
+ const char *errors = NULL;
const char *data;
int size;
- const char *errors = NULL;
- if (!PyArg_ParseTuple(args, "s#|z:unicode_internal_decode",
- &data, &size, &errors))
- return NULL;
-
- return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
- size / sizeof(Py_UNICODE)),
- size);
+ if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
+ &obj, &errors))
+ return NULL;
+
+ if (PyUnicode_Check(obj))
+ return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
+ else {
+ if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
+ return NULL;
+ return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
+ size / sizeof(Py_UNICODE)),
+ size);
+ }
}
static PyObject *
@@ -347,6 +354,33 @@ charbuffer_encode(PyObject *self,
}
static PyObject *
+unicode_internal_encode(PyObject *self,
+ PyObject *args)
+{
+ PyObject *obj;
+ const char *errors = NULL;
+ const char *data;
+ int size;
+
+ if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
+ &obj, &errors))
+ return NULL;
+
+ if (PyUnicode_Check(obj)) {
+ data = PyUnicode_AS_DATA(obj);
+ size = PyUnicode_GET_DATA_SIZE(obj);
+ return codec_tuple(PyString_FromStringAndSize(data, size),
+ size);
+ }
+ else {
+ if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
+ return NULL;
+ return codec_tuple(PyString_FromStringAndSize(data, size),
+ size);
+ }
+}
+
+static PyObject *
utf_8_encode(PyObject *self,
PyObject *args)
{
@@ -604,7 +638,7 @@ static PyMethodDef _codecs_functions[] = {
{"utf_16_ex_decode", utf_16_ex_decode, 1},
{"unicode_escape_encode", unicode_escape_encode, 1},
{"unicode_escape_decode", unicode_escape_decode, 1},
- {"unicode_internal_encode", readbuffer_encode, 1},
+ {"unicode_internal_encode", unicode_internal_encode, 1},
{"unicode_internal_decode", unicode_internal_decode, 1},
{"raw_unicode_escape_encode", raw_unicode_escape_encode, 1},
{"raw_unicode_escape_decode", raw_unicode_escape_decode, 1},