From 08a1a7f7498201b813fd584541280a46ec60bf0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lemburg?= Date: Thu, 8 Jul 2004 17:57:32 +0000 Subject: 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. --- Objects/stringobject.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'Objects/stringobject.c') diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 7fade569c3..866e7e84ba 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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; } -- cgit v1.2.1