diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2002-12-04 19:54:36 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2002-12-04 19:54:36 (GMT) |
commit | e07e18d41b5b5ac962c2e4c0dfaae79641452e1f (patch) | |
tree | 0de022eceebd984b22042cdc5b4a7b4c0c28fd63 /Modules | |
parent | 7e5bf674b896ec4fd7a9eea98e7ab3fc8deb16cb (diff) | |
download | cpython-e07e18d41b5b5ac962c2e4c0dfaae79641452e1f.zip cpython-e07e18d41b5b5ac962c2e4c0dfaae79641452e1f.tar.gz cpython-e07e18d41b5b5ac962c2e4c0dfaae79641452e1f.tar.bz2 |
Decode untyped strings from UTF-8.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_tkinter.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 22850a4..a42b782 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -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; |