summaryrefslogtreecommitdiffstats
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorMarc-André Lemburg <mal@egenix.com>2004-07-08 17:57:32 (GMT)
committerMarc-André Lemburg <mal@egenix.com>2004-07-08 17:57:32 (GMT)
commitd2d4598ec2e33e73efcbf295e630296b24a1b70b (patch)
tree75a03f4df07b3c998bfcbb334f051b5f4ada371c /Objects/stringobject.c
parent302fa6dc0d90f3e5aac66347458c69713ae7581d (diff)
downloadcpython-d2d4598ec2e33e73efcbf295e630296b24a1b70b.zip
cpython-d2d4598ec2e33e73efcbf295e630296b24a1b70b.tar.gz
cpython-d2d4598ec2e33e73efcbf295e630296b24a1b70b.tar.bz2
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.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 7fade56..866e7e8 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;
}