You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
9.4 KiB

Merged revisions 55007-55179 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ........ r55077 | guido.van.rossum | 2007-05-02 11:54:37 -0700 (Wed, 02 May 2007) | 2 lines Use the new print syntax, at least. ........ r55142 | fred.drake | 2007-05-04 21:27:30 -0700 (Fri, 04 May 2007) | 1 line remove old cruftiness ........ r55143 | fred.drake | 2007-05-04 21:52:16 -0700 (Fri, 04 May 2007) | 1 line make this work with the new Python ........ r55162 | neal.norwitz | 2007-05-06 22:29:18 -0700 (Sun, 06 May 2007) | 1 line Get asdl code gen working with Python 2.3. Should continue to work with 3.0 ........ r55164 | neal.norwitz | 2007-05-07 00:00:38 -0700 (Mon, 07 May 2007) | 1 line Verify checkins to p3yk (sic) branch go to 3000 list. ........ r55166 | neal.norwitz | 2007-05-07 00:12:35 -0700 (Mon, 07 May 2007) | 1 line Fix this test so it runs again by importing warnings_test properly. ........ r55167 | neal.norwitz | 2007-05-07 01:03:22 -0700 (Mon, 07 May 2007) | 8 lines So long xrange. range() now supports values that are outside -sys.maxint to sys.maxint. floats raise a TypeError. This has been sitting for a long time. It probably has some problems and needs cleanup. Objects/rangeobject.c now uses 4-space indents since it is almost completely new. ........ r55171 | guido.van.rossum | 2007-05-07 10:21:26 -0700 (Mon, 07 May 2007) | 4 lines Fix two tests that were previously depending on significant spaces at the end of a line (and before that on Python 2.x print behavior that has no exact equivalent in 3.0). ........
19 years ago
Merged revisions 55007-55179 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/p3yk ........ r55077 | guido.van.rossum | 2007-05-02 11:54:37 -0700 (Wed, 02 May 2007) | 2 lines Use the new print syntax, at least. ........ r55142 | fred.drake | 2007-05-04 21:27:30 -0700 (Fri, 04 May 2007) | 1 line remove old cruftiness ........ r55143 | fred.drake | 2007-05-04 21:52:16 -0700 (Fri, 04 May 2007) | 1 line make this work with the new Python ........ r55162 | neal.norwitz | 2007-05-06 22:29:18 -0700 (Sun, 06 May 2007) | 1 line Get asdl code gen working with Python 2.3. Should continue to work with 3.0 ........ r55164 | neal.norwitz | 2007-05-07 00:00:38 -0700 (Mon, 07 May 2007) | 1 line Verify checkins to p3yk (sic) branch go to 3000 list. ........ r55166 | neal.norwitz | 2007-05-07 00:12:35 -0700 (Mon, 07 May 2007) | 1 line Fix this test so it runs again by importing warnings_test properly. ........ r55167 | neal.norwitz | 2007-05-07 01:03:22 -0700 (Mon, 07 May 2007) | 8 lines So long xrange. range() now supports values that are outside -sys.maxint to sys.maxint. floats raise a TypeError. This has been sitting for a long time. It probably has some problems and needs cleanup. Objects/rangeobject.c now uses 4-space indents since it is almost completely new. ........ r55171 | guido.van.rossum | 2007-05-07 10:21:26 -0700 (Mon, 07 May 2007) | 4 lines Fix two tests that were previously depending on significant spaces at the end of a line (and before that on Python 2.x print behavior that has no exact equivalent in 3.0). ........
19 years ago
  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 unittest
  7. import unicodedata
  8. from test import support
  9. from http.client import HTTPException
  10. from test.test_normalization import check_version
  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 = 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. def test_bmp_characters(self):
  90. for code in range(0x10000):
  91. char = chr(code)
  92. name = unicodedata.name(char, None)
  93. if name is not None:
  94. self.assertEqual(unicodedata.lookup(name), char)
  95. def test_misc_symbols(self):
  96. self.checkletter("PILCROW SIGN", "\u00b6")
  97. self.checkletter("REPLACEMENT CHARACTER", "\uFFFD")
  98. self.checkletter("HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK", "\uFF9F")
  99. self.checkletter("FULLWIDTH LATIN SMALL LETTER A", "\uFF41")
  100. def test_aliases(self):
  101. # Check that the aliases defined in the NameAliases.txt file work.
  102. # This should be updated when new aliases are added or the file
  103. # should be downloaded and parsed instead. See #12753.
  104. aliases = [
  105. ('LATIN CAPITAL LETTER GHA', 0x01A2),
  106. ('LATIN SMALL LETTER GHA', 0x01A3),
  107. ('KANNADA LETTER LLLA', 0x0CDE),
  108. ('LAO LETTER FO FON', 0x0E9D),
  109. ('LAO LETTER FO FAY', 0x0E9F),
  110. ('LAO LETTER RO', 0x0EA3),
  111. ('LAO LETTER LO', 0x0EA5),
  112. ('TIBETAN MARK BKA- SHOG GI MGO RGYAN', 0x0FD0),
  113. ('YI SYLLABLE ITERATION MARK', 0xA015),
  114. ('PRESENTATION FORM FOR VERTICAL RIGHT WHITE LENTICULAR BRACKET', 0xFE18),
  115. ('BYZANTINE MUSICAL SYMBOL FTHORA SKLIRON CHROMA VASIS', 0x1D0C5)
  116. ]
  117. for alias, codepoint in aliases:
  118. self.checkletter(alias, chr(codepoint))
  119. name = unicodedata.name(chr(codepoint))
  120. self.assertNotEqual(name, alias)
  121. self.assertEqual(unicodedata.lookup(alias),
  122. unicodedata.lookup(name))
  123. with self.assertRaises(KeyError):
  124. unicodedata.ucd_3_2_0.lookup(alias)
  125. def test_aliases_names_in_pua_range(self):
  126. # We are storing aliases in the PUA 15, but their names shouldn't leak
  127. for cp in range(0xf0000, 0xf0100):
  128. with self.assertRaises(ValueError) as cm:
  129. unicodedata.name(chr(cp))
  130. self.assertEqual(str(cm.exception), 'no such name')
  131. def test_named_sequences_names_in_pua_range(self):
  132. # We are storing named seq in the PUA 15, but their names shouldn't leak
  133. for cp in range(0xf0100, 0xf0fff):
  134. with self.assertRaises(ValueError) as cm:
  135. unicodedata.name(chr(cp))
  136. self.assertEqual(str(cm.exception), 'no such name')
  137. def test_named_sequences_sample(self):
  138. # Check a few named sequences. See #12753.
  139. sequences = [
  140. ('LATIN SMALL LETTER R WITH TILDE', '\u0072\u0303'),
  141. ('TAMIL SYLLABLE SAI', '\u0BB8\u0BC8'),
  142. ('TAMIL SYLLABLE MOO', '\u0BAE\u0BCB'),
  143. ('TAMIL SYLLABLE NNOO', '\u0BA3\u0BCB'),
  144. ('TAMIL CONSONANT KSS', '\u0B95\u0BCD\u0BB7\u0BCD'),
  145. ]
  146. for seqname, codepoints in sequences:
  147. self.assertEqual(unicodedata.lookup(seqname), codepoints)
  148. with self.assertRaises(SyntaxError):
  149. self.checkletter(seqname, None)
  150. with self.assertRaises(KeyError):
  151. unicodedata.ucd_3_2_0.lookup(seqname)
  152. def test_named_sequences_full(self):
  153. # Check all the named sequences
  154. url = ("http://www.pythontest.net/unicode/%s/NamedSequences.txt" %
  155. unicodedata.unidata_version)
  156. try:
  157. testdata = support.open_urlresource(url, encoding="utf-8",
  158. check=check_version)
  159. except (OSError, HTTPException):
  160. self.skipTest("Could not retrieve " + url)
  161. self.addCleanup(testdata.close)
  162. for line in testdata:
  163. line = line.strip()
  164. if not line or line.startswith('#'):
  165. continue
  166. seqname, codepoints = line.split(';')
  167. codepoints = ''.join(chr(int(cp, 16)) for cp in codepoints.split())
  168. self.assertEqual(unicodedata.lookup(seqname), codepoints)
  169. with self.assertRaises(SyntaxError):
  170. self.checkletter(seqname, None)
  171. with self.assertRaises(KeyError):
  172. unicodedata.ucd_3_2_0.lookup(seqname)
  173. def test_errors(self):
  174. self.assertRaises(TypeError, unicodedata.name)
  175. self.assertRaises(TypeError, unicodedata.name, 'xx')
  176. self.assertRaises(TypeError, unicodedata.lookup)
  177. self.assertRaises(KeyError, unicodedata.lookup, 'unknown')
  178. def test_strict_error_handling(self):
  179. # bogus character name
  180. self.assertRaises(
  181. UnicodeError,
  182. str, b"\\N{blah}", 'unicode-escape', 'strict'
  183. )
  184. # long bogus character name
  185. self.assertRaises(
  186. UnicodeError,
  187. str, bytes("\\N{%s}" % ("x" * 100000), "ascii"), 'unicode-escape', 'strict'
  188. )
  189. # missing closing brace
  190. self.assertRaises(
  191. UnicodeError,
  192. str, b"\\N{SPACE", 'unicode-escape', 'strict'
  193. )
  194. # missing opening brace
  195. self.assertRaises(
  196. UnicodeError,
  197. str, b"\\NSPACE", 'unicode-escape', 'strict'
  198. )
  199. @support.cpython_only
  200. @unittest.skipUnless(INT_MAX < PY_SSIZE_T_MAX, "needs UINT_MAX < SIZE_MAX")
  201. @support.bigmemtest(size=UINT_MAX + 1, memuse=2 + 1, dry_run=False)
  202. def test_issue16335(self, size):
  203. # very very long bogus character name
  204. x = b'\\N{SPACE' + b'x' * (UINT_MAX + 1) + b'}'
  205. self.assertEqual(len(x), len(b'\\N{SPACE}') + (UINT_MAX + 1))
  206. self.assertRaisesRegex(UnicodeError,
  207. 'unknown Unicode character name',
  208. x.decode, 'unicode-escape'
  209. )
  210. if __name__ == "__main__":
  211. unittest.main()