test_ucn.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. """ Test script for the Unicode implementation.
  2. Written by Bill Tutt.
  3. Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)
  4. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  5. """#"
  6. import ast
  7. import unittest
  8. import unicodedata
  9. from test import support
  10. from http.client import HTTPException
  11. try:
  12. from _testcapi import INT_MAX, PY_SSIZE_T_MAX, UINT_MAX
  13. except ImportError:
  14. INT_MAX = PY_SSIZE_T_MAX = UINT_MAX = 2**64 - 1
  15. class UnicodeNamesTest(unittest.TestCase):
  16. def checkletter(self, name, code):
  17. # Helper that put all \N escapes inside eval'd raw strings,
  18. # to make sure this script runs even if the compiler
  19. # chokes on \N escapes
  20. res = ast.literal_eval(r'"\N{%s}"' % name)
  21. self.assertEqual(res, code)
  22. return res
  23. def test_general(self):
  24. # General and case insensitivity test:
  25. chars = [
  26. "LATIN CAPITAL LETTER T",
  27. "LATIN SMALL LETTER H",
  28. "LATIN SMALL LETTER E",
  29. "SPACE",
  30. "LATIN SMALL LETTER R",
  31. "LATIN CAPITAL LETTER E",
  32. "LATIN SMALL LETTER D",
  33. "SPACE",
  34. "LATIN SMALL LETTER f",
  35. "LATIN CAPITAL LeTtEr o",
  36. "LATIN SMaLl LETTER x",
  37. "SPACE",
  38. "LATIN SMALL LETTER A",
  39. "LATIN SMALL LETTER T",
  40. "LATIN SMALL LETTER E",
  41. "SPACE",
  42. "LATIN SMALL LETTER T",
  43. "LATIN SMALL LETTER H",
  44. "LATIN SMALL LETTER E",
  45. "SpAcE",
  46. "LATIN SMALL LETTER S",
  47. "LATIN SMALL LETTER H",
  48. "LATIN small LETTER e",
  49. "LATIN small LETTER e",
  50. "LATIN SMALL LETTER P",
  51. "FULL STOP"
  52. ]
  53. string = "The rEd fOx ate the sheep."
  54. self.assertEqual(
  55. "".join([self.checkletter(*args) for args in zip(chars, string)]),
  56. string
  57. )
  58. def test_ascii_letters(self):
  59. for char in "".join(map(chr, range(ord("a"), ord("z")))):
  60. name = "LATIN SMALL LETTER %s" % char.upper()
  61. code = unicodedata.lookup(name)
  62. self.assertEqual(unicodedata.name(code), name)
  63. def test_hangul_syllables(self):
  64. self.checkletter("HANGUL SYLLABLE GA", "\uac00")
  65. self.checkletter("HANGUL SYLLABLE GGWEOSS", "\uafe8")
  66. self.checkletter("HANGUL SYLLABLE DOLS", "\ub3d0")
  67. self.checkletter("HANGUL SYLLABLE RYAN", "\ub7b8")
  68. self.checkletter("HANGUL SYLLABLE MWIK", "\ubba0")
  69. self.checkletter("HANGUL SYLLABLE BBWAEM", "\ubf88")
  70. self.checkletter("HANGUL SYLLABLE SSEOL", "\uc370")
  71. self.checkletter("HANGUL SYLLABLE YI", "\uc758")
  72. self.checkletter("HANGUL SYLLABLE JJYOSS", "\ucb40")
  73. self.checkletter("HANGUL SYLLABLE KYEOLS", "\ucf28")
  74. self.checkletter("HANGUL SYLLABLE PAN", "\ud310")
  75. self.checkletter("HANGUL SYLLABLE HWEOK", "\ud6f8")
  76. self.checkletter("HANGUL SYLLABLE HIH", "\ud7a3")
  77. self.assertRaises(ValueError, unicodedata.name, "\ud7a4")
  78. def test_cjk_unified_ideographs(self):
  79. self.checkletter("CJK UNIFIED IDEOGRAPH-3400", "\u3400")
  80. self.checkletter("CJK UNIFIED IDEOGRAPH-4DB5", "\u4db5")
  81. self.checkletter("CJK UNIFIED IDEOGRAPH-4E00", "\u4e00")
  82. self.checkletter("CJK UNIFIED IDEOGRAPH-9FCB", "\u9fCB")
  83. self.checkletter("CJK UNIFIED IDEOGRAPH-20000", "\U00020000")
  84. self.checkletter("CJK UNIFIED IDEOGRAPH-2A6D6", "\U0002a6d6")
  85. self.checkletter("CJK UNIFIED IDEOGRAPH-2A700", "\U0002A700")
  86. self.checkletter("CJK UNIFIED IDEOGRAPH-2B734", "\U0002B734")
  87. self.checkletter("CJK UNIFIED IDEOGRAPH-2B740", "\U0002B740")
  88. self.checkletter("CJK UNIFIED IDEOGRAPH-2B81D", "\U0002B81D")
  89. self.checkletter("CJK UNIFIED IDEOGRAPH-3134A", "\U0003134A")
  90. def test_bmp_characters(self):
  91. for code in range(0x10000):
  92. char = chr(code)
  93. name = unicodedata.name(char, None)
  94. if name is not None:
  95. self.assertEqual(unicodedata.lookup(name), char)
  96. def test_misc_symbols(self):
  97. self.checkletter("PILCROW SIGN", "\u00b6")
  98. self.checkletter("REPLACEMENT CHARACTER", "\uFFFD")
  99. self.checkletter("HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK", "\uFF9F")
  100. self.checkletter("FULLWIDTH LATIN SMALL LETTER A", "\uFF41")
  101. def test_aliases(self):
  102. # Check that the aliases defined in the NameAliases.txt file work.
  103. # This should be updated when new aliases are added or the file
  104. # should be downloaded and parsed instead. See #12753.
  105. aliases = [
  106. ('LATIN CAPITAL LETTER GHA', 0x01A2),
  107. ('LATIN SMALL LETTER GHA', 0x01A3),
  108. ('KANNADA LETTER LLLA', 0x0CDE),
  109. ('LAO LETTER FO FON', 0x0E9D),
  110. ('LAO LETTER FO FAY', 0x0E9F),
  111. ('LAO LETTER RO', 0x0EA3),
  112. ('LAO LETTER LO', 0x0EA5),
  113. ('TIBETAN MARK BKA- SHOG GI MGO RGYAN', 0x0FD0),
  114. ('YI SYLLABLE ITERATION MARK', 0xA015),
  115. ('PRESENTATION FORM FOR VERTICAL RIGHT WHITE LENTICULAR BRACKET', 0xFE18),
  116. ('BYZANTINE MUSICAL SYMBOL FTHORA SKLIRON CHROMA VASIS', 0x1D0C5)
  117. ]
  118. for alias, codepoint in aliases:
  119. self.checkletter(alias, chr(codepoint))
  120. name = unicodedata.name(chr(codepoint))
  121. self.assertNotEqual(name, alias)
  122. self.assertEqual(unicodedata.lookup(alias),
  123. unicodedata.lookup(name))
  124. with self.assertRaises(KeyError):
  125. unicodedata.ucd_3_2_0.lookup(alias)
  126. def test_aliases_names_in_pua_range(self):
  127. # We are storing aliases in the PUA 15, but their names shouldn't leak
  128. for cp in range(0xf0000, 0xf0100):
  129. with self.assertRaises(ValueError) as cm:
  130. unicodedata.name(chr(cp))
  131. self.assertEqual(str(cm.exception), 'no such name')
  132. def test_named_sequences_names_in_pua_range(self):
  133. # We are storing named seq in the PUA 15, but their names shouldn't leak
  134. for cp in range(0xf0100, 0xf0fff):
  135. with self.assertRaises(ValueError) as cm:
  136. unicodedata.name(chr(cp))
  137. self.assertEqual(str(cm.exception), 'no such name')
  138. def test_named_sequences_sample(self):
  139. # Check a few named sequences. See #12753.
  140. sequences = [
  141. ('LATIN SMALL LETTER R WITH TILDE', '\u0072\u0303'),
  142. ('TAMIL SYLLABLE SAI', '\u0BB8\u0BC8'),
  143. ('TAMIL SYLLABLE MOO', '\u0BAE\u0BCB'),
  144. ('TAMIL SYLLABLE NNOO', '\u0BA3\u0BCB'),
  145. ('TAMIL CONSONANT KSS', '\u0B95\u0BCD\u0BB7\u0BCD'),
  146. ]
  147. for seqname, codepoints in sequences:
  148. self.assertEqual(unicodedata.lookup(seqname), codepoints)
  149. with self.assertRaises(SyntaxError):
  150. self.checkletter(seqname, None)
  151. with self.assertRaises(KeyError):
  152. unicodedata.ucd_3_2_0.lookup(seqname)
  153. def test_named_sequences_full(self):
  154. # Check all the named sequences
  155. def check_version(testfile):
  156. hdr = testfile.readline()
  157. return unicodedata.unidata_version in hdr
  158. url = ("http://www.pythontest.net/unicode/%s/NamedSequences.txt" %
  159. unicodedata.unidata_version)
  160. try:
  161. testdata = support.open_urlresource(url, encoding="utf-8",
  162. check=check_version)
  163. except (OSError, HTTPException):
  164. self.skipTest("Could not retrieve " + url)
  165. self.addCleanup(testdata.close)
  166. for line in testdata:
  167. line = line.strip()
  168. if not line or line.startswith('#'):
  169. continue
  170. seqname, codepoints = line.split(';')
  171. codepoints = ''.join(chr(int(cp, 16)) for cp in codepoints.split())
  172. self.assertEqual(unicodedata.lookup(seqname), codepoints)
  173. with self.assertRaises(SyntaxError):
  174. self.checkletter(seqname, None)
  175. with self.assertRaises(KeyError):
  176. unicodedata.ucd_3_2_0.lookup(seqname)
  177. def test_errors(self):
  178. self.assertRaises(TypeError, unicodedata.name)
  179. self.assertRaises(TypeError, unicodedata.name, 'xx')
  180. self.assertRaises(TypeError, unicodedata.lookup)
  181. self.assertRaises(KeyError, unicodedata.lookup, 'unknown')
  182. def test_strict_error_handling(self):
  183. # bogus character name
  184. self.assertRaises(
  185. UnicodeError,
  186. str, b"\\N{blah}", 'unicode-escape', 'strict'
  187. )
  188. # long bogus character name
  189. self.assertRaises(
  190. UnicodeError,
  191. str, bytes("\\N{%s}" % ("x" * 100000), "ascii"), 'unicode-escape', 'strict'
  192. )
  193. # missing closing brace
  194. self.assertRaises(
  195. UnicodeError,
  196. str, b"\\N{SPACE", 'unicode-escape', 'strict'
  197. )
  198. # missing opening brace
  199. self.assertRaises(
  200. UnicodeError,
  201. str, b"\\NSPACE", 'unicode-escape', 'strict'
  202. )
  203. @support.cpython_only
  204. @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX")
  205. @support.bigmemtest(size=UINT_MAX + 1, memuse=2 + 1, dry_run=False)
  206. def test_issue16335(self, size):
  207. # very very long bogus character name
  208. x = b'\\N{SPACE' + b'x' * (UINT_MAX + 1) + b'}'
  209. self.assertEqual(len(x), len(b'\\N{SPACE}') + (UINT_MAX + 1))
  210. self.assertRaisesRegex(UnicodeError,
  211. 'unknown Unicode character name',
  212. x.decode, 'unicode-escape'
  213. )
  214. if __name__ == "__main__":
  215. unittest.main()