Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b425f5e

Browse files
committed
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.
1 parent 0afff38 commit b425f5e

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

Modules/_codecsmodule.c

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,24 @@ static PyObject *
102102
unicode_internal_decode(PyObject *self,
103103
PyObject *args)
104104
{
105+
PyObject *obj;
106+
const char *errors = NULL;
105107
const char *data;
106108
int size;
107-
const char *errors = NULL;
108109

109-
if (!PyArg_ParseTuple(args, "s#|z:unicode_internal_decode",
110-
&data, &size, &errors))
111-
return NULL;
112-
113-
return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
114-
size / sizeof(Py_UNICODE)),
115-
size);
110+
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
111+
&obj, &errors))
112+
return NULL;
113+
114+
if (PyUnicode_Check(obj))
115+
return codec_tuple(obj, PyUnicode_GET_SIZE(obj));
116+
else {
117+
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
118+
return NULL;
119+
return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
120+
size / sizeof(Py_UNICODE)),
121+
size);
122+
}
116123
}
117124

118125
static PyObject *
@@ -346,6 +353,33 @@ charbuffer_encode(PyObject *self,
346353
size);
347354
}
348355

356+
static PyObject *
357+
unicode_internal_encode(PyObject *self,
358+
PyObject *args)
359+
{
360+
PyObject *obj;
361+
const char *errors = NULL;
362+
const char *data;
363+
int size;
364+
365+
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
366+
&obj, &errors))
367+
return NULL;
368+
369+
if (PyUnicode_Check(obj)) {
370+
data = PyUnicode_AS_DATA(obj);
371+
size = PyUnicode_GET_DATA_SIZE(obj);
372+
return codec_tuple(PyString_FromStringAndSize(data, size),
373+
size);
374+
}
375+
else {
376+
if (PyObject_AsReadBuffer(obj, (const void **)&data, &size))
377+
return NULL;
378+
return codec_tuple(PyString_FromStringAndSize(data, size),
379+
size);
380+
}
381+
}
382+
349383
static PyObject *
350384
utf_8_encode(PyObject *self,
351385
PyObject *args)
@@ -604,7 +638,7 @@ static PyMethodDef _codecs_functions[] = {
604638
{"utf_16_ex_decode", utf_16_ex_decode, 1},
605639
{"unicode_escape_encode", unicode_escape_encode, 1},
606640
{"unicode_escape_decode", unicode_escape_decode, 1},
607-
{"unicode_internal_encode", readbuffer_encode, 1},
641+
{"unicode_internal_encode", unicode_internal_encode, 1},
608642
{"unicode_internal_decode", unicode_internal_decode, 1},
609643
{"raw_unicode_escape_encode", raw_unicode_escape_encode, 1},
610644
{"raw_unicode_escape_decode", raw_unicode_escape_decode, 1},

0 commit comments

Comments
 (0)