diff options
author | Guido van Rossum <guido@python.org> | 2000-04-11 15:38:23 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2000-04-11 15:38:23 (GMT) |
commit | 3afba7644b481eb7ef6baf02fad42a185b0f008e (patch) | |
tree | f4600b1b06af4dba4433d5e05c77babd5aa9f6c8 /Python/bltinmodule.c | |
parent | a3277139f145eed85d688486f38f128782ddec8e (diff) | |
download | cpython-3afba7644b481eb7ef6baf02fad42a185b0f008e.zip cpython-3afba7644b481eb7ef6baf02fad42a185b0f008e.tar.gz cpython-3afba7644b481eb7ef6baf02fad42a185b0f008e.tar.bz2 |
Marc-Andre Lemburg:
Added special case to unicode(): when being passed a
Unicode object as first argument, return the object as-is.
Raises an exception when given a Unicode object *and* an
encoding name.
Diffstat (limited to 'Python/bltinmodule.c')
-rw-r--r-- | Python/bltinmodule.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 3016d56..cd3db23 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -165,15 +165,28 @@ builtin_unicode(self, args) PyObject *self; PyObject *args; { - char *s; + PyObject *v; + const void *buffer; int len; char *encoding = NULL; char *errors = NULL; - if ( !PyArg_ParseTuple(args, "s#|ss:unicode", &s, &len, - &encoding, &errors) ) + if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) ) return NULL; - return PyUnicode_Decode(s, len, encoding, errors); + /* Special case: Unicode will stay Unicode */ + if (PyUnicode_Check(v)) { + if (encoding) { + PyErr_SetString(PyExc_TypeError, + "unicode() does not support decoding of Unicode objects"); + return NULL; + } + Py_INCREF(v); + return v; + } + /* Read raw data and decode it */ + if (PyObject_AsReadBuffer(v, &buffer, &len)) + return NULL; + return PyUnicode_Decode((const char *)buffer, len, encoding, errors); } static char unicode_doc[] = |