]> granicus.if.org Git - python/commitdiff
Added a true unicode_internal_encode function and fixed the
authorMarc-André Lemburg <mal@egenix.com>
Thu, 21 Sep 2000 21:09:45 +0000 (21:09 +0000)
committerMarc-André Lemburg <mal@egenix.com>
Thu, 21 Sep 2000 21:09:45 +0000 (21:09 +0000)
unicode_internal_decode function to support Unicode objects
directly rather than by generating a copy of the object.

Modules/_codecsmodule.c

index 53e63c5f08010ee50ee86cfacf355773d17dac02..37d89e99ee5317513f648353f457dd9ef7b29e1d 100644 (file)
@@ -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 *
@@ -346,6 +353,33 @@ charbuffer_encode(PyObject *self,
                       size);
 }
 
+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},