mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
Allow string and unicode return types from .encode()/.decode()
methods on string and unicode objects. Added unicode.decode() which was missing for no apparent reason.
This commit is contained in:
parent
302fa6dc0d
commit
d2d4598ec2
3 changed files with 130 additions and 7 deletions
|
@ -2673,9 +2673,20 @@ string_encode(PyStringObject *self, PyObject *args)
|
|||
{
|
||||
char *encoding = NULL;
|
||||
char *errors = NULL;
|
||||
PyObject *v;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors))
|
||||
return NULL;
|
||||
return PyString_AsEncodedObject((PyObject *)self, encoding, errors);
|
||||
v = PyString_AsEncodedObject((PyObject *)self, encoding, errors);
|
||||
if (!PyString_Check(v) && !PyUnicode_Check(v)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"encoder did not return a string/unicode object "
|
||||
"(type=%.400s)",
|
||||
v->ob_type->tp_name);
|
||||
Py_DECREF(v);
|
||||
return NULL;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
@ -2694,9 +2705,20 @@ string_decode(PyStringObject *self, PyObject *args)
|
|||
{
|
||||
char *encoding = NULL;
|
||||
char *errors = NULL;
|
||||
PyObject *v;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
|
||||
return NULL;
|
||||
return PyString_AsDecodedObject((PyObject *)self, encoding, errors);
|
||||
v = PyString_AsDecodedObject((PyObject *)self, encoding, errors);
|
||||
if (!PyString_Check(v) && !PyUnicode_Check(v)) {
|
||||
PyErr_Format(PyExc_TypeError,
|
||||
"decoder did not return a string/unicode object "
|
||||
"(type=%.400s)",
|
||||
v->ob_type->tp_name);
|
||||
Py_DECREF(v);
|
||||
return NULL;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue