mirror of https://github.com/python/cpython
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.
This commit is contained in:
parent
0afff388ce
commit
b425f5e35b
|
@ -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))
|
||||
if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
|
||||
&obj, &errors))
|
||||
return NULL;
|
||||
|
||||
return codec_tuple(PyUnicode_FromUnicode((Py_UNICODE *)data,
|
||||
size / sizeof(Py_UNICODE)),
|
||||
size);
|
||||
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},
|
||||
|
|
Loading…
Reference in New Issue