| 1 |
|
| 2 |
|
| 3 |
import py |
| 4 |
import sys, random |
| 5 |
import struct |
| 6 |
from rpython.rlib import runicode |
| 7 |
|
| 8 |
from hypothesis import given, settings, strategies |
| 9 |
|
| 10 |
|
| 11 |
def test_unichr(): |
| 12 |
assert runicode.UNICHR(0xffff) == u'\uffff' |
| 13 |
if runicode.MAXUNICODE > 0xffff: |
| 14 |
if sys.maxunicode < 0x10000: |
| 15 |
assert runicode.UNICHR(0x10000) == u'\ud800\udc00' |
| 16 |
else: |
| 17 |
assert runicode.UNICHR(0x10000) == u'\U00010000' |
| 18 |
else: |
| 19 |
py.test.raises(ValueError, runicode.UNICHR, 0x10000) |
| 20 |
py.test.raises(TypeError, runicode.UNICHR, 'abc') |
| 21 |
|
| 22 |
|
| 23 |
def test_ord(): |
| 24 |
assert runicode.ORD('a') == 97 |
| 25 |
assert runicode.ORD(u'a') == 97 |
| 26 |
assert runicode.ORD(u'\uffff') == 0xffff |
| 27 |
if runicode.MAXUNICODE > 0xffff: |
| 28 |
if sys.maxunicode < 0x10000: |
| 29 |
assert runicode.ORD(u'\ud800\udc00') == 0x10000 |
| 30 |
else: |
| 31 |
assert runicode.ORD(u'\U00010000') == 0x10000 |
| 32 |
else: |
| 33 |
py.test.raises(TypeError, runicode.ORD, u'\ud800\udc00') |
| 34 |
py.test.raises(TypeError, runicode.ORD, 'abc') |
| 35 |
|
| 36 |
|
| 37 |
class UnicodeTests(object): |
| 38 |
def typeequals(self, x, y): |
| 39 |
assert x == y |
| 40 |
assert type(x) is type(y) |
| 41 |
|
| 42 |
def getdecoder(self, encoding): |
| 43 |
return getattr(runicode, "str_decode_%s" % |
| 44 |
encoding.replace("-", "_")) |
| 45 |
|
| 46 |
def getencoder(self, encoding): |
| 47 |
return getattr(runicode, |
| 48 |
"unicode_encode_%s" % encoding.replace("-", "_")) |
| 49 |
|
| 50 |
def checkdecode(self, s, encoding): |
| 51 |
decoder = self.getdecoder(encoding) |
| 52 |
try: |
| 53 |
if isinstance(s, str): |
| 54 |
trueresult = s.decode(encoding) |
| 55 |
else: |
| 56 |
trueresult = s |
| 57 |
s = s.encode(encoding) |
| 58 |
except LookupError as e: |
| 59 |
py.test.skip(e) |
| 60 |
result, consumed = decoder(s, len(s), 'strict', final=True) |
| 61 |
assert consumed == len(s) |
| 62 |
self.typeequals(trueresult, result) |
| 63 |
|
| 64 |
def checkencode(self, s, encoding): |
| 65 |
encoder = self.getencoder(encoding) |
| 66 |
try: |
| 67 |
if isinstance(s, unicode): |
| 68 |
trueresult = s.encode(encoding) |
| 69 |
else: |
| 70 |
trueresult = s |
| 71 |
s = s.decode(encoding) |
| 72 |
except LookupError as e: |
| 73 |
py.test.skip(e) |
| 74 |
result = encoder(s, len(s), 'strict') |
| 75 |
self.typeequals(trueresult, result) |
| 76 |
|
| 77 |
def checkencodeerror(self, s, encoding, start, stop): |
| 78 |
called = [False] |
| 79 |
def errorhandler(errors, enc, msg, t, startingpos, |
| 80 |
endingpos): |
| 81 |
called[0] = True |
| 82 |
assert errors == "foo!" |
| 83 |
assert enc == encoding |
| 84 |
assert t is s |
| 85 |
assert start == startingpos |
| 86 |
assert stop == endingpos |
| 87 |
return u"42424242", None, stop |
| 88 |
encoder = self.getencoder(encoding) |
| 89 |
result = encoder(s, len(s), "foo!", errorhandler) |
| 90 |
assert called[0] |
| 91 |
assert "42424242" in result |
| 92 |
|
| 93 |
|
| 94 |
def errorhandler_bytes(errors, enc, msg, t, startingpos, |
| 95 |
endingpos): |
| 96 |
return None, '\xc3', endingpos |
| 97 |
result = encoder(s, len(s), "foo!", errorhandler_bytes) |
| 98 |
assert '\xc3' in result |
| 99 |
|
| 100 |
def checkdecodeerror(self, s, encoding, start, stop, |
| 101 |
addstuff=True, msg=None, |
| 102 |
expected_reported_encoding=None): |
| 103 |
called = [0] |
| 104 |
def errorhandler(errors, enc, errmsg, t, startingpos, |
| 105 |
endingpos): |
| 106 |
called[0] += 1 |
| 107 |
if called[0] == 1: |
| 108 |
assert errors == "foo!" |
| 109 |
assert enc == (expected_reported_encoding or |
| 110 |
encoding.replace('-', '')) |
| 111 |
assert t is s |
| 112 |
assert start == startingpos |
| 113 |
assert stop == endingpos |
| 114 |
if msg is not None: |
| 115 |
assert errmsg == msg |
| 116 |
return u"42424242", stop |
| 117 |
return u"", endingpos |
| 118 |
decoder = self.getdecoder(encoding) |
| 119 |
if addstuff: |
| 120 |
s += "some rest in ascii" |
| 121 |
result, _ = decoder(s, len(s), "foo!", True, errorhandler) |
| 122 |
assert called[0] > 0 |
| 123 |
assert "42424242" in result |
| 124 |
if addstuff: |
| 125 |
assert result.endswith(u"some rest in ascii") |
| 126 |
|
| 127 |
def test_charmap_encodeerror(self): |
| 128 |
def errorhandler(errors, enc, msg, t, startingpos, |
| 129 |
endingpos): |
| 130 |
assert t[startingpos:endingpos] == u'\t\n \r' |
| 131 |
return None, ' ', endingpos |
| 132 |
s = u'aa\t\n \raa' |
| 133 |
mapping = {u'a': 'a'} |
| 134 |
r = runicode.unicode_encode_charmap(s, len(s), None, errorhandler, |
| 135 |
mapping=mapping) |
| 136 |
assert r == 'aa aa' |
| 137 |
|
| 138 |
|
| 139 |
class TestDecoding(UnicodeTests): |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
def test_all_ascii(self): |
| 144 |
for i in range(128): |
| 145 |
for encoding in "utf-8 latin-1 ascii".split(): |
| 146 |
self.checkdecode(chr(i), encoding) |
| 147 |
|
| 148 |
def test_fast_str_decode_ascii(self): |
| 149 |
u = runicode.fast_str_decode_ascii("abc\x00\x7F") |
| 150 |
assert type(u) is unicode |
| 151 |
assert u == u"abc\x00\x7F" |
| 152 |
py.test.raises(ValueError, runicode.fast_str_decode_ascii, "ab\x80") |
| 153 |
|
| 154 |
def test_all_first_256(self): |
| 155 |
for i in range(256): |
| 156 |
for encoding in ("utf-7 utf-8 latin-1 utf-16 utf-16-be utf-16-le " |
| 157 |
"utf-32 utf-32-be utf-32-le").split(): |
| 158 |
self.checkdecode(unichr(i), encoding) |
| 159 |
|
| 160 |
def test_first_10000(self): |
| 161 |
for i in range(10000): |
| 162 |
for encoding in ("utf-7 utf-8 utf-16 utf-16-be utf-16-le " |
| 163 |
"utf-32 utf-32-be utf-32-le").split(): |
| 164 |
if encoding == 'utf-8' and 0xd800 <= i <= 0xdfff: |
| 165 |
|
| 166 |
continue |
| 167 |
self.checkdecode(unichr(i), encoding) |
| 168 |
|
| 169 |
def test_random(self): |
| 170 |
for i in range(10000): |
| 171 |
v = random.randrange(sys.maxunicode) |
| 172 |
if 0xd800 <= v <= 0xdfff: |
| 173 |
continue |
| 174 |
uni = unichr(v) |
| 175 |
if sys.version >= "2.7": |
| 176 |
self.checkdecode(uni, "utf-7") |
| 177 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 178 |
"utf-32 utf-32-be utf-32-le").split(): |
| 179 |
self.checkdecode(uni, encoding) |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
@settings(max_examples=10000) |
| 184 |
@given(strategies.characters(blacklist_categories=["Cs"])) |
| 185 |
def test_random_hypothesis(self, uni): |
| 186 |
if sys.version >= "2.7": |
| 187 |
self.checkdecode(uni, "utf-7") |
| 188 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 189 |
"utf-32 utf-32-be utf-32-le").split(): |
| 190 |
self.checkdecode(uni, encoding) |
| 191 |
|
| 192 |
def test_maxunicode(self): |
| 193 |
uni = unichr(sys.maxunicode) |
| 194 |
if sys.version >= "2.7": |
| 195 |
self.checkdecode(uni, "utf-7") |
| 196 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 197 |
"utf-32 utf-32-be utf-32-le").split(): |
| 198 |
self.checkdecode(uni, encoding) |
| 199 |
|
| 200 |
def test_ascii_error(self): |
| 201 |
self.checkdecodeerror("abc\xFF\xFF\xFFcde", "ascii", 3, 4) |
| 202 |
|
| 203 |
def test_decode_replace(self): |
| 204 |
decoder = self.getdecoder('utf-8') |
| 205 |
assert decoder('caf\xe9', 4, 'replace', True) == (u'caf\ufffd', 4) |
| 206 |
|
| 207 |
def test_utf16_errors(self): |
| 208 |
|
| 209 |
for s in ["\xff", "\xfe"]: |
| 210 |
self.checkdecodeerror(s, "utf-16", 0, len(s), addstuff=False) |
| 211 |
|
| 212 |
for s in [ |
| 213 |
|
| 214 |
"\xff\xfeF", |
| 215 |
|
| 216 |
'\xff\xfe\xc0\xdb\x00', '\xff\xfe\xc0\xdb', '\xff\xfe\xc0', |
| 217 |
]: |
| 218 |
self.checkdecodeerror(s, "utf-16", 2, len(s), addstuff=False) |
| 219 |
for s in [ |
| 220 |
|
| 221 |
"\xff\xfe\xff\xdb\xff\xff", |
| 222 |
]: |
| 223 |
self.checkdecodeerror(s, "utf-16", 2, 4, addstuff=False) |
| 224 |
|
| 225 |
def test_utf16_bugs(self): |
| 226 |
s = '\x80-\xe9\xdeL\xa3\x9b' |
| 227 |
py.test.raises(UnicodeDecodeError, runicode.str_decode_utf_16_le, |
| 228 |
s, len(s), True) |
| 229 |
|
| 230 |
def test_utf16_surrogates(self): |
| 231 |
assert runicode.unicode_encode_utf_16_be( |
| 232 |
u"\ud800", 1, None) == '\xd8\x00' |
| 233 |
py.test.raises(UnicodeEncodeError, runicode.unicode_encode_utf_16_be, |
| 234 |
u"\ud800", 1, None, allow_surrogates=False) |
| 235 |
def replace_with(ru, rs): |
| 236 |
def errorhandler(errors, enc, msg, u, startingpos, endingpos): |
| 237 |
if errors == 'strict': |
| 238 |
raise UnicodeEncodeError(enc, u, startingpos, |
| 239 |
endingpos, msg) |
| 240 |
return ru, rs, endingpos |
| 241 |
return runicode.unicode_encode_utf_16_be( |
| 242 |
u"<\ud800>", 3, None, |
| 243 |
errorhandler, allow_surrogates=False) |
| 244 |
assert replace_with(u'rep', None) == '\x00<\x00r\x00e\x00p\x00>' |
| 245 |
assert replace_with(None, '\xca\xfe') == '\x00<\xca\xfe\x00>' |
| 246 |
|
| 247 |
@py.test.mark.parametrize('unich',[u"\ud800", u"\udc80"]) |
| 248 |
def test_utf32_surrogates(self, unich): |
| 249 |
assert runicode.unicode_encode_utf_32_be( |
| 250 |
unich, 1, None) == struct.pack('>i', ord(unich)) |
| 251 |
py.test.raises(UnicodeEncodeError, runicode.unicode_encode_utf_32_be, |
| 252 |
unich, 1, None, allow_surrogates=False) |
| 253 |
def replace_with(ru, rs): |
| 254 |
def errorhandler(errors, enc, msg, u, startingpos, endingpos): |
| 255 |
if errors == 'strict': |
| 256 |
raise UnicodeEncodeError(enc, u, startingpos, |
| 257 |
endingpos, msg) |
| 258 |
return ru, rs, endingpos |
| 259 |
return runicode.unicode_encode_utf_32_be( |
| 260 |
u"<%s>" % unich, 3, None, |
| 261 |
errorhandler, allow_surrogates=False) |
| 262 |
assert replace_with(u'rep', None) == u'<rep>'.encode('utf-32-be') |
| 263 |
assert replace_with(None, '\xca\xfe\xca\xfe') == '\x00\x00\x00<\xca\xfe\xca\xfe\x00\x00\x00>' |
| 264 |
|
| 265 |
assert runicode.str_decode_utf_32_be( |
| 266 |
b"\x00\x00\xdc\x80", 4, None) == (u'\udc80', 4) |
| 267 |
py.test.raises(UnicodeDecodeError, runicode.py3k_str_decode_utf_32_be, |
| 268 |
b"\x00\x00\xdc\x80", 4, None) |
| 269 |
|
| 270 |
def test_utf7_bugs(self): |
| 271 |
u = u'A\u2262\u0391.' |
| 272 |
assert runicode.unicode_encode_utf_7(u, len(u), None) == 'A+ImIDkQ.' |
| 273 |
|
| 274 |
def test_utf7_tofrom_utf8_bug(self): |
| 275 |
def _assert_decu7(input, expected): |
| 276 |
assert runicode.str_decode_utf_7(input, len(input), None) == (expected, len(input)) |
| 277 |
|
| 278 |
_assert_decu7('+-', u'+') |
| 279 |
_assert_decu7('+-+-', u'++') |
| 280 |
_assert_decu7('+-+AOQ-', u'+\xe4') |
| 281 |
_assert_decu7('+AOQ-', u'\xe4') |
| 282 |
_assert_decu7('+AOQ-', u'\xe4') |
| 283 |
_assert_decu7('+AOQ- ', u'\xe4 ') |
| 284 |
_assert_decu7(' +AOQ-', u' \xe4') |
| 285 |
_assert_decu7(' +AOQ- ', u' \xe4 ') |
| 286 |
_assert_decu7('+AOQ-+AOQ-', u'\xe4\xe4') |
| 287 |
|
| 288 |
s_utf7 = 'Die M+AOQ-nner +AOQ-rgern sich!' |
| 289 |
s_utf8 = u'Die Männer ärgern sich!' |
| 290 |
s_utf8_esc = u'Die M\xe4nner \xe4rgern sich!' |
| 291 |
|
| 292 |
_assert_decu7(s_utf7, s_utf8_esc) |
| 293 |
_assert_decu7(s_utf7, s_utf8) |
| 294 |
|
| 295 |
assert runicode.unicode_encode_utf_7(s_utf8_esc, len(s_utf8_esc), None) == s_utf7 |
| 296 |
assert runicode.unicode_encode_utf_7(s_utf8, len(s_utf8_esc), None) == s_utf7 |
| 297 |
|
| 298 |
def test_utf7_partial(self): |
| 299 |
s = u"a+-b".encode('utf-7') |
| 300 |
assert s == "a+--b" |
| 301 |
decode = self.getdecoder('utf-7') |
| 302 |
assert decode(s, 1, None) == (u'a', 1) |
| 303 |
assert decode(s, 2, None) == (u'a', 1) |
| 304 |
assert decode(s, 3, None) == (u'a+', 3) |
| 305 |
assert decode(s, 4, None) == (u'a+-', 4) |
| 306 |
assert decode(s, 5, None) == (u'a+-b', 5) |
| 307 |
|
| 308 |
assert decode((27 * u"\u3042" + "\n").encode('utf7')[:28], 28, None) == (u'', 0) |
| 309 |
assert decode('+MEI\n+MEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCMEIwQjBCME', 72, None) == (u'\u3042\n', 5) |
| 310 |
|
| 311 |
def test_utf7_surrogates(self): |
| 312 |
encode = self.getencoder('utf-7') |
| 313 |
u = u'\U000abcde' |
| 314 |
assert encode(u, len(u), None) == '+2m/c3g-' |
| 315 |
decode = self.getdecoder('utf-7') |
| 316 |
|
| 317 |
|
| 318 |
assert encode(u'\uD801', 1, None) == '+2AE-' |
| 319 |
assert encode(u'\uD801x', 2, None) == '+2AE-x' |
| 320 |
assert encode(u'\uDC01', 1, None) == '+3AE-' |
| 321 |
assert encode(u'\uDC01x', 2, None) == '+3AE-x' |
| 322 |
assert decode('+2AE-', 5, None) == (u'\uD801', 5) |
| 323 |
assert decode('+2AE-x', 6, None) == (u'\uD801x', 6) |
| 324 |
assert decode('+3AE-', 5, None) == (u'\uDC01', 5) |
| 325 |
assert decode('+3AE-x', 6, None) == (u'\uDC01x', 6) |
| 326 |
|
| 327 |
u = u'\uD801\U000abcde' |
| 328 |
assert encode(u, len(u), None) == '+2AHab9ze-' |
| 329 |
assert decode('+2AHab9ze-', 10, None) == (u'\uD801\U000abcde', 10) |
| 330 |
|
| 331 |
|
| 332 |
class TestUTF8Decoding(UnicodeTests): |
| 333 |
def setup_method(self, meth): |
| 334 |
self.decoder = self.getdecoder('utf-8') |
| 335 |
|
| 336 |
def custom_replace(self, errors, encoding, msg, s, startingpos, endingpos): |
| 337 |
assert errors == 'custom' |
| 338 |
|
| 339 |
FOO = u'\u1234' |
| 340 |
return FOO, startingpos + 1 |
| 341 |
|
| 342 |
def to_bytestring(self, bytes): |
| 343 |
return ''.join(chr(int(c, 16)) for c in bytes.split()) |
| 344 |
|
| 345 |
def test_single_chars_utf8(self): |
| 346 |
for s in ["\xd7\x90", "\xd6\x96", "\xeb\x96\x95", "\xf0\x90\x91\x93"]: |
| 347 |
self.checkdecode(s, "utf-8") |
| 348 |
|
| 349 |
def test_utf8_surrogate(self): |
| 350 |
|
| 351 |
if runicode.MAXUNICODE < 65536: |
| 352 |
self.checkdecode(u"\ud800", "utf-8") |
| 353 |
else: |
| 354 |
py.test.raises(UnicodeDecodeError, self.checkdecode, u"\ud800", "utf-8") |
| 355 |
|
| 356 |
def test_invalid_start_byte(self): |
| 357 |
""" |
| 358 |
Test that an 'invalid start byte' error is raised when the first byte |
| 359 |
is not in the ASCII range or is not a valid start byte of a 2-, 3-, or |
| 360 |
4-bytes sequence. The invalid start byte is replaced with a single |
| 361 |
U+FFFD when errors='replace'. |
| 362 |
E.g. <80> is a continuation byte and can appear only after a start byte. |
| 363 |
""" |
| 364 |
FFFD = u'\ufffd' |
| 365 |
FOO = u'\u1234' |
| 366 |
for byte in '\x80\xA0\x9F\xBF\xC0\xC1\xF5\xFF': |
| 367 |
py.test.raises(UnicodeDecodeError, self.decoder, byte, 1, None, final=True) |
| 368 |
self.checkdecodeerror(byte, 'utf-8', 0, 1, addstuff=False, |
| 369 |
msg='invalid start byte') |
| 370 |
assert self.decoder(byte, 1, 'replace', final=True) == (FFFD, 1) |
| 371 |
assert (self.decoder('aaaa' + byte + 'bbbb', 9, 'replace', |
| 372 |
final=True) == |
| 373 |
(u'aaaa'+ FFFD + u'bbbb', 9)) |
| 374 |
assert self.decoder(byte, 1, 'ignore', final=True) == (u'', 1) |
| 375 |
assert (self.decoder('aaaa' + byte + 'bbbb', 9, 'ignore', |
| 376 |
final=True) == (u'aaaabbbb', 9)) |
| 377 |
assert self.decoder(byte, 1, 'custom', final=True, |
| 378 |
errorhandler=self.custom_replace) == (FOO, 1) |
| 379 |
assert (self.decoder('aaaa' + byte + 'bbbb', 9, 'custom', |
| 380 |
final=True, errorhandler=self.custom_replace) == |
| 381 |
(u'aaaa'+ FOO + u'bbbb', 9)) |
| 382 |
|
| 383 |
def test_unexpected_end_of_data(self): |
| 384 |
""" |
| 385 |
Test that an 'unexpected end of data' error is raised when the string |
| 386 |
ends after a start byte of a 2-, 3-, or 4-bytes sequence without having |
| 387 |
enough continuation bytes. The incomplete sequence is replaced with a |
| 388 |
single U+FFFD when errors='replace'. |
| 389 |
E.g. in the sequence <F3 80 80>, F3 is the start byte of a 4-bytes |
| 390 |
sequence, but it's followed by only 2 valid continuation bytes and the |
| 391 |
last continuation bytes is missing. |
| 392 |
Note: the continuation bytes must be all valid, if one of them is |
| 393 |
invalid another error will be raised. |
| 394 |
""" |
| 395 |
sequences = [ |
| 396 |
'C2', 'DF', |
| 397 |
'E0 A0', 'E0 BF', 'E1 80', 'E1 BF', 'EC 80', 'EC BF', |
| 398 |
'ED 80', 'ED 9F', 'EE 80', 'EE BF', 'EF 80', 'EF BF', |
| 399 |
'F0 90', 'F0 BF', 'F0 90 80', 'F0 90 BF', 'F0 BF 80', 'F0 BF BF', |
| 400 |
'F1 80', 'F1 BF', 'F1 80 80', 'F1 80 BF', 'F1 BF 80', 'F1 BF BF', |
| 401 |
'F3 80', 'F3 BF', 'F3 80 80', 'F3 80 BF', 'F3 BF 80', 'F3 BF BF', |
| 402 |
'F4 80', 'F4 8F', 'F4 80 80', 'F4 80 BF', 'F4 8F 80', 'F4 8F BF' |
| 403 |
] |
| 404 |
FFFD = u'\ufffd' |
| 405 |
FOO = u'\u1234' |
| 406 |
for seq in sequences: |
| 407 |
seq = self.to_bytestring(seq) |
| 408 |
py.test.raises(UnicodeDecodeError, self.decoder, seq, len(seq), |
| 409 |
None, final=True) |
| 410 |
self.checkdecodeerror(seq, 'utf-8', 0, len(seq), addstuff=False, |
| 411 |
msg='unexpected end of data') |
| 412 |
assert self.decoder(seq, len(seq), 'replace', final=True |
| 413 |
) == (FFFD, len(seq)) |
| 414 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, |
| 415 |
'replace', final=True) == |
| 416 |
(u'aaaa'+ FFFD + u'bbbb', len(seq) + 8)) |
| 417 |
assert self.decoder(seq, len(seq), 'ignore', final=True |
| 418 |
) == (u'', len(seq)) |
| 419 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, 'ignore', |
| 420 |
final=True) == (u'aaaabbbb', len(seq) + 8)) |
| 421 |
assert (self.decoder(seq, len(seq), 'custom', final=True, |
| 422 |
errorhandler=self.custom_replace) == |
| 423 |
(FOO * len(seq), len(seq))) |
| 424 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, 'custom', |
| 425 |
final=True, errorhandler=self.custom_replace) == |
| 426 |
(u'aaaa'+ FOO * len(seq) + u'bbbb', len(seq) + 8)) |
| 427 |
|
| 428 |
def test_invalid_cb_for_2bytes_seq(self): |
| 429 |
""" |
| 430 |
Test that an 'invalid continuation byte' error is raised when the |
| 431 |
continuation byte of a 2-bytes sequence is invalid. The start byte |
| 432 |
is replaced by a single U+FFFD and the second byte is handled |
| 433 |
separately when errors='replace'. |
| 434 |
E.g. in the sequence <C2 41>, C2 is the start byte of a 2-bytes |
| 435 |
sequence, but 41 is not a valid continuation byte because it's the |
| 436 |
ASCII letter 'A'. |
| 437 |
""" |
| 438 |
FFFD = u'\ufffd' |
| 439 |
FFFDx2 = FFFD * 2 |
| 440 |
sequences = [ |
| 441 |
('C2 00', FFFD+u'\x00'), ('C2 7F', FFFD+u'\x7f'), |
| 442 |
('C2 C0', FFFDx2), ('C2 FF', FFFDx2), |
| 443 |
('DF 00', FFFD+u'\x00'), ('DF 7F', FFFD+u'\x7f'), |
| 444 |
('DF C0', FFFDx2), ('DF FF', FFFDx2), |
| 445 |
] |
| 446 |
for seq, res in sequences: |
| 447 |
seq = self.to_bytestring(seq) |
| 448 |
py.test.raises(UnicodeDecodeError, self.decoder, seq, len(seq), |
| 449 |
None, final=True) |
| 450 |
self.checkdecodeerror(seq, 'utf-8', 0, 1, addstuff=False, |
| 451 |
msg='invalid continuation byte') |
| 452 |
assert self.decoder(seq, len(seq), 'replace', final=True |
| 453 |
) == (res, len(seq)) |
| 454 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, |
| 455 |
'replace', final=True) == |
| 456 |
(u'aaaa' + res + u'bbbb', len(seq) + 8)) |
| 457 |
res = res.replace(FFFD, u'') |
| 458 |
assert self.decoder(seq, len(seq), 'ignore', final=True |
| 459 |
) == (res, len(seq)) |
| 460 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, |
| 461 |
'ignore', final=True) == |
| 462 |
(u'aaaa' + res + u'bbbb', len(seq) + 8)) |
| 463 |
|
| 464 |
def test_invalid_cb_for_3bytes_seq(self): |
| 465 |
""" |
| 466 |
Test that an 'invalid continuation byte' error is raised when the |
| 467 |
continuation byte(s) of a 3-bytes sequence are invalid. When |
| 468 |
errors='replace', if the first continuation byte is valid, the first |
| 469 |
two bytes (start byte + 1st cb) are replaced by a single U+FFFD and the |
| 470 |
third byte is handled separately, otherwise only the start byte is |
| 471 |
replaced with a U+FFFD and the other continuation bytes are handled |
| 472 |
separately. |
| 473 |
E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 474 |
sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 475 |
because it's the ASCII letter 'A'. |
| 476 |
Note: when the start byte is E0 or ED, the valid ranges for the first |
| 477 |
continuation byte are limited to A0..BF and 80..9F respectively. |
| 478 |
However, when the start byte is ED, Python 2 considers all the bytes |
| 479 |
in range 80..BF valid. This is fixed in Python 3. |
| 480 |
""" |
| 481 |
FFFD = u'\ufffd' |
| 482 |
FFFDx2 = FFFD * 2 |
| 483 |
sequences = [ |
| 484 |
('E0 00', FFFD+u'\x00'), ('E0 7F', FFFD+u'\x7f'), ('E0 80', FFFDx2), |
| 485 |
('E0 9F', FFFDx2), ('E0 C0', FFFDx2), ('E0 FF', FFFDx2), |
| 486 |
('E0 A0 00', FFFD+u'\x00'), ('E0 A0 7F', FFFD+u'\x7f'), |
| 487 |
('E0 A0 C0', FFFDx2), ('E0 A0 FF', FFFDx2), |
| 488 |
('E0 BF 00', FFFD+u'\x00'), ('E0 BF 7F', FFFD+u'\x7f'), |
| 489 |
('E0 BF C0', FFFDx2), ('E0 BF FF', FFFDx2), ('E1 00', FFFD+u'\x00'), |
| 490 |
('E1 7F', FFFD+u'\x7f'), ('E1 C0', FFFDx2), ('E1 FF', FFFDx2), |
| 491 |
('E1 80 00', FFFD+u'\x00'), ('E1 80 7F', FFFD+u'\x7f'), |
| 492 |
('E1 80 C0', FFFDx2), ('E1 80 FF', FFFDx2), |
| 493 |
('E1 BF 00', FFFD+u'\x00'), ('E1 BF 7F', FFFD+u'\x7f'), |
| 494 |
('E1 BF C0', FFFDx2), ('E1 BF FF', FFFDx2), ('EC 00', FFFD+u'\x00'), |
| 495 |
('EC 7F', FFFD+u'\x7f'), ('EC C0', FFFDx2), ('EC FF', FFFDx2), |
| 496 |
('EC 80 00', FFFD+u'\x00'), ('EC 80 7F', FFFD+u'\x7f'), |
| 497 |
('EC 80 C0', FFFDx2), ('EC 80 FF', FFFDx2), |
| 498 |
('EC BF 00', FFFD+u'\x00'), ('EC BF 7F', FFFD+u'\x7f'), |
| 499 |
('EC BF C0', FFFDx2), ('EC BF FF', FFFDx2), ('ED 00', FFFD+u'\x00'), |
| 500 |
('ED 7F', FFFD+u'\x7f'), |
| 501 |
|
| 502 |
('ED C0', FFFDx2), ('ED FF', FFFDx2), ('ED 80 00', FFFD+u'\x00'), |
| 503 |
('ED 80 7F', FFFD+u'\x7f'), ('ED 80 C0', FFFDx2), |
| 504 |
('ED 80 FF', FFFDx2), ('ED 9F 00', FFFD+u'\x00'), |
| 505 |
('ED 9F 7F', FFFD+u'\x7f'), ('ED 9F C0', FFFDx2), |
| 506 |
('ED 9F FF', FFFDx2), ('EE 00', FFFD+u'\x00'), |
| 507 |
('EE 7F', FFFD+u'\x7f'), ('EE C0', FFFDx2), ('EE FF', FFFDx2), |
| 508 |
('EE 80 00', FFFD+u'\x00'), ('EE 80 7F', FFFD+u'\x7f'), |
| 509 |
('EE 80 C0', FFFDx2), ('EE 80 FF', FFFDx2), |
| 510 |
('EE BF 00', FFFD+u'\x00'), ('EE BF 7F', FFFD+u'\x7f'), |
| 511 |
('EE BF C0', FFFDx2), ('EE BF FF', FFFDx2), ('EF 00', FFFD+u'\x00'), |
| 512 |
('EF 7F', FFFD+u'\x7f'), ('EF C0', FFFDx2), ('EF FF', FFFDx2), |
| 513 |
('EF 80 00', FFFD+u'\x00'), ('EF 80 7F', FFFD+u'\x7f'), |
| 514 |
('EF 80 C0', FFFDx2), ('EF 80 FF', FFFDx2), |
| 515 |
('EF BF 00', FFFD+u'\x00'), ('EF BF 7F', FFFD+u'\x7f'), |
| 516 |
('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2), |
| 517 |
] |
| 518 |
for seq, res in sequences: |
| 519 |
seq = self.to_bytestring(seq) |
| 520 |
py.test.raises(UnicodeDecodeError, self.decoder, seq, len(seq), |
| 521 |
None, final=True) |
| 522 |
self.checkdecodeerror(seq, 'utf-8', 0, len(seq)-1, addstuff=False, |
| 523 |
msg='invalid continuation byte') |
| 524 |
assert self.decoder(seq, len(seq), 'replace', final=True |
| 525 |
) == (res, len(seq)) |
| 526 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, |
| 527 |
'replace', final=True) == |
| 528 |
(u'aaaa' + res + u'bbbb', len(seq) + 8)) |
| 529 |
res = res.replace(FFFD, u'') |
| 530 |
assert self.decoder(seq, len(seq), 'ignore', final=True |
| 531 |
) == (res, len(seq)) |
| 532 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, 'ignore', |
| 533 |
final=True) == (u'aaaa' + res + u'bbbb', len(seq) + 8)) |
| 534 |
|
| 535 |
def test_invalid_cb_for_4bytes_seq(self): |
| 536 |
""" |
| 537 |
Test that an 'invalid continuation byte' error is raised when the |
| 538 |
continuation byte(s) of a 4-bytes sequence are invalid. When |
| 539 |
errors='replace',the start byte and all the following valid |
| 540 |
continuation bytes are replaced with a single U+FFFD, and all the bytes |
| 541 |
starting from the first invalid continuation bytes (included) are |
| 542 |
handled separately. |
| 543 |
E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 544 |
sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 545 |
because it's the ASCII letter 'A'. |
| 546 |
Note: when the start byte is E0 or ED, the valid ranges for the first |
| 547 |
continuation byte are limited to A0..BF and 80..9F respectively. |
| 548 |
However, when the start byte is ED, Python 2 considers all the bytes |
| 549 |
in range 80..BF valid. This is fixed in Python 3. |
| 550 |
""" |
| 551 |
FFFD = u'\ufffd' |
| 552 |
FFFDx2 = FFFD * 2 |
| 553 |
sequences = [ |
| 554 |
('F0 00', FFFD+u'\x00'), ('F0 7F', FFFD+u'\x7f'), ('F0 80', FFFDx2), |
| 555 |
('F0 8F', FFFDx2), ('F0 C0', FFFDx2), ('F0 FF', FFFDx2), |
| 556 |
('F0 90 00', FFFD+u'\x00'), ('F0 90 7F', FFFD+u'\x7f'), |
| 557 |
('F0 90 C0', FFFDx2), ('F0 90 FF', FFFDx2), |
| 558 |
('F0 BF 00', FFFD+u'\x00'), ('F0 BF 7F', FFFD+u'\x7f'), |
| 559 |
('F0 BF C0', FFFDx2), ('F0 BF FF', FFFDx2), |
| 560 |
('F0 90 80 00', FFFD+u'\x00'), ('F0 90 80 7F', FFFD+u'\x7f'), |
| 561 |
('F0 90 80 C0', FFFDx2), ('F0 90 80 FF', FFFDx2), |
| 562 |
('F0 90 BF 00', FFFD+u'\x00'), ('F0 90 BF 7F', FFFD+u'\x7f'), |
| 563 |
('F0 90 BF C0', FFFDx2), ('F0 90 BF FF', FFFDx2), |
| 564 |
('F0 BF 80 00', FFFD+u'\x00'), ('F0 BF 80 7F', FFFD+u'\x7f'), |
| 565 |
('F0 BF 80 C0', FFFDx2), ('F0 BF 80 FF', FFFDx2), |
| 566 |
('F0 BF BF 00', FFFD+u'\x00'), ('F0 BF BF 7F', FFFD+u'\x7f'), |
| 567 |
('F0 BF BF C0', FFFDx2), ('F0 BF BF FF', FFFDx2), |
| 568 |
('F1 00', FFFD+u'\x00'), ('F1 7F', FFFD+u'\x7f'), ('F1 C0', FFFDx2), |
| 569 |
('F1 FF', FFFDx2), ('F1 80 00', FFFD+u'\x00'), |
| 570 |
('F1 80 7F', FFFD+u'\x7f'), ('F1 80 C0', FFFDx2), |
| 571 |
('F1 80 FF', FFFDx2), ('F1 BF 00', FFFD+u'\x00'), |
| 572 |
('F1 BF 7F', FFFD+u'\x7f'), ('F1 BF C0', FFFDx2), |
| 573 |
('F1 BF FF', FFFDx2), ('F1 80 80 00', FFFD+u'\x00'), |
| 574 |
('F1 80 80 7F', FFFD+u'\x7f'), ('F1 80 80 C0', FFFDx2), |
| 575 |
('F1 80 80 FF', FFFDx2), ('F1 80 BF 00', FFFD+u'\x00'), |
| 576 |
('F1 80 BF 7F', FFFD+u'\x7f'), ('F1 80 BF C0', FFFDx2), |
| 577 |
('F1 80 BF FF', FFFDx2), ('F1 BF 80 00', FFFD+u'\x00'), |
| 578 |
('F1 BF 80 7F', FFFD+u'\x7f'), ('F1 BF 80 C0', FFFDx2), |
| 579 |
('F1 BF 80 FF', FFFDx2), ('F1 BF BF 00', FFFD+u'\x00'), |
| 580 |
('F1 BF BF 7F', FFFD+u'\x7f'), ('F1 BF BF C0', FFFDx2), |
| 581 |
('F1 BF BF FF', FFFDx2), ('F3 00', FFFD+u'\x00'), |
| 582 |
('F3 7F', FFFD+u'\x7f'), ('F3 C0', FFFDx2), ('F3 FF', FFFDx2), |
| 583 |
('F3 80 00', FFFD+u'\x00'), ('F3 80 7F', FFFD+u'\x7f'), |
| 584 |
('F3 80 C0', FFFDx2), ('F3 80 FF', FFFDx2), |
| 585 |
('F3 BF 00', FFFD+u'\x00'), ('F3 BF 7F', FFFD+u'\x7f'), |
| 586 |
('F3 BF C0', FFFDx2), ('F3 BF FF', FFFDx2), |
| 587 |
('F3 80 80 00', FFFD+u'\x00'), ('F3 80 80 7F', FFFD+u'\x7f'), |
| 588 |
('F3 80 80 C0', FFFDx2), ('F3 80 80 FF', FFFDx2), |
| 589 |
('F3 80 BF 00', FFFD+u'\x00'), ('F3 80 BF 7F', FFFD+u'\x7f'), |
| 590 |
('F3 80 BF C0', FFFDx2), ('F3 80 BF FF', FFFDx2), |
| 591 |
('F3 BF 80 00', FFFD+u'\x00'), ('F3 BF 80 7F', FFFD+u'\x7f'), |
| 592 |
('F3 BF 80 C0', FFFDx2), ('F3 BF 80 FF', FFFDx2), |
| 593 |
('F3 BF BF 00', FFFD+u'\x00'), ('F3 BF BF 7F', FFFD+u'\x7f'), |
| 594 |
('F3 BF BF C0', FFFDx2), ('F3 BF BF FF', FFFDx2), |
| 595 |
('F4 00', FFFD+u'\x00'), ('F4 7F', FFFD+u'\x7f'), ('F4 90', FFFDx2), |
| 596 |
('F4 BF', FFFDx2), ('F4 C0', FFFDx2), ('F4 FF', FFFDx2), |
| 597 |
('F4 80 00', FFFD+u'\x00'), ('F4 80 7F', FFFD+u'\x7f'), |
| 598 |
('F4 80 C0', FFFDx2), ('F4 80 FF', FFFDx2), |
| 599 |
('F4 8F 00', FFFD+u'\x00'), ('F4 8F 7F', FFFD+u'\x7f'), |
| 600 |
('F4 8F C0', FFFDx2), ('F4 8F FF', FFFDx2), |
| 601 |
('F4 80 80 00', FFFD+u'\x00'), ('F4 80 80 7F', FFFD+u'\x7f'), |
| 602 |
('F4 80 80 C0', FFFDx2), ('F4 80 80 FF', FFFDx2), |
| 603 |
('F4 80 BF 00', FFFD+u'\x00'), ('F4 80 BF 7F', FFFD+u'\x7f'), |
| 604 |
('F4 80 BF C0', FFFDx2), ('F4 80 BF FF', FFFDx2), |
| 605 |
('F4 8F 80 00', FFFD+u'\x00'), ('F4 8F 80 7F', FFFD+u'\x7f'), |
| 606 |
('F4 8F 80 C0', FFFDx2), ('F4 8F 80 FF', FFFDx2), |
| 607 |
('F4 8F BF 00', FFFD+u'\x00'), ('F4 8F BF 7F', FFFD+u'\x7f'), |
| 608 |
('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2) |
| 609 |
] |
| 610 |
for seq, res in sequences: |
| 611 |
seq = self.to_bytestring(seq) |
| 612 |
py.test.raises(UnicodeDecodeError, self.decoder, seq, len(seq), |
| 613 |
None, final=True) |
| 614 |
self.checkdecodeerror(seq, 'utf-8', 0, len(seq)-1, addstuff=False, |
| 615 |
msg='invalid continuation byte') |
| 616 |
assert self.decoder(seq, len(seq), 'replace', final=True |
| 617 |
) == (res, len(seq)) |
| 618 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, |
| 619 |
'replace', final=True) == |
| 620 |
(u'aaaa' + res + u'bbbb', len(seq) + 8)) |
| 621 |
res = res.replace(FFFD, u'') |
| 622 |
assert self.decoder(seq, len(seq), 'ignore', final=True |
| 623 |
) == (res, len(seq)) |
| 624 |
assert (self.decoder('aaaa' + seq + 'bbbb', len(seq) + 8, 'ignore', |
| 625 |
final=True) == (u'aaaa' + res + u'bbbb', len(seq) + 8)) |
| 626 |
|
| 627 |
def test_utf8_errors(self): |
| 628 |
|
| 629 |
for s in ['\xd7', '\xd6', '\xeb\x96', '\xf0\x90\x91', '\xc2', '\xdf']: |
| 630 |
self.checkdecodeerror(s, 'utf-8', 0, len(s), addstuff=False, |
| 631 |
msg='unexpected end of data') |
| 632 |
|
| 633 |
|
| 634 |
for s in ["\xd7\x50", "\xd6\x06", "\xd6\xD6"]: |
| 635 |
self.checkdecodeerror(s, "utf-8", 0, 1, addstuff=True, |
| 636 |
msg='invalid continuation byte') |
| 637 |
|
| 638 |
for s in ["\xeb\x56\x95", "\xeb\x06\x95", "\xeb\xD6\x95"]: |
| 639 |
self.checkdecodeerror(s, "utf-8", 0, 1, addstuff=True, |
| 640 |
msg='invalid continuation byte') |
| 641 |
for s in ["\xeb\x96\x55", "\xeb\x96\x05", "\xeb\x96\xD5"]: |
| 642 |
self.checkdecodeerror(s, "utf-8", 0, 2, addstuff=True, |
| 643 |
msg='invalid continuation byte') |
| 644 |
|
| 645 |
for s in ["\xf0\x50\x91\x93", "\xf0\x00\x91\x93", "\xf0\xd0\x91\x93"]: |
| 646 |
self.checkdecodeerror(s, "utf-8", 0, 1, addstuff=True, |
| 647 |
msg='invalid continuation byte') |
| 648 |
for s in ["\xf0\x90\x51\x93", "\xf0\x90\x01\x93", "\xf0\x90\xd1\x93"]: |
| 649 |
self.checkdecodeerror(s, "utf-8", 0, 2, addstuff=True, |
| 650 |
msg='invalid continuation byte') |
| 651 |
for s in ["\xf0\x90\x91\x53", "\xf0\x90\x91\x03", "\xf0\x90\x91\xd3"]: |
| 652 |
self.checkdecodeerror(s, "utf-8", 0, 3, addstuff=True, |
| 653 |
msg='invalid continuation byte') |
| 654 |
|
| 655 |
def test_issue8271(self): |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
FFFD = u'\ufffd' |
| 663 |
sequences = [ |
| 664 |
|
| 665 |
('\x80', FFFD), |
| 666 |
('\x80\x80', FFFD*2), |
| 667 |
('\xc0', FFFD), |
| 668 |
('\xc0\xc0', FFFD*2), |
| 669 |
('\xc1', FFFD), |
| 670 |
('\xc1\xc0', FFFD*2), |
| 671 |
('\xc0\xc1', FFFD*2), |
| 672 |
|
| 673 |
('\xc2', FFFD), |
| 674 |
('\xc2\xc2', FFFD*2), |
| 675 |
('\xc2\xc2\xc2', FFFD*3), |
| 676 |
('\xc2\x41', FFFD+'A'), |
| 677 |
|
| 678 |
('\xe1', FFFD), |
| 679 |
('\xe1\xe1', FFFD*2), |
| 680 |
('\xe1\xe1\xe1', FFFD*3), |
| 681 |
('\xe1\xe1\xe1\xe1', FFFD*4), |
| 682 |
('\xe1\x80', FFFD), |
| 683 |
('\xe1\x41', FFFD+'A'), |
| 684 |
('\xe1\x41\x80', FFFD+'A'+FFFD), |
| 685 |
('\xe1\x41\x41', FFFD+'AA'), |
| 686 |
('\xe1\x80\x41', FFFD+'A'), |
| 687 |
('\xe1\x80\xe1\x41', FFFD*2+'A'), |
| 688 |
('\xe1\x41\xe1\x80', FFFD+'A'+FFFD), |
| 689 |
|
| 690 |
('\xf1', FFFD), |
| 691 |
('\xf1\xf1', FFFD*2), |
| 692 |
('\xf1\xf1\xf1', FFFD*3), |
| 693 |
('\xf1\xf1\xf1\xf1', FFFD*4), |
| 694 |
('\xf1\xf1\xf1\xf1\xf1', FFFD*5), |
| 695 |
('\xf1\x80', FFFD), |
| 696 |
('\xf1\x80\x80', FFFD), |
| 697 |
('\xf1\x80\x41', FFFD+'A'), |
| 698 |
('\xf1\x80\x41\x41', FFFD+'AA'), |
| 699 |
('\xf1\x80\x80\x41', FFFD+'A'), |
| 700 |
('\xf1\x41\x80', FFFD+'A'+FFFD), |
| 701 |
('\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), |
| 702 |
('\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), |
| 703 |
('\xf1\x41\x41\x80', FFFD+'AA'+FFFD), |
| 704 |
('\xf1\x41\xf1\x80', FFFD+'A'+FFFD), |
| 705 |
('\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2), |
| 706 |
('\xf1\xf1\x80\x41', FFFD*2+'A'), |
| 707 |
('\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2), |
| 708 |
|
| 709 |
('\xf5', FFFD), |
| 710 |
('\xf5\xf5', FFFD*2), |
| 711 |
('\xf5\x80', FFFD*2), |
| 712 |
('\xf5\x80\x80', FFFD*3), |
| 713 |
('\xf5\x80\x80\x80', FFFD*4), |
| 714 |
('\xf5\x80\x41', FFFD*2+'A'), |
| 715 |
('\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD), |
| 716 |
('\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'), |
| 717 |
|
| 718 |
('\xf8', FFFD), |
| 719 |
('\xf8\xf8', FFFD*2), |
| 720 |
('\xf8\x80', FFFD*2), |
| 721 |
('\xf8\x80\x41', FFFD*2 + 'A'), |
| 722 |
('\xf8\x80\x80\x80\x80', FFFD*5), |
| 723 |
|
| 724 |
('\xfc', FFFD), |
| 725 |
('\xfc\xfc', FFFD*2), |
| 726 |
('\xfc\x80\x80', FFFD*3), |
| 727 |
('\xfc\x80\x80\x80\x80\x80', FFFD*6), |
| 728 |
|
| 729 |
('\xfe', FFFD), |
| 730 |
('\xfe\x80\x80', FFFD*3), |
| 731 |
|
| 732 |
('\xf1\x80\x41\x42\x43', u'\ufffd\x41\x42\x43'), |
| 733 |
('\xf1\x80\xff\x42\x43', u'\ufffd\ufffd\x42\x43'), |
| 734 |
('\xf1\x80\xc2\x81\x43', u'\ufffd\x81\x43'), |
| 735 |
('\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64', |
| 736 |
u'\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'), |
| 737 |
] |
| 738 |
|
| 739 |
for n, (seq, res) in enumerate(sequences): |
| 740 |
decoder = self.getdecoder('utf-8') |
| 741 |
py.test.raises(UnicodeDecodeError, decoder, seq, len(seq), None, final=True) |
| 742 |
assert decoder(seq, len(seq), 'replace', final=True |
| 743 |
) == (res, len(seq)) |
| 744 |
assert decoder(seq + 'b', len(seq) + 1, 'replace', final=True |
| 745 |
) == (res + u'b', len(seq) + 1) |
| 746 |
res = res.replace(FFFD, u'') |
| 747 |
assert decoder(seq, len(seq), 'ignore', final=True |
| 748 |
) == (res, len(seq)) |
| 749 |
|
| 750 |
|
| 751 |
class TestEncoding(UnicodeTests): |
| 752 |
def test_all_ascii(self): |
| 753 |
for i in range(128): |
| 754 |
if sys.version >= "2.7": |
| 755 |
self.checkencode(unichr(i), "utf-7") |
| 756 |
for encoding in "utf-8 latin-1 ascii".split(): |
| 757 |
self.checkencode(unichr(i), encoding) |
| 758 |
|
| 759 |
def test_all_first_256(self): |
| 760 |
for i in range(256): |
| 761 |
if sys.version >= "2.7": |
| 762 |
self.checkencode(unichr(i), "utf-7") |
| 763 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 764 |
"utf-32 utf-32-be utf-32-le").split(): |
| 765 |
self.checkencode(unichr(i), encoding) |
| 766 |
|
| 767 |
def test_first_10000(self): |
| 768 |
for i in range(10000): |
| 769 |
if sys.version >= "2.7": |
| 770 |
self.checkencode(unichr(i), "utf-7") |
| 771 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 772 |
"utf-32 utf-32-be utf-32-le").split(): |
| 773 |
self.checkencode(unichr(i), encoding) |
| 774 |
|
| 775 |
def test_random(self): |
| 776 |
for i in range(10000): |
| 777 |
v = random.randrange(sys.maxunicode) |
| 778 |
if 0xd800 <= v <= 0xdfff: |
| 779 |
continue |
| 780 |
uni = unichr(v) |
| 781 |
if sys.version >= "2.7": |
| 782 |
self.checkencode(uni, "utf-7") |
| 783 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 784 |
"utf-32 utf-32-be utf-32-le").split(): |
| 785 |
self.checkencode(uni, encoding) |
| 786 |
|
| 787 |
def test_maxunicode(self): |
| 788 |
uni = unichr(sys.maxunicode) |
| 789 |
if sys.version >= "2.7": |
| 790 |
self.checkencode(uni, "utf-7") |
| 791 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 792 |
"utf-32 utf-32-be utf-32-le").split(): |
| 793 |
self.checkencode(uni, encoding) |
| 794 |
|
| 795 |
def test_empty(self): |
| 796 |
for encoding in ("utf-8 utf-16 utf-16-be utf-16-le " |
| 797 |
"utf-32 utf-32-be utf-32-le").split(): |
| 798 |
self.checkencode(u'', encoding) |
| 799 |
|
| 800 |
def test_single_chars_utf8(self): |
| 801 |
|
| 802 |
for s in ["\xd7\x90", "\xd6\x96", "\xeb\x96\x95", "\xf0\x90\x91\x93"]: |
| 803 |
self.checkencode(s, "utf-8") |
| 804 |
|
| 805 |
def test_utf8_surrogates(self): |
| 806 |
|
| 807 |
u = u"\ud800" |
| 808 |
for i in range(4): |
| 809 |
u += u"\udc00" |
| 810 |
if runicode.MAXUNICODE < 65536: |
| 811 |
|
| 812 |
self.checkencode(u, "utf-8") |
| 813 |
else: |
| 814 |
|
| 815 |
py.test.raises(UnicodeEncodeError, self.checkencode, u, "utf-8") |
| 816 |
|
| 817 |
def test_ascii_error(self): |
| 818 |
self.checkencodeerror(u"abc\xFF\xFF\xFFcde", "ascii", 3, 6) |
| 819 |
|
| 820 |
def test_latin1_error(self): |
| 821 |
self.checkencodeerror(u"abc\uffff\uffff\uffffcde", "latin-1", 3, 6) |
| 822 |
|
| 823 |
def test_encode_decimal(self): |
| 824 |
encoder = self.getencoder('decimal') |
| 825 |
assert encoder(u' 12, 34 ', 8, None) == ' 12, 34 ' |
| 826 |
py.test.raises(UnicodeEncodeError, encoder, u' 12, \u1234 ', 7, None) |
| 827 |
assert encoder(u'u\u1234', 2, 'replace') == 'u?' |
| 828 |
|
| 829 |
def test_encode_utf8sp(self): |
| 830 |
|
| 831 |
|
| 832 |
c = u"\udc00" |
| 833 |
for input, expected in [ |
| 834 |
(u"", ""), |
| 835 |
(u"abc", "abc"), |
| 836 |
(u"\u1234", "\xe1\x88\xb4"), |
| 837 |
(u"\ud800", "\xed\xa0\x80"), |
| 838 |
(u"\udc00", "\xed\xb0\x80"), |
| 839 |
(u"\ud800" + c, "\xed\xa0\x80\xed\xb0\x80"), |
| 840 |
]: |
| 841 |
got = runicode.unicode_encode_utf8sp(input, len(input)) |
| 842 |
assert got == expected |
| 843 |
|
| 844 |
|
| 845 |
class TestTranslation(object): |
| 846 |
def setup_class(cls): |
| 847 |
if runicode.MAXUNICODE != sys.maxunicode: |
| 848 |
py.test.skip("these tests cannot run on the llinterp") |
| 849 |
|
| 850 |
def test_utf8(self): |
| 851 |
from rpython.rtyper.test.test_llinterp import interpret |
| 852 |
def f(x): |
| 853 |
|
| 854 |
s1 = "".join(["\xd7\x90\xd6\x96\xeb\x96\x95\xf0\x90\x91\x93"] * x) |
| 855 |
u, consumed = runicode.str_decode_utf_8(s1, len(s1), 'strict', |
| 856 |
allow_surrogates=True) |
| 857 |
s2 = runicode.unicode_encode_utf_8(u, len(u), 'strict', |
| 858 |
allow_surrogates=True) |
| 859 |
u3, consumed3 = runicode.str_decode_utf_8(s1, len(s1), 'strict', |
| 860 |
allow_surrogates=False) |
| 861 |
s3 = runicode.unicode_encode_utf_8(u3, len(u3), 'strict', |
| 862 |
allow_surrogates=False) |
| 863 |
return s1 == s2 == s3 |
| 864 |
res = interpret(f, [2]) |
| 865 |
assert res |
| 866 |
|
| 867 |
def test_surrogates(self): |
| 868 |
if runicode.MAXUNICODE < 65536: |
| 869 |
py.test.skip("Narrow unicode build") |
| 870 |
from rpython.rtyper.test.test_llinterp import interpret |
| 871 |
def f(x): |
| 872 |
u = runicode.UNICHR(x) |
| 873 |
t = runicode.ORD(u) |
| 874 |
return t |
| 875 |
|
| 876 |
res = interpret(f, [0x10140]) |
| 877 |
assert res == 0x10140 |
| 878 |
|
| 879 |
def test_encode_surrogate_pair(self): |
| 880 |
u = runicode.UNICHR(0xD800) + runicode.UNICHR(0xDC00) |
| 881 |
if runicode.MAXUNICODE < 65536: |
| 882 |
|
| 883 |
assert runicode.unicode_encode_unicode_escape( |
| 884 |
u, len(u), True) == r'\U00010000' |
| 885 |
assert runicode.unicode_encode_raw_unicode_escape( |
| 886 |
u, len(u), True) == r'\U00010000' |
| 887 |
else: |
| 888 |
|
| 889 |
assert runicode.unicode_encode_unicode_escape( |
| 890 |
u, len(u), True) == r'\ud800\udc00' |
| 891 |
assert runicode.unicode_encode_raw_unicode_escape( |
| 892 |
u, len(u), True) == r'\ud800\udc00' |
| 893 |
|
| 894 |
def test_encode_surrogate_pair_utf8(self): |
| 895 |
u = runicode.UNICHR(0xD800) + runicode.UNICHR(0xDC00) |
| 896 |
if runicode.MAXUNICODE < 65536: |
| 897 |
|
| 898 |
assert runicode.unicode_encode_utf_8( |
| 899 |
u, len(u), True, allow_surrogates=True) == '\xf0\x90\x80\x80' |
| 900 |
assert runicode.unicode_encode_utf_8( |
| 901 |
u, len(u), True, allow_surrogates=False) == '\xf0\x90\x80\x80' |
| 902 |
else: |
| 903 |
|
| 904 |
assert runicode.unicode_encode_utf_8( |
| 905 |
u, len(u), True, allow_surrogates=True) == '\xf0\x90\x80\x80' |
| 906 |
|
| 907 |
py.test.raises( |
| 908 |
UnicodeEncodeError, runicode.unicode_encode_utf_8, |
| 909 |
u, len(u), True, allow_surrogates=False) |