]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Bug #1704793: Return UTF-16 pair if unicodedata.lookup cannot
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 28 Jul 2007 07:03:05 +0000 (07:03 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 28 Jul 2007 07:03:05 +0000 (07:03 +0000)
represent the result in a single character.

Lib/test/test_unicodedata.py
Misc/NEWS
Modules/unicodedata.c

index 0023bf440691d15e59c8aea1d8583c4d952d387b..574178d13691663753c3f21081ed6ddce096a4c7 100644 (file)
@@ -214,6 +214,9 @@ class UnicodeMiscTest(UnicodeDatabaseTest):
                 count += 1
         self.assert_(count >= 10) # should have tested at least the ASCII digits
 
+    def test_bug_1704793(self):
+        self.assertEquals(self.db.lookup("GOTHIC LETTER FAIHU"), u'\U00010346')
+
 def test_main():
     test.test_support.run_unittest(
         UnicodeMiscTest,
index 4764dafd121b0808e30e0de44c74e190acea29ba..55ebfa5018953c3532387065e69e66db556a4881 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -238,6 +238,9 @@ Core and builtins
 Library
 -------
 
+- Bug #1704793: Return UTF-16 pair if unicodedata.lookup cannot
+  represent the result in a single character.
+
 - Bug #978833: Close https sockets by releasing the _ssl object.
 
 - Change location of the package index to pypi.python.org/pypi
index fac9adc4c3854ba0afdf38cce877d46bd6238e0d..a0756931aeea3864b351c17559d181d90fd2ec71 100644 (file)
@@ -1077,8 +1077,7 @@ static PyObject *
 unicodedata_lookup(PyObject* self, PyObject* args)
 {
     Py_UCS4 code;
-    Py_UNICODE str[1];
-    char errbuf[256];
+    Py_UNICODE str[2];
 
     char* name;
     int namelen;
@@ -1086,24 +1085,20 @@ unicodedata_lookup(PyObject* self, PyObject* args)
         return NULL;
 
     if (!_getcode(self, name, namelen, &code)) {
-       /* XXX(nnorwitz): why are we allocating for the error msg?
-               Why not always use snprintf? */
-        char fmt[] = "undefined character name '%s'";
-        char *buf = PyMem_MALLOC(sizeof(fmt) + namelen);
-        if (buf)
-            sprintf(buf, fmt, name);
-        else {
-            buf = errbuf;
-            PyOS_snprintf(buf, sizeof(errbuf), fmt, name);
-        }
-        PyErr_SetString(PyExc_KeyError, buf);
-        if (buf != errbuf)
-               PyMem_FREE(buf);
+        PyErr_Format(PyExc_KeyError, "undefined character name '%s'",
+                     name);
         return NULL;
     }
 
+#ifndef Py_UNICODE_WIDE
+    if (code >= 0x10000) {
+        str[0] = 0xd800 + ((code - 0x10000) >> 10);
+        str[1] = 0xdc00 + ((code - 0x10000) & 0x3ff);
+        return PyUnicode_FromUnicode(str, 2);
+    }
+#endif
     str[0] = (Py_UNICODE) code;
-    return PyUnicode_FromUnicode(str, 1);
+    return PyUnicode_FromUnicode(str, 1);    
 }
 
 /* XXX Add doc strings. */