Mercurial > cpython-lru_cache-weakref
diff Objects/stringobject.c @ 32551:f46d49e2e0f0 legacy-trunk
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.
| author | Marc-André Lemburg <mal@egenix.com> |
|---|---|
| date | Thu, 08 Jul 2004 17:57:32 +0000 |
| parents | 64534fb3afcd |
| children | 3706a3704aea |
line wrap: on
line diff
--- a/Objects/stringobject.c Thu Jul 08 17:14:17 2004 +0000 +++ b/Objects/stringobject.c Thu Jul 08 17:57:32 2004 +0000 @@ -2673,9 +2673,20 @@ { 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 @@ { 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; }
