]> granicus.if.org Git - python/commitdiff
Decode untyped strings from UTF-8.
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 4 Dec 2002 19:54:36 +0000 (19:54 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 4 Dec 2002 19:54:36 +0000 (19:54 +0000)
Modules/_tkinter.c

index 22850a4ff52b728eeb126a2b4a5b436056aabbad..a42b7826087f673eb686ad4e1c5b4c0615761275 100644 (file)
@@ -770,8 +770,33 @@ FromObj(PyObject* tkapp, Tcl_Obj *value)
        PyObject *result = NULL;
        TkappObject *app = (TkappObject*)tkapp;
 
-       if (value->typePtr == NULL)
-               return PyString_FromStringAndSize(value->bytes, value->length);
+       if (value->typePtr == NULL) {
+               /* If the result contains any bytes with the top bit set,
+                  it's UTF-8 and we should decode it to Unicode */
+#ifdef Py_USING_UNICODE
+               int i;
+               char *s = value->bytes;
+               int len = value->length;
+               for (i = 0; i < len; i++) {
+                       if (value->bytes[i] & 0x80)
+                               break;
+               }
+
+               if (i == value->length)
+                       result = PyString_FromStringAndSize(s, len);
+               else {
+                       /* Convert UTF-8 to Unicode string */
+                       result = PyUnicode_DecodeUTF8(s, len, "strict");
+                       if (result == NULL) {
+                               PyErr_Clear();
+                               result = PyString_FromStringAndSize(s, len);
+                       }
+               }
+#else
+               res = PyString_FromStringAndSize(value->bytes, value->length);
+#endif
+               return result;
+       }
 
        if (value->typePtr == app->BooleanType) {
                result = value->internalRep.longValue ? Py_True : Py_False;