]> granicus.if.org Git - python/commitdiff
Marc-Andre Lemburg:
authorGuido van Rossum <guido@python.org>
Tue, 11 Apr 2000 15:38:23 +0000 (15:38 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 11 Apr 2000 15:38:23 +0000 (15:38 +0000)
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.

Python/bltinmodule.c

index 3016d567584a329c9d7778bc35207054ff64d067..cd3db231d2e65b5c299bd4770e7648220ce5cd5b 100644 (file)
@@ -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[] =