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