Bug #1704793: Raise KeyError if unicodedata.lookup cannot

represent the result in a single character.
This commit is contained in:
Martin v. Löwis 2007-07-28 07:01:43 +00:00
parent 0e258c5ea5
commit bd28ca65d6
3 changed files with 19 additions and 2 deletions

View File

@ -6,7 +6,7 @@
"""#" """#"
import unittest, test.test_support import unittest, test.test_support
import hashlib import hashlib, sys
encoding = 'utf-8' encoding = 'utf-8'
@ -214,6 +214,10 @@ class UnicodeMiscTest(UnicodeDatabaseTest):
count += 1 count += 1
self.assert_(count >= 10) # should have tested at least the ASCII digits 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(): def test_main():
test.test_support.run_unittest( test.test_support.run_unittest(
UnicodeMiscTest, UnicodeMiscTest,

View File

@ -26,6 +26,9 @@ Core and builtins
Library 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 - Change location of the package index to pypi.python.org/pypi
- Bug #1701409: Fix a segfault in printing ctypes.c_char_p and - Bug #1701409: Fix a segfault in printing ctypes.c_char_p and

View File

@ -1102,6 +1102,16 @@ unicodedata_lookup(PyObject* self, PyObject* args)
return NULL; 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; str[0] = (Py_UNICODE) code;
return PyUnicode_FromUnicode(str, 1); return PyUnicode_FromUnicode(str, 1);
} }