[Python-checkins] r56601 - in python/trunk: Lib/test/test_unicodedata.py Misc/NEWS Modules/unicodedata.c

martin.v.loewis python-checkins at python.org
Sat Jul 28 09:03:05 CEST 2007


Author: martin.v.loewis
Date: Sat Jul 28 09:03:05 2007
New Revision: 56601

Modified:
   python/trunk/Lib/test/test_unicodedata.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/unicodedata.c
Log:
Bug #1704793: Return UTF-16 pair if unicodedata.lookup cannot
represent the result in a single character.


Modified: python/trunk/Lib/test/test_unicodedata.py
==============================================================================
--- python/trunk/Lib/test/test_unicodedata.py	(original)
+++ python/trunk/Lib/test/test_unicodedata.py	Sat Jul 28 09:03:05 2007
@@ -214,6 +214,9 @@
                 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,

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Jul 28 09:03:05 2007
@@ -238,6 +238,9 @@
 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

Modified: python/trunk/Modules/unicodedata.c
==============================================================================
--- python/trunk/Modules/unicodedata.c	(original)
+++ python/trunk/Modules/unicodedata.c	Sat Jul 28 09:03:05 2007
@@ -1077,8 +1077,7 @@
 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 @@
         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. */


More information about the Python-checkins mailing list