mirror of
https://github.com/python/cpython.git
synced 2025-10-31 21:51:50 +00:00
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.
This commit is contained in:
parent
a3277139f1
commit
3afba7644b
1 changed files with 17 additions and 4 deletions
|
|
@ -165,15 +165,28 @@ builtin_unicode(self, args)
|
||||||
PyObject *self;
|
PyObject *self;
|
||||||
PyObject *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *s;
|
PyObject *v;
|
||||||
|
const void *buffer;
|
||||||
int len;
|
int len;
|
||||||
char *encoding = NULL;
|
char *encoding = NULL;
|
||||||
char *errors = NULL;
|
char *errors = NULL;
|
||||||
|
|
||||||
if ( !PyArg_ParseTuple(args, "s#|ss:unicode", &s, &len,
|
if ( !PyArg_ParseTuple(args, "O|ss:unicode", &v, &encoding, &errors) )
|
||||||
&encoding, &errors) )
|
|
||||||
return NULL;
|
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[] =
|
static char unicode_doc[] =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue