]> granicus.if.org Git - python/commitdiff
Bug #1704793: Raise KeyError if unicodedata.lookup cannot
authorMartin v. Löwis <martin@v.loewis.de>
Sat, 28 Jul 2007 07:01:43 +0000 (07:01 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sat, 28 Jul 2007 07:01:43 +0000 (07:01 +0000)
represent the result in a single character.

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

index 0023bf440691d15e59c8aea1d8583c4d952d387b..92353f1ea978dc67587d6929f28d5fa4adcd6ef4 100644 (file)
@@ -6,7 +6,7 @@
 
 """#"
 import unittest, test.test_support
-import hashlib
+import hashlib, sys
 
 encoding = 'utf-8'
 
@@ -214,6 +214,10 @@ class UnicodeMiscTest(UnicodeDatabaseTest):
                 count += 1
         self.assert_(count >= 10) # should have tested at least the ASCII digits
 
+    def test_bug_1704793(self):
+        if sys.maxunicode == 65535:
+            self.assertRaises(KeyError, self.db.lookup, "GOTHIC LETTER FAIHU")
+
 def test_main():
     test.test_support.run_unittest(
         UnicodeMiscTest,
index 8ca898b469d39f5d20f1a692e79cf74087c95875..f42c8ec7405a3dbc2b9e749c0ba23d97987d75f7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,9 @@ Core and builtins
 Library
 -------
 
+- Bug #1704793: Raise KeyError if unicodedata.lookup cannot
+  represent the result in a single character.
+
 - Change location of the package index to pypi.python.org/pypi
 
 - Bug #1701409: Fix a segfault in printing ctypes.c_char_p and
index a30d30c8eb7291e26d95f06ab31ec1c67540727f..1a32b644743a4d72709270d9e9f60e99b7239a7c 100644 (file)
@@ -1102,8 +1102,18 @@ unicodedata_lookup(PyObject* self, PyObject* args)
         return NULL;
     }
 
+#ifndef Py_UNICODE_WIDE
+    if (code >= 0x10000) {
+        /* Raise KeyError for compatibility; the possibly more
+           correct ValueError was not documented as a possible
+           exception for 2.5.x and earlier. */
+        PyErr_Format(PyExc_KeyError, "result %d larger than sys.maxunicode",
+                     code);
+        return NULL;
+    }
+#endif
     str[0] = (Py_UNICODE) code;
-    return PyUnicode_FromUnicode(str, 1);
+    return PyUnicode_FromUnicode(str, 1);    
 }
 
 /* XXX Add doc strings. */