| 1 |
""" Tests for the unicodedata module. |
| 2 |
|
| 3 |
Written by Marc-Andre Lemburg ([email protected]). |
| 4 |
|
| 5 |
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
from functools import partial |
| 10 |
import hashlib |
| 11 |
from http.client import HTTPException |
| 12 |
import sys |
| 13 |
import unicodedata |
| 14 |
import unittest |
| 15 |
from test.support import ( |
| 16 |
open_urlresource, |
| 17 |
requires_resource, |
| 18 |
script_helper, |
| 19 |
cpython_only, |
| 20 |
check_disallow_instantiation, |
| 21 |
force_not_colorized, |
| 22 |
is_resource_enabled, |
| 23 |
findfile, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
quicktest = not is_resource_enabled('cpu') |
| 28 |
|
| 29 |
def iterallchars(): |
| 30 |
maxunicode = 0xffff if quicktest else sys.maxunicode |
| 31 |
return map(chr, range(maxunicode + 1)) |
| 32 |
|
| 33 |
class UnicodeMethodsTest(unittest.TestCase): |
| 34 |
|
| 35 |
# update this, if the database changes |
| 36 |
expectedchecksum = ('32fa5c486707c3f78321b95340883fd84e950dc5' |
| 37 |
if quicktest else |
| 38 |
'63aa77dcb36b0e1df082ee2a6071caeda7f0955e') |
| 39 |
|
| 40 |
def test_method_checksum(self): |
| 41 |
h = hashlib.sha1() |
| 42 |
for char in iterallchars(): |
| 43 |
s1 = char + 'abc' |
| 44 |
s2 = char + 'ABC' |
| 45 |
s3 = char + '123' |
| 46 |
data = ( |
| 47 |
# Predicates (single char) |
| 48 |
"01"[char.isalnum()], |
| 49 |
"01"[char.isalpha()], |
| 50 |
"01"[char.isdecimal()], |
| 51 |
"01"[char.isdigit()], |
| 52 |
"01"[char.islower()], |
| 53 |
"01"[char.isnumeric()], |
| 54 |
"01"[char.isspace()], |
| 55 |
"01"[char.istitle()], |
| 56 |
"01"[char.isupper()], |
| 57 |
|
| 58 |
# Predicates (multiple chars) |
| 59 |
"01"[s1.isalnum()], |
| 60 |
"01"[s1.isalpha()], |
| 61 |
"01"[s3.isdecimal()], |
| 62 |
"01"[s3.isdigit()], |
| 63 |
"01"[s1.islower()], |
| 64 |
"01"[s3.isnumeric()], |
| 65 |
"01"[(char + ' \t').isspace()], |
| 66 |
"01"[s1.istitle()], |
| 67 |
"01"[s2.isupper()], |
| 68 |
|
| 69 |
# Mappings (single char) |
| 70 |
char.lower(), |
| 71 |
char.upper(), |
| 72 |
char.title(), |
| 73 |
|
| 74 |
# Mappings (multiple chars) |
| 75 |
s1.lower(), |
| 76 |
s2.upper(), |
| 77 |
s1.title(), |
| 78 |
s2.title(), |
| 79 |
|
| 80 |
) |
| 81 |
h.update(''.join(data).encode('utf-8', 'surrogatepass')) |
| 82 |
result = h.hexdigest() |
| 83 |
self.assertEqual(result, self.expectedchecksum) |
| 84 |
|
| 85 |
|
| 86 |
class UnicodeFunctionsTest(unittest.TestCase): |
| 87 |
db = unicodedata |
| 88 |
old = False |
| 89 |
|
| 90 |
# Update this if the database changes. Make sure to do a full rebuild |
| 91 |
# (e.g. 'make distclean && make') to get the correct checksum. |
| 92 |
expectedchecksum = ('a91d306c268ba7d5cdf14d49e63b3f967058869c' |
| 93 |
if quicktest else |
| 94 |
'232affd2a50ec4bd69d2482aa0291385cbdefaba') |
| 95 |
|
| 96 |
def test_function_checksum(self): |
| 97 |
db = self.db |
| 98 |
data = [] |
| 99 |
h = hashlib.sha1() |
| 100 |
|
| 101 |
for char in iterallchars(): |
| 102 |
data = "%.12g%.12g%.12g%s%s%s%s%s%s%s" % ( |
| 103 |
# Properties |
| 104 |
db.digit(char, -1), |
| 105 |
db.numeric(char, -1), |
| 106 |
db.decimal(char, -1), |
| 107 |
db.category(char), |
| 108 |
db.bidirectional(char), |
| 109 |
db.decomposition(char), |
| 110 |
db.mirrored(char), |
| 111 |
db.combining(char), |
| 112 |
db.east_asian_width(char), |
| 113 |
db.name(char, ""), |
| 114 |
) |
| 115 |
h.update(data.encode("ascii")) |
| 116 |
result = h.hexdigest() |
| 117 |
self.assertEqual(result, self.expectedchecksum) |
| 118 |
|
| 119 |
def test_name_inverse_lookup(self): |
| 120 |
for char in iterallchars(): |
| 121 |
looked_name = self.db.name(char, None) |
| 122 |
if looked_name is not None: |
| 123 |
self.assertEqual(self.db.lookup(looked_name), char) |
| 124 |
|
| 125 |
def test_no_names_in_pua(self): |
| 126 |
puas = [*range(0xe000, 0xf8ff), |
| 127 |
*range(0xf0000, 0xfffff), |
| 128 |
*range(0x100000, 0x10ffff)] |
| 129 |
for i in puas: |
| 130 |
char = chr(i) |
| 131 |
self.assertRaises(ValueError, self.db.name, char) |
| 132 |
|
| 133 |
def test_lookup_nonexistant(self): |
| 134 |
# just make sure that lookup can fail |
| 135 |
for nonexistant in [ |
| 136 |
"LATIN SMLL LETR A", |
| 137 |
"OPEN HANDS SIGHS", |
| 138 |
"DREGS", |
| 139 |
"HANDBUG", |
| 140 |
"MODIFIER LETTER CYRILLIC SMALL QUESTION MARK", |
| 141 |
"???", |
| 142 |
]: |
| 143 |
self.assertRaises(KeyError, self.db.lookup, nonexistant) |
| 144 |
|
| 145 |
def test_digit(self): |
| 146 |
self.assertEqual(self.db.digit('A', None), None) |
| 147 |
self.assertEqual(self.db.digit('9'), 9) |
| 148 |
self.assertEqual(self.db.digit('\u215b', None), None) |
| 149 |
self.assertEqual(self.db.digit('\u2468'), 9) |
| 150 |
self.assertEqual(self.db.digit('\U00020000', None), None) |
| 151 |
self.assertEqual(self.db.digit('\U0001D7FD'), 7) |
| 152 |
|
| 153 |
# New in 13.0.0 |
| 154 |
self.assertEqual(self.db.digit('\U0001fbf9', None), 9) |
| 155 |
# New in 14.0.0 |
| 156 |
self.assertEqual(self.db.digit('\U00016ac9', None), 9) |
| 157 |
# New in 15.0.0 |
| 158 |
self.assertEqual(self.db.digit('\U0001e4f9', None), 9) |
| 159 |
|
| 160 |
self.assertRaises(TypeError, self.db.digit) |
| 161 |
self.assertRaises(TypeError, self.db.digit, 'xx') |
| 162 |
self.assertRaises(ValueError, self.db.digit, 'x') |
| 163 |
|
| 164 |
def test_numeric(self): |
| 165 |
self.assertEqual(self.db.numeric('A',None), None) |
| 166 |
self.assertEqual(self.db.numeric('9'), 9) |
| 167 |
self.assertEqual(self.db.numeric('\u215b'), 0.125) |
| 168 |
self.assertEqual(self.db.numeric('\u2468'), 9.0) |
| 169 |
self.assertEqual(self.db.numeric('\U00020000', None), None) |
| 170 |
|
| 171 |
# New in 4.1.0 |
| 172 |
self.assertEqual(self.db.numeric('\U0001012A', None), None if self.old else 9000) |
| 173 |
# New in 5.0.0 |
| 174 |
self.assertEqual(self.db.numeric('\u07c0', None), None if self.old else 0.0) |
| 175 |
# New in 5.1.0 |
| 176 |
self.assertEqual(self.db.numeric('\ua627', None), None if self.old else 7.0) |
| 177 |
# New in 6.0.0 |
| 178 |
self.assertEqual(self.db.numeric('\u0b72', None), None if self.old else 0.25) |
| 179 |
# New in 12.0.0 |
| 180 |
self.assertEqual(self.db.numeric('\U0001ed3c', None), None if self.old else 0.5) |
| 181 |
# New in 13.0.0 |
| 182 |
self.assertEqual(self.db.numeric('\U0001fbf9', None), None if self.old else 9) |
| 183 |
# New in 14.0.0 |
| 184 |
self.assertEqual(self.db.numeric('\U00016ac9', None), None if self.old else 9) |
| 185 |
# New in 15.0.0 |
| 186 |
self.assertEqual(self.db.numeric('\U0001e4f9', None), None if self.old else 9) |
| 187 |
|
| 188 |
self.assertRaises(TypeError, self.db.numeric) |
| 189 |
self.assertRaises(TypeError, self.db.numeric, 'xx') |
| 190 |
self.assertRaises(ValueError, self.db.numeric, 'x') |
| 191 |
|
| 192 |
def test_decimal(self): |
| 193 |
self.assertEqual(self.db.decimal('A',None), None) |
| 194 |
self.assertEqual(self.db.decimal('9'), 9) |
| 195 |
self.assertEqual(self.db.decimal('\u215b', None), None) |
| 196 |
self.assertEqual(self.db.decimal('\u2468', None), None) |
| 197 |
self.assertEqual(self.db.decimal('\U00020000', None), None) |
| 198 |
self.assertEqual(self.db.decimal('\U0001D7FD'), 7) |
| 199 |
|
| 200 |
# New in 4.1.0 |
| 201 |
self.assertEqual(self.db.decimal('\xb2', None), 2 if self.old else None) |
| 202 |
self.assertEqual(self.db.decimal('\u1369', None), 1 if self.old else None) |
| 203 |
# New in 5.0.0 |
| 204 |
self.assertEqual(self.db.decimal('\u07c0', None), None if self.old else 0) |
| 205 |
# New in 13.0.0 |
| 206 |
self.assertEqual(self.db.decimal('\U0001fbf9', None), None if self.old else 9) |
| 207 |
# New in 14.0.0 |
| 208 |
self.assertEqual(self.db.decimal('\U00016ac9', None), None if self.old else 9) |
| 209 |
# New in 15.0.0 |
| 210 |
self.assertEqual(self.db.decimal('\U0001e4f9', None), None if self.old else 9) |
| 211 |
|
| 212 |
self.assertRaises(TypeError, self.db.decimal) |
| 213 |
self.assertRaises(TypeError, self.db.decimal, 'xx') |
| 214 |
self.assertRaises(ValueError, self.db.decimal, 'x') |
| 215 |
|
| 216 |
def test_category(self): |
| 217 |
self.assertEqual(self.db.category('\uFFFE'), 'Cn') |
| 218 |
self.assertEqual(self.db.category('a'), 'Ll') |
| 219 |
self.assertEqual(self.db.category('A'), 'Lu') |
| 220 |
self.assertEqual(self.db.category('\U00020000'), 'Lo') |
| 221 |
|
| 222 |
# New in 4.1.0 |
| 223 |
self.assertEqual(self.db.category('\U0001012A'), 'Cn' if self.old else 'No') |
| 224 |
self.assertEqual(self.db.category('\U000e01ef'), 'Cn' if self.old else 'Mn') |
| 225 |
# New in 5.1.0 |
| 226 |
self.assertEqual(self.db.category('\u0374'), 'Sk' if self.old else 'Lm') |
| 227 |
# Changed in 13.0.0 |
| 228 |
self.assertEqual(self.db.category('\u0b55'), 'Cn' if self.old else 'Mn') |
| 229 |
self.assertEqual(self.db.category('\U0003134a'), 'Cn' if self.old else 'Lo') |
| 230 |
# Changed in 14.0.0 |
| 231 |
self.assertEqual(self.db.category('\u061d'), 'Cn' if self.old else 'Po') |
| 232 |
self.assertEqual(self.db.category('\U0002b738'), 'Cn' if self.old else 'Lo') |
| 233 |
# Changed in 15.0.0 |
| 234 |
self.assertEqual(self.db.category('\u0cf3'), 'Cn' if self.old else 'Mc') |
| 235 |
self.assertEqual(self.db.category('\U000323af'), 'Cn' if self.old else 'Lo') |
| 236 |
|
| 237 |
self.assertRaises(TypeError, self.db.category) |
| 238 |
self.assertRaises(TypeError, self.db.category, 'xx') |
| 239 |
|
| 240 |
def test_bidirectional(self): |
| 241 |
self.assertEqual(self.db.bidirectional('\uFFFE'), '') |
| 242 |
self.assertEqual(self.db.bidirectional(' '), 'WS') |
| 243 |
self.assertEqual(self.db.bidirectional('A'), 'L') |
| 244 |
self.assertEqual(self.db.bidirectional('\U00020000'), 'L') |
| 245 |
|
| 246 |
# New in 4.1.0 |
| 247 |
self.assertEqual(self.db.bidirectional('+'), 'ET' if self.old else 'ES') |
| 248 |
self.assertEqual(self.db.bidirectional('\u0221'), '' if self.old else 'L') |
| 249 |
self.assertEqual(self.db.bidirectional('\U000e01ef'), '' if self.old else 'NSM') |
| 250 |
# New in 13.0.0 |
| 251 |
self.assertEqual(self.db.bidirectional('\u0b55'), '' if self.old else 'NSM') |
| 252 |
self.assertEqual(self.db.bidirectional('\U0003134a'), '' if self.old else 'L') |
| 253 |
# New in 14.0.0 |
| 254 |
self.assertEqual(self.db.bidirectional('\u061d'), '' if self.old else 'AL') |
| 255 |
self.assertEqual(self.db.bidirectional('\U0002b738'), '' if self.old else 'L') |
| 256 |
# New in 15.0.0 |
| 257 |
self.assertEqual(self.db.bidirectional('\u0cf3'), '' if self.old else 'L') |
| 258 |
self.assertEqual(self.db.bidirectional('\U000323af'), '' if self.old else 'L') |
| 259 |
|
| 260 |
self.assertRaises(TypeError, self.db.bidirectional) |
| 261 |
self.assertRaises(TypeError, self.db.bidirectional, 'xx') |
| 262 |
|
| 263 |
def test_decomposition(self): |
| 264 |
self.assertEqual(self.db.decomposition('\uFFFE'),'') |
| 265 |
self.assertEqual(self.db.decomposition('\u00bc'), '<fraction> 0031 2044 0034') |
| 266 |
|
| 267 |
# New in 4.1.0 |
| 268 |
self.assertEqual(self.db.decomposition('\u03f9'), '' if self.old else '<compat> 03A3') |
| 269 |
# New in 13.0.0 |
| 270 |
self.assertEqual(self.db.decomposition('\uab69'), '' if self.old else '<super> 028D') |
| 271 |
self.assertEqual(self.db.decomposition('\U00011938'), '' if self.old else '11935 11930') |
| 272 |
self.assertEqual(self.db.decomposition('\U0001fbf9'), '' if self.old else '<font> 0039') |
| 273 |
# New in 14.0.0 |
| 274 |
self.assertEqual(self.db.decomposition('\ua7f2'), '' if self.old else '<super> 0043') |
| 275 |
self.assertEqual(self.db.decomposition('\U000107ba'), '' if self.old else '<super> 1DF1E') |
| 276 |
# New in 15.0.0 |
| 277 |
self.assertEqual(self.db.decomposition('\U0001e06d'), '' if self.old else '<super> 04B1') |
| 278 |
|
| 279 |
self.assertRaises(TypeError, self.db.decomposition) |
| 280 |
self.assertRaises(TypeError, self.db.decomposition, 'xx') |
| 281 |
|
| 282 |
def test_mirrored(self): |
| 283 |
self.assertEqual(self.db.mirrored('\uFFFE'), 0) |
| 284 |
self.assertEqual(self.db.mirrored('a'), 0) |
| 285 |
self.assertEqual(self.db.mirrored('\u2201'), 1) |
| 286 |
self.assertEqual(self.db.mirrored('\U00020000'), 0) |
| 287 |
|
| 288 |
# New in 5.0.0 |
| 289 |
self.assertEqual(self.db.mirrored('\u0f3a'), 0 if self.old else 1) |
| 290 |
self.assertEqual(self.db.mirrored('\U0001d7c3'), 0 if self.old else 1) |
| 291 |
# New in 11.0.0 |
| 292 |
self.assertEqual(self.db.mirrored('\u29a1'), 1 if self.old else 0) |
| 293 |
# New in 14.0.0 |
| 294 |
self.assertEqual(self.db.mirrored('\u2e5c'), 0 if self.old else 1) |
| 295 |
|
| 296 |
self.assertRaises(TypeError, self.db.mirrored) |
| 297 |
self.assertRaises(TypeError, self.db.mirrored, 'xx') |
| 298 |
|
| 299 |
def test_combining(self): |
| 300 |
self.assertEqual(self.db.combining('\uFFFE'), 0) |
| 301 |
self.assertEqual(self.db.combining('a'), 0) |
| 302 |
self.assertEqual(self.db.combining('\u20e1'), 230) |
| 303 |
self.assertEqual(self.db.combining('\U00020000'), 0) |
| 304 |
|
| 305 |
# New in 4.1.0 |
| 306 |
self.assertEqual(self.db.combining('\u0350'), 0 if self.old else 230) |
| 307 |
# New in 9.0.0 |
| 308 |
self.assertEqual(self.db.combining('\U0001e94a'), 0 if self.old else 7) |
| 309 |
# New in 13.0.0 |
| 310 |
self.assertEqual(self.db.combining('\u1abf'), 0 if self.old else 220) |
| 311 |
self.assertEqual(self.db.combining('\U00016ff1'), 0 if self.old else 6) |
| 312 |
# New in 14.0.0 |
| 313 |
self.assertEqual(self.db.combining('\u0c3c'), 0 if self.old else 7) |
| 314 |
self.assertEqual(self.db.combining('\U0001e2ae'), 0 if self.old else 230) |
| 315 |
# New in 15.0.0 |
| 316 |
self.assertEqual(self.db.combining('\U00010efd'), 0 if self.old else 220) |
| 317 |
|
| 318 |
self.assertRaises(TypeError, self.db.combining) |
| 319 |
self.assertRaises(TypeError, self.db.combining, 'xx') |
| 320 |
|
| 321 |
def test_normalization(self): |
| 322 |
# Test normalize() and is_normalized() |
| 323 |
def check(ch, expected): |
| 324 |
if isinstance(expected, str): |
| 325 |
expected = [expected]*4 |
| 326 |
forms = ('NFC', 'NFD', 'NFKC', 'NFKD') |
| 327 |
result = [self.db.normalize(form, ch) for form in forms] |
| 328 |
self.assertEqual(ascii(result), ascii(list(expected))) |
| 329 |
self.assertEqual([self.db.is_normalized(form, ch) for form in forms], |
| 330 |
[ch == y for x, y in zip(result, expected)]) |
| 331 |
|
| 332 |
check('', '') |
| 333 |
check('A', 'A') |
| 334 |
check(' ', ' ') |
| 335 |
check('\U0010ffff', '\U0010ffff') |
| 336 |
check('abc', 'abc') |
| 337 |
# Broken in 4.0.0 |
| 338 |
check('\u0340', '\u0300') |
| 339 |
check('\u0300', '\u0300') |
| 340 |
check('\U0002fa1d', '\U0002a600') |
| 341 |
check('\U0002a600', '\U0002a600') |
| 342 |
check('\u0344', '\u0308\u0301') |
| 343 |
check('\u0308\u0301', '\u0308\u0301') |
| 344 |
# Broken in 4.0.0 and 4.0.1 |
| 345 |
check('\U0001d1bc', '\U0001d1ba\U0001d165') |
| 346 |
check('\U0001d1ba\U0001d165', '\U0001d1ba\U0001d165') |
| 347 |
check('\ufb2c', '\u05e9\u05bc\u05c1') |
| 348 |
check('\u05e9\u05bc\u05c1', '\u05e9\u05bc\u05c1') |
| 349 |
check('\U0001d1c0', '\U0001d1ba\U0001d165\U0001d16f') |
| 350 |
check('\U0001d1ba\U0001d165\U0001d16f', '\U0001d1ba\U0001d165\U0001d16f') |
| 351 |
|
| 352 |
# Broken in 4.0.0 |
| 353 |
check('\xa0', ['\xa0', '\xa0', ' ', ' ']) |
| 354 |
check('\u2003', ['\u2003', '\u2003', ' ', ' ']) |
| 355 |
check('\U0001d7ff', ['\U0001d7ff', '\U0001d7ff', '9', '9']) |
| 356 |
|
| 357 |
check('\xa8', ['\xa8', '\xa8', ' \u0308', ' \u0308']) |
| 358 |
check(' \u0308', ' \u0308') |
| 359 |
|
| 360 |
check('\xc0', ['\xc0', 'A\u0300']*2) |
| 361 |
check('A\u0300', ['\xc0', 'A\u0300']*2) |
| 362 |
|
| 363 |
check('\ud7a3', ['\ud7a3', '\u1112\u1175\u11c2']*2) |
| 364 |
check('\u1112\u1175\u11c2', ['\ud7a3', '\u1112\u1175\u11c2']*2) |
| 365 |
|
| 366 |
check('\xb4', ['\xb4', '\xb4', ' \u0301', ' \u0301']) |
| 367 |
check('\u1ffd', ['\xb4', '\xb4', ' \u0301', ' \u0301']) |
| 368 |
check(' \u0301', ' \u0301') |
| 369 |
|
| 370 |
check('\xc5', ['\xc5', 'A\u030a']*2) |
| 371 |
check('\u212b', ['\xc5', 'A\u030a']*2) |
| 372 |
check('A\u030a', ['\xc5', 'A\u030a']*2) |
| 373 |
|
| 374 |
check('\u1f71', ['\u03ac', '\u03b1\u0301']*2) |
| 375 |
check('\u03ac', ['\u03ac', '\u03b1\u0301']*2) |
| 376 |
check('\u03b1\u0301', ['\u03ac', '\u03b1\u0301']*2) |
| 377 |
|
| 378 |
check('\u01c4', ['\u01c4', '\u01c4', 'D\u017d', 'DZ\u030c']) |
| 379 |
check('D\u017d', ['D\u017d', 'DZ\u030c']*2) |
| 380 |
check('DZ\u030c', ['D\u017d', 'DZ\u030c']*2) |
| 381 |
|
| 382 |
check('\u1fed', ['\u1fed', '\xa8\u0300', ' \u0308\u0300', ' \u0308\u0300']) |
| 383 |
check('\xa8\u0300', ['\u1fed', '\xa8\u0300', ' \u0308\u0300', ' \u0308\u0300']) |
| 384 |
check(' \u0308\u0300', ' \u0308\u0300') |
| 385 |
|
| 386 |
check('\u326e', ['\u326e', '\u326e', '\uac00', '\u1100\u1161']) |
| 387 |
check('\u320e', ['\u320e', '\u320e', '(\uac00)', '(\u1100\u1161)']) |
| 388 |
check('(\uac00)', ['(\uac00)', '(\u1100\u1161)']*2) |
| 389 |
check('(\u1100\u1161)', ['(\uac00)', '(\u1100\u1161)']*2) |
| 390 |
|
| 391 |
check('\u0385', ['\u0385', '\xa8\u0301', ' \u0308\u0301', ' \u0308\u0301']) |
| 392 |
check('\u1fee', ['\u0385', '\xa8\u0301', ' \u0308\u0301', ' \u0308\u0301']) |
| 393 |
check('\xa8\u0301', ['\u0385', '\xa8\u0301', ' \u0308\u0301', ' \u0308\u0301']) |
| 394 |
check(' \u0308\u0301', ' \u0308\u0301') |
| 395 |
|
| 396 |
check('\u1fdf', ['\u1fdf', '\u1ffe\u0342', ' \u0314\u0342', ' \u0314\u0342']) |
| 397 |
check('\u1ffe\u0342', ['\u1fdf', '\u1ffe\u0342', ' \u0314\u0342', ' \u0314\u0342']) |
| 398 |
check('\u1ffe', ['\u1ffe', '\u1ffe', ' \u0314', ' \u0314']) |
| 399 |
check(' \u0314\u0342', ' \u0314\u0342') |
| 400 |
|
| 401 |
check('\u03d3', ['\u03d3', '\u03d2\u0301', '\u038e', '\u03a5\u0301']) |
| 402 |
check('\u03d2\u0301', ['\u03d3', '\u03d2\u0301', '\u038e', '\u03a5\u0301']) |
| 403 |
check('\u038e', ['\u038e', '\u03a5\u0301']*2) |
| 404 |
check('\u1feb', ['\u038e', '\u03a5\u0301']*2) |
| 405 |
check('\u03a5\u0301', ['\u038e', '\u03a5\u0301']*2) |
| 406 |
|
| 407 |
check('\u0626', ['\u0626', '\u064a\u0654']*2) |
| 408 |
check('\u064a\u0654', ['\u0626', '\u064a\u0654']*2) |
| 409 |
check('\ufe89', ['\ufe89', '\ufe89', '\u0626', '\u064a\u0654']) |
| 410 |
check('\ufe8a', ['\ufe8a', '\ufe8a', '\u0626', '\u064a\u0654']) |
| 411 |
check('\ufe8b', ['\ufe8b', '\ufe8b', '\u0626', '\u064a\u0654']) |
| 412 |
check('\ufe8c', ['\ufe8c', '\ufe8c', '\u0626', '\u064a\u0654']) |
| 413 |
|
| 414 |
check('\ufef9', ['\ufef9', '\ufef9', '\u0644\u0625', '\u0644\u0627\u0655']) |
| 415 |
check('\ufefa', ['\ufefa', '\ufefa', '\u0644\u0625', '\u0644\u0627\u0655']) |
| 416 |
check('\ufefb', ['\ufefb', '\ufefb', '\u0644\u0627', '\u0644\u0627']) |
| 417 |
check('\ufefc', ['\ufefc', '\ufefc', '\u0644\u0627', '\u0644\u0627']) |
| 418 |
check('\u0644\u0625', ['\u0644\u0625', '\u0644\u0627\u0655']*2) |
| 419 |
check('\u0644\u0627\u0655', ['\u0644\u0625', '\u0644\u0627\u0655']*2) |
| 420 |
check('\u0644\u0627', '\u0644\u0627') |
| 421 |
|
| 422 |
# Broken in 4.0.0 |
| 423 |
check('\u327c', '\u327c' if self.old else |
| 424 |
['\u327c', '\u327c', '\ucc38\uace0', '\u110e\u1161\u11b7\u1100\u1169']) |
| 425 |
check('\ucc38\uace0', ['\ucc38\uace0', '\u110e\u1161\u11b7\u1100\u1169']*2) |
| 426 |
check('\ucc38', ['\ucc38', '\u110e\u1161\u11b7']*2) |
| 427 |
check('\u110e\u1161\u11b7\u1100\u1169', |
| 428 |
['\ucc38\uace0', '\u110e\u1161\u11b7\u1100\u1169']*2) |
| 429 |
check('\u110e\u1161\u11b7\u1100', |
| 430 |
['\ucc38\u1100', '\u110e\u1161\u11b7\u1100']*2) |
| 431 |
check('\u110e\u1161\u11b7', |
| 432 |
['\ucc38', '\u110e\u1161\u11b7']*2) |
| 433 |
check('\u110e\u1161', |
| 434 |
['\ucc28', '\u110e\u1161']*2) |
| 435 |
check('\u110e', '\u110e') |
| 436 |
# Broken in 4.0.0-12.0.0 |
| 437 |
check('\U00011938', '\U00011938' if self.old else |
| 438 |
['\U00011938', '\U00011935\U00011930']*2) |
| 439 |
check('\U00011935\U00011930', ['\U00011938', '\U00011935\U00011930']*2) |
| 440 |
# New in 4.0.1 |
| 441 |
check('\u321d', '\u321d' if self.old else |
| 442 |
['\u321d', '\u321d', '(\uc624\uc804)', '(\u110b\u1169\u110c\u1165\u11ab)']) |
| 443 |
check('(\uc624\uc804)', |
| 444 |
['(\uc624\uc804)', '(\u110b\u1169\u110c\u1165\u11ab)']*2) |
| 445 |
check('(\u110b\u1169\u110c\u1165\u11ab)', |
| 446 |
['(\uc624\uc804)', '(\u110b\u1169\u110c\u1165\u11ab)']*2) |
| 447 |
check('\u4d57', '\u4d57') |
| 448 |
check('\u45d7', '\u45d7' if self.old else '\u45d7') |
| 449 |
check('\U0002f9bf', '\u4d57' if self.old else '\u45d7') |
| 450 |
# New in 4.1.0 |
| 451 |
check('\u03a3', '\u03a3') |
| 452 |
check('\u03f9', '\u03f9' if self.old else |
| 453 |
['\u03f9', '\u03f9', '\u03a3', '\u03a3']) |
| 454 |
# New in 5.0.0 |
| 455 |
check('\u1b06', '\u1b06' if self.old else ['\u1b06', '\u1b05\u1b35']*2) |
| 456 |
# New in 5.2.0 |
| 457 |
check('\U0001f213', '\U0001f213' if self.old else |
| 458 |
['\U0001f213', '\U0001f213', '\u30c7', '\u30c6\u3099']) |
| 459 |
# New in 6.1.0 |
| 460 |
check('\ufa2e', '\ufa2e' if self.old else '\u90de') |
| 461 |
# New in 13.0.0 |
| 462 |
check('\U00011938', '\U00011938' if self.old else |
| 463 |
['\U00011938', '\U00011935\U00011930', '\U00011938', '\U00011935\U00011930']) |
| 464 |
check('\U0001fbf9', '\U0001fbf9' if self.old else |
| 465 |
['\U0001fbf9', '\U0001fbf9', '9', '9']) |
| 466 |
# New in 14.0.0 |
| 467 |
check('\U000107ba', '\U000107ba' if self.old else |
| 468 |
['\U000107ba', '\U000107ba', '\U0001df1e', '\U0001df1e']) |
| 469 |
# New in 15.0.0 |
| 470 |
check('\U0001e06d', '\U0001e06d' if self.old else |
| 471 |
['\U0001e06d', '\U0001e06d', '\u04b1', '\u04b1']) |
| 472 |
|
| 473 |
self.assertRaises(TypeError, self.db.normalize) |
| 474 |
self.assertRaises(TypeError, self.db.normalize, 'NFC') |
| 475 |
self.assertRaises(ValueError, self.db.normalize, 'SPAM', 'A') |
| 476 |
|
| 477 |
self.assertRaises(TypeError, self.db.is_normalized) |
| 478 |
self.assertRaises(TypeError, self.db.is_normalized, 'NFC') |
| 479 |
self.assertRaises(ValueError, self.db.is_normalized, 'SPAM', 'A') |
| 480 |
|
| 481 |
def test_pr29(self): |
| 482 |
# https://www.unicode.org/review/pr-29.html |
| 483 |
# See issues #1054943 and #10254. |
| 484 |
composed = ("\u0b47\u0300\u0b3e", "\u1100\u0300\u1161", |
| 485 |
'Li\u030dt-s\u1e73\u0301', |
| 486 |
'\u092e\u093e\u0930\u094d\u0915 \u091c\u093c' |
| 487 |
+ '\u0941\u0915\u0947\u0930\u092c\u0930\u094d\u0917', |
| 488 |
'\u0915\u093f\u0930\u094d\u0917\u093f\u091c\u093c' |
| 489 |
+ '\u0938\u094d\u0924\u093e\u0928') |
| 490 |
for text in composed: |
| 491 |
self.assertEqual(self.db.normalize('NFC', text), text) |
| 492 |
|
| 493 |
def test_issue10254(self): |
| 494 |
# Crash reported in #10254 |
| 495 |
# New in 4.1.0 |
| 496 |
a = 'C\u0338' * 20 + 'C\u0327' |
| 497 |
b = 'C\u0338' * 20 + '\xC7' |
| 498 |
self.assertEqual(self.db.normalize('NFC', a), b) |
| 499 |
|
| 500 |
def test_issue29456(self): |
| 501 |
# Fix #29456 |
| 502 |
u1176_str_a = '\u1100\u1176\u11a8' |
| 503 |
u1176_str_b = '\u1100\u1176\u11a8' |
| 504 |
u11a7_str_a = '\u1100\u1175\u11a7' |
| 505 |
u11a7_str_b = '\uae30\u11a7' |
| 506 |
u11c3_str_a = '\u1100\u1175\u11c3' |
| 507 |
u11c3_str_b = '\uae30\u11c3' |
| 508 |
self.assertEqual(self.db.normalize('NFC', u1176_str_a), u1176_str_b) |
| 509 |
# New in 4.1.0 |
| 510 |
self.assertEqual(self.db.normalize('NFC', u11a7_str_a), u11a7_str_b) |
| 511 |
self.assertEqual(self.db.normalize('NFC', u11c3_str_a), u11c3_str_b) |
| 512 |
|
| 513 |
def test_east_asian_width(self): |
| 514 |
eaw = self.db.east_asian_width |
| 515 |
self.assertRaises(TypeError, eaw, b'a') |
| 516 |
self.assertRaises(TypeError, eaw, bytearray()) |
| 517 |
self.assertRaises(TypeError, eaw, '') |
| 518 |
self.assertRaises(TypeError, eaw, 'ra') |
| 519 |
self.assertEqual(eaw('\x1e'), 'N') |
| 520 |
self.assertEqual(eaw('\x20'), 'Na') |
| 521 |
self.assertEqual(eaw('\uC894'), 'W') |
| 522 |
self.assertEqual(eaw('\uFF66'), 'H') |
| 523 |
self.assertEqual(eaw('\uFF1F'), 'F') |
| 524 |
self.assertEqual(eaw('\u2010'), 'A') |
| 525 |
self.assertEqual(eaw('\U00020000'), 'W') |
| 526 |
|
| 527 |
# New in 4.1.0 |
| 528 |
self.assertEqual(eaw('\u0350'), 'N' if self.old else 'A') |
| 529 |
self.assertEqual(eaw('\U000e01ef'), 'N' if self.old else 'A') |
| 530 |
# New in 5.2.0 |
| 531 |
self.assertEqual(eaw('\u115a'), 'N' if self.old else 'W') |
| 532 |
# New in 9.0.0 |
| 533 |
self.assertEqual(eaw('\u231a'), 'N' if self.old else 'W') |
| 534 |
self.assertEqual(eaw('\u2614'), 'N' if self.old else 'W') |
| 535 |
self.assertEqual(eaw('\U0001f19a'), 'N' if self.old else 'W') |
| 536 |
self.assertEqual(eaw('\U0001f991'), 'N' if self.old else 'W') |
| 537 |
self.assertEqual(eaw('\U0001f9c0'), 'N' if self.old else 'W') |
| 538 |
# New in 12.0.0 |
| 539 |
self.assertEqual(eaw('\u32ff'), 'N' if self.old else 'W') |
| 540 |
self.assertEqual(eaw('\U0001fa95'), 'N' if self.old else 'W') |
| 541 |
# New in 13.0.0 |
| 542 |
self.assertEqual(eaw('\u31bb'), 'N' if self.old else 'W') |
| 543 |
self.assertEqual(eaw('\U0003134a'), 'N' if self.old else 'W') |
| 544 |
# New in 14.0.0 |
| 545 |
self.assertEqual(eaw('\u9ffd'), 'N' if self.old else 'W') |
| 546 |
self.assertEqual(eaw('\U0002b738'), 'N' if self.old else 'W') |
| 547 |
# New in 15.0.0 |
| 548 |
self.assertEqual(eaw('\U000323af'), 'N' if self.old else 'W') |
| 549 |
|
| 550 |
def test_east_asian_width_unassigned(self): |
| 551 |
eaw = self.db.east_asian_width |
| 552 |
# unassigned |
| 553 |
for char in '\u0530\u0ecf\u10c6\u20fc\uaaca\U000107bd\U000115f2': |
| 554 |
self.assertEqual(eaw(char), 'N') |
| 555 |
self.assertIs(self.db.name(char, None), None) |
| 556 |
|
| 557 |
# unassigned but reserved for CJK |
| 558 |
for char in ('\U0002A6E0\U0002FA20\U0003134B\U0003FFFD' |
| 559 |
'\uFA6E\uFADA'): # New in 5.2.0 |
| 560 |
self.assertEqual(eaw(char), 'W') |
| 561 |
self.assertIs(self.db.name(char, None), None) |
| 562 |
|
| 563 |
# private use areas |
| 564 |
for char in '\uE000\uF800\U000F0000\U000FFFEE\U00100000\U0010FFF0': |
| 565 |
self.assertEqual(eaw(char), 'A') |
| 566 |
self.assertIs(self.db.name(char, None), None) |
| 567 |
|
| 568 |
|
| 569 |
class Unicode_3_2_0_FunctionsTest(UnicodeFunctionsTest): |
| 570 |
db = unicodedata.ucd_3_2_0 |
| 571 |
old = True |
| 572 |
expectedchecksum = ('76b126d719d52ba11788a627d058163106da7d56' |
| 573 |
if quicktest else |
| 574 |
'871389bdd96a709929496da7f9e59718daf61adb') |
| 575 |
|
| 576 |
|
| 577 |
class UnicodeMiscTest(unittest.TestCase): |
| 578 |
db = unicodedata |
| 579 |
|
| 580 |
@cpython_only |
| 581 |
def test_disallow_instantiation(self): |
| 582 |
# Ensure that the type disallows instantiation (bpo-43916) |
| 583 |
check_disallow_instantiation(self, unicodedata.UCD) |
| 584 |
|
| 585 |
@force_not_colorized |
| 586 |
def test_failed_import_during_compiling(self): |
| 587 |
# Issue 4367 |
| 588 |
# Decoding \N escapes requires the unicodedata module. If it can't be |
| 589 |
# imported, we shouldn't segfault. |
| 590 |
|
| 591 |
# This program should raise a SyntaxError in the eval. |
| 592 |
code = "import sys;" \ |
| 593 |
"sys.modules['unicodedata'] = None;" \ |
| 594 |
"""eval("'\\\\N{SOFT HYPHEN}'")""" |
| 595 |
# We use a separate process because the unicodedata module may already |
| 596 |
# have been loaded in this process. |
| 597 |
result = script_helper.assert_python_failure("-c", code) |
| 598 |
error = "SyntaxError: (unicode error) \\N escapes not supported " \ |
| 599 |
"(can't load unicodedata module)" |
| 600 |
self.assertIn(error, result.err.decode("ascii")) |
| 601 |
|
| 602 |
def test_decimal_numeric_consistent(self): |
| 603 |
# Test that decimal and numeric are consistent, |
| 604 |
# i.e. if a character has a decimal value, |
| 605 |
# its numeric value should be the same. |
| 606 |
count = 0 |
| 607 |
for c in iterallchars(): |
| 608 |
dec = self.db.decimal(c, -1) |
| 609 |
if dec != -1: |
| 610 |
self.assertEqual(dec, self.db.numeric(c)) |
| 611 |
count += 1 |
| 612 |
self.assertTrue(count >= 10, count) # should have tested at least the ASCII digits |
| 613 |
|
| 614 |
def test_digit_numeric_consistent(self): |
| 615 |
# Test that digit and numeric are consistent, |
| 616 |
# i.e. if a character has a digit value, |
| 617 |
# its numeric value should be the same. |
| 618 |
count = 0 |
| 619 |
for c in iterallchars(): |
| 620 |
dec = self.db.digit(c, -1) |
| 621 |
if dec != -1: |
| 622 |
self.assertEqual(dec, self.db.numeric(c)) |
| 623 |
count += 1 |
| 624 |
self.assertTrue(count >= 10, count) # should have tested at least the ASCII digits |
| 625 |
|
| 626 |
def test_normalize_consistent(self): |
| 627 |
allchars = list(iterallchars()) |
| 628 |
for form in ('NFC', 'NFD', 'NFKC', 'NFKD'): |
| 629 |
for c in allchars: |
| 630 |
norm = self.db.normalize(form, c) |
| 631 |
self.assertEqual(self.db.is_normalized(form, c), norm == c) |
| 632 |
if norm != c: |
| 633 |
self.assertEqual(self.db.normalize(form, norm), norm) |
| 634 |
self.assertTrue(self.db.is_normalized(form, norm)) |
| 635 |
|
| 636 |
def test_bug_1704793(self): |
| 637 |
self.assertEqual(self.db.lookup("GOTHIC LETTER FAIHU"), '\U00010346') |
| 638 |
|
| 639 |
def test_ucd_510(self): |
| 640 |
# In UCD 5.1.0, a mirrored property changed wrt. UCD 3.2.0 |
| 641 |
self.assertTrue(unicodedata.mirrored("\u0f3a")) |
| 642 |
self.assertTrue(not unicodedata.ucd_3_2_0.mirrored("\u0f3a")) |
| 643 |
# Also, we now have two ways of representing |
| 644 |
# the upper-case mapping: as delta, or as absolute value |
| 645 |
self.assertTrue("a".upper()=='A') |
| 646 |
self.assertTrue("\u1d79".upper()=='\ua77d') |
| 647 |
self.assertTrue(".".upper()=='.') |
| 648 |
|
| 649 |
@requires_resource('cpu') |
| 650 |
def test_bug_5828(self): |
| 651 |
self.assertEqual("\u1d79".lower(), "\u1d79") |
| 652 |
# Only U+0000 should have U+0000 as its upper/lower/titlecase variant |
| 653 |
self.assertEqual( |
| 654 |
[ |
| 655 |
c for c in iterallchars() |
| 656 |
if "\x00" in (c.lower(), c.upper(), c.title()) |
| 657 |
], |
| 658 |
["\x00"] |
| 659 |
) |
| 660 |
|
| 661 |
def test_bug_4971(self): |
| 662 |
# LETTER DZ WITH CARON: DZ, Dz, dz |
| 663 |
self.assertEqual("\u01c4".title(), "\u01c5") |
| 664 |
self.assertEqual("\u01c5".title(), "\u01c5") |
| 665 |
self.assertEqual("\u01c6".title(), "\u01c5") |
| 666 |
|
| 667 |
def test_linebreak_7643(self): |
| 668 |
for c in iterallchars(): |
| 669 |
lines = (c + 'A').splitlines() |
| 670 |
if c in ('\x0a', '\x0b', '\x0c', '\x0d', '\x85', |
| 671 |
'\x1c', '\x1d', '\x1e', '\u2028', '\u2029'): |
| 672 |
self.assertEqual(len(lines), 2, |
| 673 |
r"%a should be a linebreak" % c) |
| 674 |
else: |
| 675 |
self.assertEqual(len(lines), 1, |
| 676 |
r"%a should not be a linebreak" % c) |
| 677 |
|
| 678 |
|
| 679 |
class NormalizationTest(unittest.TestCase): |
| 680 |
@staticmethod |
| 681 |
def check_version(testfile): |
| 682 |
hdr = testfile.readline() |
| 683 |
return unicodedata.unidata_version in hdr |
| 684 |
|
| 685 |
@staticmethod |
| 686 |
def unistr(data): |
| 687 |
data = [int(x, 16) for x in data.split(" ")] |
| 688 |
return "".join([chr(x) for x in data]) |
| 689 |
|
| 690 |
@requires_resource('network') |
| 691 |
@requires_resource('cpu') |
| 692 |
def test_normalization(self): |
| 693 |
TESTDATAFILE = "NormalizationTest.txt" |
| 694 |
TESTDATAURL = f"http://www.pythontest.net/unicode/{unicodedata.unidata_version}/{TESTDATAFILE}" |
| 695 |
|
| 696 |
# Hit the exception early |
| 697 |
try: |
| 698 |
testdata = open_urlresource(TESTDATAURL, encoding="utf-8", |
| 699 |
check=self.check_version) |
| 700 |
except PermissionError: |
| 701 |
self.skipTest(f"Permission error when downloading {TESTDATAURL} " |
| 702 |
f"into the test data directory") |
| 703 |
except (OSError, HTTPException) as exc: |
| 704 |
self.skipTest(f"Failed to download {TESTDATAURL}: {exc}") |
| 705 |
|
| 706 |
with testdata: |
| 707 |
self.run_normalization_tests(testdata, unicodedata) |
| 708 |
|
| 709 |
@requires_resource('cpu') |
| 710 |
def test_normalization_3_2_0(self): |
| 711 |
testdatafile = findfile('NormalizationTest-3.2.0.txt') |
| 712 |
with open(testdatafile, encoding='utf-8') as testdata: |
| 713 |
self.run_normalization_tests(testdata, unicodedata.ucd_3_2_0) |
| 714 |
|
| 715 |
def run_normalization_tests(self, testdata, ucd): |
| 716 |
part = None |
| 717 |
part1_data = set() |
| 718 |
|
| 719 |
NFC = partial(ucd.normalize, "NFC") |
| 720 |
NFKC = partial(ucd.normalize, "NFKC") |
| 721 |
NFD = partial(ucd.normalize, "NFD") |
| 722 |
NFKD = partial(ucd.normalize, "NFKD") |
| 723 |
is_normalized = ucd.is_normalized |
| 724 |
|
| 725 |
for line in testdata: |
| 726 |
if '#' in line: |
| 727 |
line = line.split('#')[0] |
| 728 |
line = line.strip() |
| 729 |
if not line: |
| 730 |
continue |
| 731 |
if line.startswith("@Part"): |
| 732 |
part = line.split()[0] |
| 733 |
continue |
| 734 |
c1,c2,c3,c4,c5 = [self.unistr(x) for x in line.split(';')[:-1]] |
| 735 |
|
| 736 |
# Perform tests |
| 737 |
self.assertTrue(c2 == NFC(c1) == NFC(c2) == NFC(c3), line) |
| 738 |
self.assertTrue(c4 == NFC(c4) == NFC(c5), line) |
| 739 |
self.assertTrue(c3 == NFD(c1) == NFD(c2) == NFD(c3), line) |
| 740 |
self.assertTrue(c5 == NFD(c4) == NFD(c5), line) |
| 741 |
self.assertTrue(c4 == NFKC(c1) == NFKC(c2) == \ |
| 742 |
NFKC(c3) == NFKC(c4) == NFKC(c5), |
| 743 |
line) |
| 744 |
self.assertTrue(c5 == NFKD(c1) == NFKD(c2) == \ |
| 745 |
NFKD(c3) == NFKD(c4) == NFKD(c5), |
| 746 |
line) |
| 747 |
|
| 748 |
self.assertTrue(is_normalized("NFC", c2)) |
| 749 |
self.assertTrue(is_normalized("NFC", c4)) |
| 750 |
|
| 751 |
self.assertTrue(is_normalized("NFD", c3)) |
| 752 |
self.assertTrue(is_normalized("NFD", c5)) |
| 753 |
|
| 754 |
self.assertTrue(is_normalized("NFKC", c4)) |
| 755 |
self.assertTrue(is_normalized("NFKD", c5)) |
| 756 |
|
| 757 |
# Record part 1 data |
| 758 |
if part == "@Part1": |
| 759 |
part1_data.add(c1) |
| 760 |
|
| 761 |
# Perform tests for all other data |
| 762 |
for X in iterallchars(): |
| 763 |
if X in part1_data: |
| 764 |
continue |
| 765 |
self.assertTrue(X == NFC(X) == NFD(X) == NFKC(X) == NFKD(X), ord(X)) |
| 766 |
|
| 767 |
def test_edge_cases(self): |
| 768 |
self.assertRaises(TypeError, unicodedata.normalize) |
| 769 |
self.assertRaises(ValueError, unicodedata.normalize, 'unknown', 'xx') |
| 770 |
self.assertEqual(unicodedata.normalize('NFKC', ''), '') |
| 771 |
|
| 772 |
def test_bug_834676(self): |
| 773 |
# Check for bug 834676 |
| 774 |
unicodedata.normalize('NFC', '\ud55c\uae00') |
| 775 |
|
| 776 |
|
| 777 |
if __name__ == "__main__": |
| 778 |
unittest.main() |