| 1 |
|
| 2 |
import sys |
| 3 |
import pytest |
| 4 |
|
| 5 |
class AppTestCodecs: |
| 6 |
spaceconfig = { |
| 7 |
"usemodules": ['unicodedata', 'struct', 'binascii', '_warnings'], |
| 8 |
} |
| 9 |
|
| 10 |
def test_register_noncallable(self): |
| 11 |
import _codecs |
| 12 |
raises(TypeError, _codecs.register, 1) |
| 13 |
|
| 14 |
def test_bigU_codecs(self): |
| 15 |
u = u'\U00010001\U00020002\U00030003\U00040004\U00050005' |
| 16 |
for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
| 17 |
'utf-32', 'utf-32-le', 'utf-32-be', |
| 18 |
'raw_unicode_escape', |
| 19 |
'unicode_escape'): |
| 20 |
assert str(u.encode(encoding), encoding) == u |
| 21 |
|
| 22 |
def test_ucs4(self): |
| 23 |
x = u'\U00100000' |
| 24 |
y = x.encode("raw-unicode-escape").decode("raw-unicode-escape") |
| 25 |
assert x == y |
| 26 |
|
| 27 |
def test_named_unicode(self): |
| 28 |
assert str(b'\\N{SPACE}','unicode-escape') == u" " |
| 29 |
raises( UnicodeDecodeError, str,b'\\N{SPACE','unicode-escape') |
| 30 |
raises( UnicodeDecodeError, str,b'\\NSPACE}','unicode-escape') |
| 31 |
raises( UnicodeDecodeError, str,b'\\NSPACE','unicode-escape') |
| 32 |
raises( UnicodeDecodeError, str,b'\\N','unicode-escape') |
| 33 |
assert str(b'\\N{SPACE}\\N{SPACE}','unicode-escape') == u" " |
| 34 |
assert str(b'\\N{SPACE}a\\N{SPACE}','unicode-escape') == u" a " |
| 35 |
assert b"\\N{foo}xx".decode("unicode-escape", "ignore") == u"xx" |
| 36 |
assert 1 <= len(u"\N{CJK UNIFIED IDEOGRAPH-20000}") <= 2 |
| 37 |
|
| 38 |
def test_literals(self): |
| 39 |
raises(SyntaxError, eval, 'u\'\\Uffffffff\'') |
| 40 |
|
| 41 |
def test_insecure_pickle(self): |
| 42 |
import _pickle as pickle |
| 43 |
insecure = [b"abc", b"2 + 2", |
| 44 |
|
| 45 |
b"'abc", |
| 46 |
b"'abc\"", |
| 47 |
b"'abc' ?", |
| 48 |
b"'\\'", |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
] |
| 53 |
for s in insecure: |
| 54 |
buf = b"S" + s + b"\012p0\012." |
| 55 |
raises ((ValueError, pickle.UnpicklingError), pickle.loads, buf) |
| 56 |
|
| 57 |
def test_unicodedecodeerror(self): |
| 58 |
assert str(UnicodeDecodeError( |
| 59 |
"ascii", b"g\xfcrk", 1, 2, "ouch")) == "'ascii' codec can't decode byte 0xfc in position 1: ouch" |
| 60 |
|
| 61 |
assert str(UnicodeDecodeError( |
| 62 |
"ascii", b"g\xfcrk", 1, 3, "ouch")) == "'ascii' codec can't decode bytes in position 1-2: ouch" |
| 63 |
|
| 64 |
def test_unicodedecodeerror_utf8(self): |
| 65 |
error = raises(UnicodeDecodeError, b'\xf6'.decode, "utf-8").value |
| 66 |
assert str(error) == "'utf-8' codec can't decode byte 0xf6 in position 0: invalid start byte" |
| 67 |
|
| 68 |
def test_unicodetranslateerror(self): |
| 69 |
import sys |
| 70 |
assert str(UnicodeTranslateError( |
| 71 |
"g\xfcrk", 1, 2, "ouch"))== "can't translate character '\\xfc' in position 1: ouch" |
| 72 |
|
| 73 |
assert str(UnicodeTranslateError( |
| 74 |
"g\u0100rk", 1, 2, "ouch"))== "can't translate character '\\u0100' in position 1: ouch" |
| 75 |
|
| 76 |
assert str(UnicodeTranslateError( |
| 77 |
"g\uffffrk", 1, 2, "ouch"))== "can't translate character '\\uffff' in position 1: ouch" |
| 78 |
|
| 79 |
if sys.maxunicode > 0xffff and len(chr(0x10000)) == 1: |
| 80 |
assert str(UnicodeTranslateError( |
| 81 |
"g\U00010000rk", 1, 2, "ouch"))== "can't translate character '\\U00010000' in position 1: ouch" |
| 82 |
|
| 83 |
assert str(UnicodeTranslateError( |
| 84 |
"g\xfcrk", 1, 3, "ouch"))=="can't translate characters in position 1-2: ouch" |
| 85 |
|
| 86 |
def test_unicodeencodeerror(self): |
| 87 |
import sys |
| 88 |
assert str(UnicodeEncodeError( |
| 89 |
"ascii", "g\xfcrk", 1, 2, "ouch"))=="'ascii' codec can't encode character '\\xfc' in position 1: ouch" |
| 90 |
|
| 91 |
assert str(UnicodeEncodeError( |
| 92 |
"ascii", "g\xfcrk", 1, 4, "ouch"))== "'ascii' codec can't encode characters in position 1-3: ouch" |
| 93 |
|
| 94 |
assert str(UnicodeEncodeError( |
| 95 |
"ascii", "\xfcx", 0, 1, "ouch"))=="'ascii' codec can't encode character '\\xfc' in position 0: ouch" |
| 96 |
|
| 97 |
assert str(UnicodeEncodeError( |
| 98 |
"ascii", "\u0100x", 0, 1, "ouch"))=="'ascii' codec can't encode character '\\u0100' in position 0: ouch" |
| 99 |
|
| 100 |
assert str(UnicodeEncodeError( |
| 101 |
"ascii", "\uffffx", 0, 1, "ouch"))=="'ascii' codec can't encode character '\\uffff' in position 0: ouch" |
| 102 |
if sys.maxunicode > 0xffff and len(chr(0x10000)) == 1: |
| 103 |
assert str(UnicodeEncodeError( |
| 104 |
"ascii", "\U00010000x", 0, 1, "ouch")) =="'ascii' codec can't encode character '\\U00010000' in position 0: ouch" |
| 105 |
|
| 106 |
def test_indexerror(self): |
| 107 |
import _codecs |
| 108 |
test = b"\\" |
| 109 |
raises (ValueError, _codecs.escape_decode, test) |
| 110 |
|
| 111 |
def test_charmap_decode(self): |
| 112 |
from _codecs import charmap_decode |
| 113 |
import sys |
| 114 |
assert charmap_decode(b'', 'strict', 'blablabla') == ('', 0) |
| 115 |
assert charmap_decode(b'xxx') == ('xxx', 3) |
| 116 |
res = charmap_decode(b'xxx', 'strict', {ord('x'): 'XX'}) |
| 117 |
assert res == ('XXXXXX', 3) |
| 118 |
map = tuple([chr(i) for i in range(256)]) |
| 119 |
assert charmap_decode(b'xxx\xff', 'strict', map) == ('xxx\xff', 4) |
| 120 |
|
| 121 |
exc = raises(TypeError, charmap_decode, b'\xff', "strict", {0xff: b'a'}) |
| 122 |
assert str(exc.value) == "character mapping must return integer, None or str" |
| 123 |
raises(TypeError, charmap_decode, b'\xff', "strict", {0xff: 0x110000}) |
| 124 |
assert (charmap_decode(b"\x00\x01\x02", "strict", |
| 125 |
{0: 0x10FFFF, 1: ord('b'), 2: ord('c')}) == |
| 126 |
(u"\U0010FFFFbc", 3)) |
| 127 |
assert (charmap_decode(b"\x00\x01\x02", "strict", |
| 128 |
{0: u'\U0010FFFF', 1: u'b', 2: u'c'}) == |
| 129 |
(u"\U0010FFFFbc", 3)) |
| 130 |
assert charmap_decode(b'\xff', "strict", {0xff: 0xd800}) == (u'\ud800', 1) |
| 131 |
|
| 132 |
def test_escape_decode(self): |
| 133 |
from _codecs import unicode_escape_decode as decode |
| 134 |
import sys |
| 135 |
if sys.version_info[0] < 3: |
| 136 |
assert decode('\\\x80') == (u'\\\x80', 2) |
| 137 |
else: |
| 138 |
assert decode('\\\x80') == (u'\\\xc2\x80', 3) |
| 139 |
|
| 140 |
def test_escape_decode_errors(self): |
| 141 |
from _codecs import escape_decode as decode |
| 142 |
raises(ValueError, decode, br"\x") |
| 143 |
raises(ValueError, decode, br"[\x]") |
| 144 |
assert decode(br"[\x]\x", "ignore") == (b"[]", 6) |
| 145 |
assert decode(br"[\x]\x", "replace") == (b"[?]?", 6) |
| 146 |
raises(ValueError, decode, br"\x0") |
| 147 |
raises(ValueError, decode, br"[\x0]") |
| 148 |
assert decode(br"[\x0]\x0", "ignore") == (b"[]", 8) |
| 149 |
assert decode(br"[\x0]\x0", "replace") == (b"[?]?", 8) |
| 150 |
|
| 151 |
def test_escape_warning(self): |
| 152 |
from _codecs import escape_decode |
| 153 |
import warnings |
| 154 |
with warnings.catch_warnings(record=True) as w: |
| 155 |
warnings.simplefilter("error") |
| 156 |
with raises(DeprecationWarning): |
| 157 |
escape_decode("a\\zb") |
| 158 |
with raises(DeprecationWarning): |
| 159 |
escape_decode(b"\\400") |
| 160 |
|
| 161 |
|
| 162 |
def test_unicode_escape(self): |
| 163 |
from _codecs import unicode_escape_encode, unicode_escape_decode |
| 164 |
assert unicode_escape_encode('abc') == ('abc'.encode('unicode_escape'), 3) |
| 165 |
assert unicode_escape_decode(b'abc') == (b'abc'.decode('unicode_escape'), 3) |
| 166 |
lgt = 12 |
| 167 |
assert unicode_escape_decode(b'\\x61\\x62\\x63') == ('abc', lgt) |
| 168 |
|
| 169 |
def test_unicode_replace(self): |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
FFFD = '\ufffd' |
| 176 |
sequences = [ |
| 177 |
|
| 178 |
(b'\x80', FFFD), |
| 179 |
(b'\x80\x80', FFFD*2), |
| 180 |
(b'\xc0', FFFD), |
| 181 |
(b'\xc0\xc0', FFFD*2), |
| 182 |
(b'\xc1', FFFD), |
| 183 |
(b'\xc1\xc0', FFFD*2), |
| 184 |
(b'\xc0\xc1', FFFD*2), |
| 185 |
|
| 186 |
(b'\xc2', FFFD), |
| 187 |
(b'\xc2\xc2', FFFD*2), |
| 188 |
(b'\xc2\xc2\xc2', FFFD*3), |
| 189 |
(b'\xc2\x41', FFFD+'A'), |
| 190 |
|
| 191 |
(b'\xe1', FFFD), |
| 192 |
(b'\xe1\xe1', FFFD*2), |
| 193 |
(b'\xe1\xe1\xe1', FFFD*3), |
| 194 |
(b'\xe1\xe1\xe1\xe1', FFFD*4), |
| 195 |
(b'\xe1\x80', FFFD), |
| 196 |
(b'\xe1\x41', FFFD+'A'), |
| 197 |
(b'\xe1\x41\x80', FFFD+'A'+FFFD), |
| 198 |
(b'\xe1\x41\x41', FFFD+'AA'), |
| 199 |
(b'\xe1\x80\x41', FFFD+'A'), |
| 200 |
(b'\xe1\x80\xe1\x41', FFFD*2+'A'), |
| 201 |
(b'\xe1\x41\xe1\x80', FFFD+'A'+FFFD), |
| 202 |
|
| 203 |
(b'\xf1', FFFD), |
| 204 |
(b'\xf1\xf1', FFFD*2), |
| 205 |
(b'\xf1\xf1\xf1', FFFD*3), |
| 206 |
(b'\xf1\xf1\xf1\xf1', FFFD*4), |
| 207 |
(b'\xf1\xf1\xf1\xf1\xf1', FFFD*5), |
| 208 |
(b'\xf1\x80', FFFD), |
| 209 |
(b'\xf1\x80\x80', FFFD), |
| 210 |
(b'\xf1\x80\x41', FFFD+'A'), |
| 211 |
(b'\xf1\x80\x41\x41', FFFD+'AA'), |
| 212 |
(b'\xf1\x80\x80\x41', FFFD+'A'), |
| 213 |
(b'\xf1\x41\x80', FFFD+'A'+FFFD), |
| 214 |
(b'\xf1\x41\x80\x80', FFFD+'A'+FFFD*2), |
| 215 |
(b'\xf1\x41\x80\x41', FFFD+'A'+FFFD+'A'), |
| 216 |
(b'\xf1\x41\x41\x80', FFFD+'AA'+FFFD), |
| 217 |
(b'\xf1\x41\xf1\x80', FFFD+'A'+FFFD), |
| 218 |
(b'\xf1\x41\x80\xf1', FFFD+'A'+FFFD*2), |
| 219 |
(b'\xf1\xf1\x80\x41', FFFD*2+'A'), |
| 220 |
(b'\xf1\x41\xf1\xf1', FFFD+'A'+FFFD*2), |
| 221 |
|
| 222 |
(b'\xf5', FFFD), |
| 223 |
(b'\xf5\xf5', FFFD*2), |
| 224 |
(b'\xf5\x80', FFFD*2), |
| 225 |
(b'\xf5\x80\x80', FFFD*3), |
| 226 |
(b'\xf5\x80\x80\x80', FFFD*4), |
| 227 |
(b'\xf5\x80\x41', FFFD*2+'A'), |
| 228 |
(b'\xf5\x80\x41\xf5', FFFD*2+'A'+FFFD), |
| 229 |
(b'\xf5\x41\x80\x80\x41', FFFD+'A'+FFFD*2+'A'), |
| 230 |
|
| 231 |
(b'\xf8', FFFD), |
| 232 |
(b'\xf8\xf8', FFFD*2), |
| 233 |
(b'\xf8\x80', FFFD*2), |
| 234 |
(b'\xf8\x80\x41', FFFD*2 + 'A'), |
| 235 |
(b'\xf8\x80\x80\x80\x80', FFFD*5), |
| 236 |
|
| 237 |
(b'\xfc', FFFD), |
| 238 |
(b'\xfc\xfc', FFFD*2), |
| 239 |
(b'\xfc\x80\x80', FFFD*3), |
| 240 |
(b'\xfc\x80\x80\x80\x80\x80', FFFD*6), |
| 241 |
|
| 242 |
(b'\xfe', FFFD), |
| 243 |
(b'\xfe\x80\x80', FFFD*3), |
| 244 |
|
| 245 |
(b'\xf1\x80\x41\x42\x43', '\ufffd\x41\x42\x43'), |
| 246 |
(b'\xf1\x80\xff\x42\x43', '\ufffd\ufffd\x42\x43'), |
| 247 |
(b'\xf1\x80\xc2\x81\x43', '\ufffd\x81\x43'), |
| 248 |
(b'\x61\xF1\x80\x80\xE1\x80\xC2\x62\x80\x63\x80\xBF\x64', |
| 249 |
'\x61\uFFFD\uFFFD\uFFFD\x62\uFFFD\x63\uFFFD\uFFFD\x64'), |
| 250 |
] |
| 251 |
for (seq, res) in sequences: |
| 252 |
raises(UnicodeDecodeError, seq.decode, 'utf-8', 'strict') |
| 253 |
uni = seq.decode('utf-8', 'replace') |
| 254 |
assert uni == res |
| 255 |
uni = (seq+b'b').decode('utf-8', 'replace') |
| 256 |
assert uni == res+'b' |
| 257 |
uni = seq.decode('utf-8', 'ignore') |
| 258 |
assert uni == res.replace('\uFFFD', '') |
| 259 |
|
| 260 |
def test_unexpected_end_of_data(self): |
| 261 |
""" |
| 262 |
Test that an 'unexpected end of data' error is raised when the string |
| 263 |
ends after a start byte of a 2-, 3-, or 4-bytes sequence without having |
| 264 |
enough continuation bytes. The incomplete sequence is replaced with a |
| 265 |
single U+FFFD when errors='replace'. |
| 266 |
E.g. in the sequence <F3 80 80>, F3 is the start byte of a 4-bytes |
| 267 |
sequence, but it's followed by only 2 valid continuation bytes and the |
| 268 |
last continuation bytes is missing. |
| 269 |
Note: the continuation bytes must be all valid, if one of them is |
| 270 |
invalid another error will be raised. |
| 271 |
""" |
| 272 |
sequences = [ |
| 273 |
'C2', 'DF', |
| 274 |
'E0 A0', 'E0 BF', 'E1 80', 'E1 BF', 'EC 80', 'EC BF', |
| 275 |
'ED 80', 'ED 9F', 'EE 80', 'EE BF', 'EF 80', 'EF BF', |
| 276 |
'F0 90', 'F0 BF', 'F0 90 80', 'F0 90 BF', 'F0 BF 80', 'F0 BF BF', |
| 277 |
'F1 80', 'F1 BF', 'F1 80 80', 'F1 80 BF', 'F1 BF 80', 'F1 BF BF', |
| 278 |
'F3 80', 'F3 BF', 'F3 80 80', 'F3 80 BF', 'F3 BF 80', 'F3 BF BF', |
| 279 |
'F4 80', 'F4 8F', 'F4 80 80', 'F4 80 BF', 'F4 8F 80', 'F4 8F BF' |
| 280 |
] |
| 281 |
for seq in sequences: |
| 282 |
bseq = bytes(int(c, 16) for c in seq.split()) |
| 283 |
exc = raises(UnicodeDecodeError, bseq.decode, 'utf-8') |
| 284 |
assert 'unexpected end of data' in str(exc.value) |
| 285 |
useq = bseq.decode('utf-8', 'replace') |
| 286 |
assert useq == u'\ufffd', (bseq, useq) |
| 287 |
assert ((b'aaaa' + bseq + b'bbbb').decode('utf-8', 'replace') == |
| 288 |
u'aaaa\ufffdbbbb') |
| 289 |
assert bseq.decode('utf-8', 'ignore') == '' |
| 290 |
assert ((b'aaaa' + bseq + b'bbbb').decode('utf-8', 'ignore') == |
| 291 |
u'aaaabbbb') |
| 292 |
|
| 293 |
def test_invalid_cb_for_3bytes_seq(self): |
| 294 |
""" |
| 295 |
Test that an 'invalid continuation byte' error is raised when the |
| 296 |
continuation byte(s) of a 3-bytes sequence are invalid. When |
| 297 |
errors='replace', if the first continuation byte is valid, the first |
| 298 |
two bytes (start byte + 1st cb) are replaced by a single U+FFFD and the |
| 299 |
third byte is handled separately, otherwise only the start byte is |
| 300 |
replaced with a U+FFFD and the other continuation bytes are handled |
| 301 |
separately. |
| 302 |
E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 303 |
sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 304 |
because it's the ASCII letter 'A'. |
| 305 |
Note: when the start byte is E0 or ED, the valid ranges for the first |
| 306 |
continuation byte are limited to A0..BF and 80..9F respectively. |
| 307 |
Python 2 used to consider all the bytes in range 80..BF valid when the |
| 308 |
start byte was ED. This is fixed in Python 3. |
| 309 |
""" |
| 310 |
FFFD = '\ufffd' |
| 311 |
FFFDx2 = FFFD * 2 |
| 312 |
sequences = [ |
| 313 |
('E0 00', FFFD+'\x00'), ('E0 7F', FFFD+'\x7f'), ('E0 80', FFFDx2), |
| 314 |
('E0 9F', FFFDx2), ('E0 C0', FFFDx2), ('E0 FF', FFFDx2), |
| 315 |
('E0 A0 00', FFFD+'\x00'), ('E0 A0 7F', FFFD+'\x7f'), |
| 316 |
('E0 A0 C0', FFFDx2), ('E0 A0 FF', FFFDx2), |
| 317 |
('E0 BF 00', FFFD+'\x00'), ('E0 BF 7F', FFFD+'\x7f'), |
| 318 |
('E0 BF C0', FFFDx2), ('E0 BF FF', FFFDx2), ('E1 00', FFFD+'\x00'), |
| 319 |
('E1 7F', FFFD+'\x7f'), ('E1 C0', FFFDx2), ('E1 FF', FFFDx2), |
| 320 |
('E1 80 00', FFFD+'\x00'), ('E1 80 7F', FFFD+'\x7f'), |
| 321 |
('E1 80 C0', FFFDx2), ('E1 80 FF', FFFDx2), |
| 322 |
('E1 BF 00', FFFD+'\x00'), ('E1 BF 7F', FFFD+'\x7f'), |
| 323 |
('E1 BF C0', FFFDx2), ('E1 BF FF', FFFDx2), ('EC 00', FFFD+'\x00'), |
| 324 |
('EC 7F', FFFD+'\x7f'), ('EC C0', FFFDx2), ('EC FF', FFFDx2), |
| 325 |
('EC 80 00', FFFD+'\x00'), ('EC 80 7F', FFFD+'\x7f'), |
| 326 |
('EC 80 C0', FFFDx2), ('EC 80 FF', FFFDx2), |
| 327 |
('EC BF 00', FFFD+'\x00'), ('EC BF 7F', FFFD+'\x7f'), |
| 328 |
('EC BF C0', FFFDx2), ('EC BF FF', FFFDx2), ('ED 00', FFFD+'\x00'), |
| 329 |
('ED 7F', FFFD+'\x7f'), |
| 330 |
('ED A0', FFFDx2), ('ED BF', FFFDx2), |
| 331 |
('ED C0', FFFDx2), ('ED FF', FFFDx2), ('ED 80 00', FFFD+'\x00'), |
| 332 |
('ED 80 7F', FFFD+'\x7f'), ('ED 80 C0', FFFDx2), |
| 333 |
('ED 80 FF', FFFDx2), ('ED 9F 00', FFFD+'\x00'), |
| 334 |
('ED 9F 7F', FFFD+'\x7f'), ('ED 9F C0', FFFDx2), |
| 335 |
('ED 9F FF', FFFDx2), ('EE 00', FFFD+'\x00'), |
| 336 |
('EE 7F', FFFD+'\x7f'), ('EE C0', FFFDx2), ('EE FF', FFFDx2), |
| 337 |
('EE 80 00', FFFD+'\x00'), ('EE 80 7F', FFFD+'\x7f'), |
| 338 |
('EE 80 C0', FFFDx2), ('EE 80 FF', FFFDx2), |
| 339 |
('EE BF 00', FFFD+'\x00'), ('EE BF 7F', FFFD+'\x7f'), |
| 340 |
('EE BF C0', FFFDx2), ('EE BF FF', FFFDx2), ('EF 00', FFFD+'\x00'), |
| 341 |
('EF 7F', FFFD+'\x7f'), ('EF C0', FFFDx2), ('EF FF', FFFDx2), |
| 342 |
('EF 80 00', FFFD+'\x00'), ('EF 80 7F', FFFD+'\x7f'), |
| 343 |
('EF 80 C0', FFFDx2), ('EF 80 FF', FFFDx2), |
| 344 |
('EF BF 00', FFFD+'\x00'), ('EF BF 7F', FFFD+'\x7f'), |
| 345 |
('EF BF C0', FFFDx2), ('EF BF FF', FFFDx2), |
| 346 |
] |
| 347 |
err = 'invalid continuation byte' |
| 348 |
for s, res in sequences: |
| 349 |
seq = bytes(int(c, 16) for c in s.split()) |
| 350 |
exc = raises(UnicodeDecodeError, seq.decode, 'utf-8') |
| 351 |
assert err in str(exc.value) |
| 352 |
assert seq.decode('utf-8', 'replace') == res |
| 353 |
assert ((b'aaaa' + seq + b'bbbb').decode('utf-8', 'replace') == |
| 354 |
'aaaa' + res + 'bbbb') |
| 355 |
res = res.replace('\ufffd', '') |
| 356 |
assert seq.decode('utf-8', 'ignore') == res |
| 357 |
assert((b'aaaa' + seq + b'bbbb').decode('utf-8', 'ignore') == |
| 358 |
'aaaa' + res + 'bbbb') |
| 359 |
|
| 360 |
def test_invalid_cb_for_4bytes_seq(self): |
| 361 |
""" |
| 362 |
Test that an 'invalid continuation byte' error is raised when the |
| 363 |
continuation byte(s) of a 4-bytes sequence are invalid. When |
| 364 |
errors='replace',the start byte and all the following valid |
| 365 |
continuation bytes are replaced with a single U+FFFD, and all the bytes |
| 366 |
starting from the first invalid continuation bytes (included) are |
| 367 |
handled separately. |
| 368 |
E.g. in the sequence <E1 80 41>, E1 is the start byte of a 3-bytes |
| 369 |
sequence, 80 is a valid continuation byte, but 41 is not a valid cb |
| 370 |
because it's the ASCII letter 'A'. |
| 371 |
Note: when the start byte is E0 or ED, the valid ranges for the first |
| 372 |
continuation byte are limited to A0..BF and 80..9F respectively. |
| 373 |
However, when the start byte is ED, Python 2 considers all the bytes |
| 374 |
in range 80..BF valid. This is fixed in Python 3. |
| 375 |
""" |
| 376 |
FFFD = '\ufffd' |
| 377 |
FFFDx2 = FFFD * 2 |
| 378 |
sequences = [ |
| 379 |
('F0 00', FFFD+'\x00'), ('F0 7F', FFFD+'\x7f'), ('F0 80', FFFDx2), |
| 380 |
('F0 8F', FFFDx2), ('F0 C0', FFFDx2), ('F0 FF', FFFDx2), |
| 381 |
('F0 90 00', FFFD+'\x00'), ('F0 90 7F', FFFD+'\x7f'), |
| 382 |
('F0 90 C0', FFFDx2), ('F0 90 FF', FFFDx2), |
| 383 |
('F0 BF 00', FFFD+'\x00'), ('F0 BF 7F', FFFD+'\x7f'), |
| 384 |
('F0 BF C0', FFFDx2), ('F0 BF FF', FFFDx2), |
| 385 |
('F0 90 80 00', FFFD+'\x00'), ('F0 90 80 7F', FFFD+'\x7f'), |
| 386 |
('F0 90 80 C0', FFFDx2), ('F0 90 80 FF', FFFDx2), |
| 387 |
('F0 90 BF 00', FFFD+'\x00'), ('F0 90 BF 7F', FFFD+'\x7f'), |
| 388 |
('F0 90 BF C0', FFFDx2), ('F0 90 BF FF', FFFDx2), |
| 389 |
('F0 BF 80 00', FFFD+'\x00'), ('F0 BF 80 7F', FFFD+'\x7f'), |
| 390 |
('F0 BF 80 C0', FFFDx2), ('F0 BF 80 FF', FFFDx2), |
| 391 |
('F0 BF BF 00', FFFD+'\x00'), ('F0 BF BF 7F', FFFD+'\x7f'), |
| 392 |
('F0 BF BF C0', FFFDx2), ('F0 BF BF FF', FFFDx2), |
| 393 |
('F1 00', FFFD+'\x00'), ('F1 7F', FFFD+'\x7f'), ('F1 C0', FFFDx2), |
| 394 |
('F1 FF', FFFDx2), ('F1 80 00', FFFD+'\x00'), |
| 395 |
('F1 80 7F', FFFD+'\x7f'), ('F1 80 C0', FFFDx2), |
| 396 |
('F1 80 FF', FFFDx2), ('F1 BF 00', FFFD+'\x00'), |
| 397 |
('F1 BF 7F', FFFD+'\x7f'), ('F1 BF C0', FFFDx2), |
| 398 |
('F1 BF FF', FFFDx2), ('F1 80 80 00', FFFD+'\x00'), |
| 399 |
('F1 80 80 7F', FFFD+'\x7f'), ('F1 80 80 C0', FFFDx2), |
| 400 |
('F1 80 80 FF', FFFDx2), ('F1 80 BF 00', FFFD+'\x00'), |
| 401 |
('F1 80 BF 7F', FFFD+'\x7f'), ('F1 80 BF C0', FFFDx2), |
| 402 |
('F1 80 BF FF', FFFDx2), ('F1 BF 80 00', FFFD+'\x00'), |
| 403 |
('F1 BF 80 7F', FFFD+'\x7f'), ('F1 BF 80 C0', FFFDx2), |
| 404 |
('F1 BF 80 FF', FFFDx2), ('F1 BF BF 00', FFFD+'\x00'), |
| 405 |
('F1 BF BF 7F', FFFD+'\x7f'), ('F1 BF BF C0', FFFDx2), |
| 406 |
('F1 BF BF FF', FFFDx2), ('F3 00', FFFD+'\x00'), |
| 407 |
('F3 7F', FFFD+'\x7f'), ('F3 C0', FFFDx2), ('F3 FF', FFFDx2), |
| 408 |
('F3 80 00', FFFD+'\x00'), ('F3 80 7F', FFFD+'\x7f'), |
| 409 |
('F3 80 C0', FFFDx2), ('F3 80 FF', FFFDx2), |
| 410 |
('F3 BF 00', FFFD+'\x00'), ('F3 BF 7F', FFFD+'\x7f'), |
| 411 |
('F3 BF C0', FFFDx2), ('F3 BF FF', FFFDx2), |
| 412 |
('F3 80 80 00', FFFD+'\x00'), ('F3 80 80 7F', FFFD+'\x7f'), |
| 413 |
('F3 80 80 C0', FFFDx2), ('F3 80 80 FF', FFFDx2), |
| 414 |
('F3 80 BF 00', FFFD+'\x00'), ('F3 80 BF 7F', FFFD+'\x7f'), |
| 415 |
('F3 80 BF C0', FFFDx2), ('F3 80 BF FF', FFFDx2), |
| 416 |
('F3 BF 80 00', FFFD+'\x00'), ('F3 BF 80 7F', FFFD+'\x7f'), |
| 417 |
('F3 BF 80 C0', FFFDx2), ('F3 BF 80 FF', FFFDx2), |
| 418 |
('F3 BF BF 00', FFFD+'\x00'), ('F3 BF BF 7F', FFFD+'\x7f'), |
| 419 |
('F3 BF BF C0', FFFDx2), ('F3 BF BF FF', FFFDx2), |
| 420 |
('F4 00', FFFD+'\x00'), ('F4 7F', FFFD+'\x7f'), ('F4 90', FFFDx2), |
| 421 |
('F4 BF', FFFDx2), ('F4 C0', FFFDx2), ('F4 FF', FFFDx2), |
| 422 |
('F4 80 00', FFFD+'\x00'), ('F4 80 7F', FFFD+'\x7f'), |
| 423 |
('F4 80 C0', FFFDx2), ('F4 80 FF', FFFDx2), |
| 424 |
('F4 8F 00', FFFD+'\x00'), ('F4 8F 7F', FFFD+'\x7f'), |
| 425 |
('F4 8F C0', FFFDx2), ('F4 8F FF', FFFDx2), |
| 426 |
('F4 80 80 00', FFFD+'\x00'), ('F4 80 80 7F', FFFD+'\x7f'), |
| 427 |
('F4 80 80 C0', FFFDx2), ('F4 80 80 FF', FFFDx2), |
| 428 |
('F4 80 BF 00', FFFD+'\x00'), ('F4 80 BF 7F', FFFD+'\x7f'), |
| 429 |
('F4 80 BF C0', FFFDx2), ('F4 80 BF FF', FFFDx2), |
| 430 |
('F4 8F 80 00', FFFD+'\x00'), ('F4 8F 80 7F', FFFD+'\x7f'), |
| 431 |
('F4 8F 80 C0', FFFDx2), ('F4 8F 80 FF', FFFDx2), |
| 432 |
('F4 8F BF 00', FFFD+'\x00'), ('F4 8F BF 7F', FFFD+'\x7f'), |
| 433 |
('F4 8F BF C0', FFFDx2), ('F4 8F BF FF', FFFDx2) |
| 434 |
] |
| 435 |
err = 'invalid continuation byte' |
| 436 |
for s, res in sequences: |
| 437 |
seq = bytes(int(c, 16) for c in s.split()) |
| 438 |
exc = raises(UnicodeDecodeError, seq.decode, 'utf-8') |
| 439 |
assert err in str(exc.value) |
| 440 |
assert seq.decode('utf-8', 'replace') == res |
| 441 |
assert ((b'aaaa' + seq + b'bbbb').decode('utf-8', 'replace') == |
| 442 |
'aaaa' + res + 'bbbb') |
| 443 |
res = res.replace('\ufffd', '') |
| 444 |
assert seq.decode('utf-8', 'ignore') == res |
| 445 |
assert((b'aaaa' + seq + b'bbbb').decode('utf-8', 'ignore') == |
| 446 |
'aaaa' + res + 'bbbb') |
| 447 |
|
| 448 |
@pytest.mark.skipif(sys.platform != 'win32', reason='win32-only') |
| 449 |
class AppTestCodePage: |
| 450 |
spaceconfig = { |
| 451 |
} |
| 452 |
|
| 453 |
def test_code_pages(self): |
| 454 |
import _codecs as codecs |
| 455 |
def check_decode(cp, test): |
| 456 |
raw, errors, expected = test |
| 457 |
if expected is not None: |
| 458 |
try: |
| 459 |
decoded = codecs.code_page_decode(cp, raw, errors, True) |
| 460 |
except UnicodeDecodeError as err: |
| 461 |
assert False, ('Unable to decode %a from "cp%s" with ' |
| 462 |
'errors=%r: %s' % (raw, cp, errors, err)) |
| 463 |
assert decoded[0] == expected, ('%a.decode("cp%s", %r)=%a != %a' |
| 464 |
% (raw, cp, errors, decoded[0], expected)) |
| 465 |
assert decoded[1] >= 0 |
| 466 |
assert decoded[1] <= len(raw) |
| 467 |
else: |
| 468 |
raises(UnicodeDecodeError, |
| 469 |
codecs.code_page_decode, cp, raw, errors, True) |
| 470 |
|
| 471 |
def check_encode(cp, test): |
| 472 |
text, errors, expected = test |
| 473 |
if expected is not None: |
| 474 |
try: |
| 475 |
encoded = codecs.code_page_encode(cp, text, errors) |
| 476 |
except UnicodeEncodeError as err: |
| 477 |
assert False, ('Unable to encode %a to "cp%s" with ' |
| 478 |
'errors=%r: %s' % (text, cp, errors, err)) |
| 479 |
assert encoded[0] == expected, ('%a.encode("cp%s", %r)=%a != %a' |
| 480 |
% (text, cp, errors, encoded[0], expected)) |
| 481 |
assert encoded[1] == len(text) |
| 482 |
else: |
| 483 |
raises(UnicodeEncodeError, |
| 484 |
codecs.code_page_encode, cp, text, errors) |
| 485 |
|
| 486 |
for test in ( |
| 487 |
(u'abc', 'strict', b'abc'), |
| 488 |
(u'\uff44\u9a3e', 'strict', b'\x82\x84\xe9\x80'), |
| 489 |
|
| 490 |
(u'\xff', 'strict', None), |
| 491 |
(u'[\xff]', 'ignore', b'[]'), |
| 492 |
(u'[\xff]', 'replace', b'[y]'), |
| 493 |
(u'[\u20ac]', 'replace', b'[?]'), |
| 494 |
(u'[\xff]', 'backslashreplace', b'[\\xff]'), |
| 495 |
(u'[\xff]', 'namereplace', |
| 496 |
b'[\\N{LATIN SMALL LETTER Y WITH DIAERESIS}]'), |
| 497 |
(u'[\xff]', 'xmlcharrefreplace', b'[ÿ]'), |
| 498 |
(u'\udcff', 'strict', None), |
| 499 |
(u'[\udcff]', 'surrogateescape', b'[\xff]'), |
| 500 |
(u'[\udcff]', 'surrogatepass', None), |
| 501 |
): |
| 502 |
check_encode(932, test) |
| 503 |
|
| 504 |
for test in ( |
| 505 |
(b'abc', 'strict', u'abc'), |
| 506 |
(b'\x82\x84\xe9\x80', 'strict', u'\uff44\u9a3e'), |
| 507 |
|
| 508 |
(b'[\xff]', 'strict', None), |
| 509 |
(b'[\xff]', 'ignore', u'[]'), |
| 510 |
(b'[\xff]', 'replace', u'[\ufffd]'), |
| 511 |
(b'[\xff]', 'backslashreplace', u'[\\xff]'), |
| 512 |
(b'[\xff]', 'surrogateescape', u'[\udcff]'), |
| 513 |
(b'[\xff]', 'surrogatepass', None), |
| 514 |
(b'\x81\x00abc', 'strict', None), |
| 515 |
(b'\x81\x00abc', 'ignore', u'\x00abc'), |
| 516 |
(b'\x81\x00abc', 'replace', u'\ufffd\x00abc'), |
| 517 |
(b'\x81\x00abc', 'backslashreplace', u'\\x81\x00abc'), |
| 518 |
): |
| 519 |
check_decode(932, test) |
| 520 |
|
| 521 |
for test in ( |
| 522 |
(u'abc', 'strict', b'abc'), |
| 523 |
(u'\xe9\u20ac', 'strict', b'\xe9\x80'), |
| 524 |
(u'\xff', 'strict', b'\xff'), |
| 525 |
|
| 526 |
(u'\u0141', 'strict', None), |
| 527 |
(u'\u0141', 'ignore', b''), |
| 528 |
(u'\u0141', 'replace', b'L'), |
| 529 |
(u'\udc98', 'surrogateescape', b'\x98'), |
| 530 |
(u'\udc98', 'surrogatepass', None), |
| 531 |
): |
| 532 |
check_encode(1252, test) |
| 533 |
|
| 534 |
for test in ( |
| 535 |
(b'abc', 'strict', u'abc'), |
| 536 |
(b'\xe9\x80', 'strict', u'\xe9\u20ac'), |
| 537 |
(b'\xff', 'strict', u'\xff'), |
| 538 |
): |
| 539 |
check_decode(1252, test) |
| 540 |
|
| 541 |
def test_encode_65001(self): |
| 542 |
tests = [ |
| 543 |
('abc', 'strict', b'abc'), |
| 544 |
('\xe9\u20ac', 'strict', b'\xc3\xa9\xe2\x82\xac'), |
| 545 |
('\U0010ffff', 'strict', b'\xf4\x8f\xbf\xbf'), |
| 546 |
('\udc80', 'strict', None), |
| 547 |
('\udc80', 'ignore', b''), |
| 548 |
('\udc80', 'replace', b'?'), |
| 549 |
('\udc80', 'backslashreplace', b'\\udc80'), |
| 550 |
('\udc80', 'namereplace', b'\\udc80'), |
| 551 |
('\udc80', 'surrogatepass', b'\xed\xb2\x80'), |
| 552 |
] |
| 553 |
for text, errors, expected in tests: |
| 554 |
if expected is not None: |
| 555 |
try: |
| 556 |
encoded = text.encode('cp65001', errors) |
| 557 |
except UnicodeEncodeError as err: |
| 558 |
assert False, ('Unable to encode %a to cp65001 with ' |
| 559 |
'errors=%r: %s' % (text, errors, err)) |
| 560 |
assert encoded ==expected, ('%a.encode("cp65001", %r)=%a != %a' |
| 561 |
% (text, errors, encoded, expected)) |
| 562 |
else: |
| 563 |
raises(UnicodeEncodeError, text.encode, "cp65001", errors) |
| 564 |
|
| 565 |
def test_decode_65001(self): |
| 566 |
tests = [ |
| 567 |
(b'abc', 'strict', 'abc'), |
| 568 |
(b'\xc3\xa9\xe2\x82\xac', 'strict', '\xe9\u20ac'), |
| 569 |
(b'\xf4\x8f\xbf\xbf', 'strict', '\U0010ffff'), |
| 570 |
(b'\xef\xbf\xbd', 'strict', '\ufffd'), |
| 571 |
(b'[\xc3\xa9]', 'strict', '[\xe9]'), |
| 572 |
|
| 573 |
(b'[\xff]', 'strict', None), |
| 574 |
(b'[\xff]', 'ignore', '[]'), |
| 575 |
(b'[\xff]', 'replace', '[\ufffd]'), |
| 576 |
(b'[\xff]', 'surrogateescape', '[\udcff]'), |
| 577 |
(b'[\xed\xb2\x80]', 'strict', None), |
| 578 |
(b'[\xed\xb2\x80]', 'ignore', '[]'), |
| 579 |
(b'[\xed\xb2\x80]', 'replace', '[\ufffd\ufffd\ufffd]'), |
| 580 |
] |
| 581 |
for raw, errors, expected in tests: |
| 582 |
if expected is not None: |
| 583 |
try: |
| 584 |
decoded = raw.decode('cp65001', errors) |
| 585 |
except UnicodeDecodeError as err: |
| 586 |
assert False, ('Unable to decode %a from cp65001 with ' |
| 587 |
'errors=%r: %s' % (raw, errors, err)) |
| 588 |
assert decoded == expected, ('%a.decode("cp65001", %r)=%a != %a' |
| 589 |
% (raw, errors, decoded, expected)) |
| 590 |
else: |
| 591 |
raises(UnicodeDecodeError, raw.decode, 'cp65001', errors) |
| 592 |
|
| 593 |
|
| 594 |
class AppTestPartialEvaluation: |
| 595 |
spaceconfig = dict(usemodules=['array',]) |
| 596 |
|
| 597 |
def setup_class(cls): |
| 598 |
cls.w_appdirect = cls.space.wrap(cls.runappdirect) |
| 599 |
|
| 600 |
def test_partial_utf8(self): |
| 601 |
import _codecs |
| 602 |
check_partial = [ |
| 603 |
u"\x00", |
| 604 |
u"\x00", |
| 605 |
u"\x00\xff", |
| 606 |
u"\x00\xff", |
| 607 |
u"\x00\xff\u07ff", |
| 608 |
u"\x00\xff\u07ff", |
| 609 |
u"\x00\xff\u07ff", |
| 610 |
u"\x00\xff\u07ff\u0800", |
| 611 |
u"\x00\xff\u07ff\u0800", |
| 612 |
u"\x00\xff\u07ff\u0800", |
| 613 |
u"\x00\xff\u07ff\u0800\uffff", |
| 614 |
u"\x00\xff\u07ff\u0800\uffff", |
| 615 |
u"\x00\xff\u07ff\u0800\uffff", |
| 616 |
u"\x00\xff\u07ff\u0800\uffff", |
| 617 |
u"\x00\xff\u07ff\u0800\uffff\U00010000", |
| 618 |
] |
| 619 |
|
| 620 |
buf = b'' |
| 621 |
result = u"" |
| 622 |
uval = u"\x00\xff\u07ff\u0800\uffff\U00010000" |
| 623 |
for (c, partialresult) in zip(uval.encode('utf-8'), check_partial): |
| 624 |
buf += bytes([c]) |
| 625 |
|
| 626 |
val, lgt = _codecs.utf_8_decode(buf, 'strict', False) |
| 627 |
if lgt >0 : |
| 628 |
buf = b'' |
| 629 |
result += val |
| 630 |
assert result == partialresult |
| 631 |
|
| 632 |
def test_partial_utf16(self): |
| 633 |
import _codecs |
| 634 |
check_partial = [ |
| 635 |
u"", |
| 636 |
u"", |
| 637 |
u"", |
| 638 |
u"\x00", |
| 639 |
u"\x00", |
| 640 |
u"\x00\xff", |
| 641 |
u"\x00\xff", |
| 642 |
u"\x00\xff\u0100", |
| 643 |
u"\x00\xff\u0100", |
| 644 |
u"\x00\xff\u0100\uffff", |
| 645 |
u"\x00\xff\u0100\uffff", |
| 646 |
u"\x00\xff\u0100\uffff", |
| 647 |
u"\x00\xff\u0100\uffff", |
| 648 |
u"\x00\xff\u0100\uffff\U00010000", |
| 649 |
] |
| 650 |
buf = b'' |
| 651 |
result = u"" |
| 652 |
uval = u"\x00\xff\u0100\uffff\U00010000" |
| 653 |
for (c, partialresult) in zip(uval.encode('utf-16'), check_partial): |
| 654 |
buf += bytes([c]) |
| 655 |
res = _codecs.utf_16_decode(buf, 'strict', False) |
| 656 |
if res[1] >0 : |
| 657 |
buf = b'' |
| 658 |
result += res[0] |
| 659 |
assert result == partialresult |
| 660 |
|
| 661 |
def test_bug1098990_a(self): |
| 662 |
import codecs, io |
| 663 |
self.encoding = 'utf-8' |
| 664 |
s1 = u"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\r\n" |
| 665 |
s2 = u"offending line: ladfj askldfj klasdj fskla dfzaskdj fasklfj laskd fjasklfzzzzaa%whereisthis!!!\r\n" |
| 666 |
s3 = u"next line.\r\n" |
| 667 |
|
| 668 |
s = (s1+s2+s3).encode(self.encoding) |
| 669 |
stream = io.BytesIO(s) |
| 670 |
reader = codecs.getreader(self.encoding)(stream) |
| 671 |
assert reader.readline() == s1 |
| 672 |
assert reader.readline() == s2 |
| 673 |
assert reader.readline() == s3 |
| 674 |
assert reader.readline() == u"" |
| 675 |
|
| 676 |
def test_bug1098990_b(self): |
| 677 |
import codecs, io |
| 678 |
self.encoding = 'utf-8' |
| 679 |
s1 = u"aaaaaaaaaaaaaaaaaaaaaaaa\r\n" |
| 680 |
s2 = u"bbbbbbbbbbbbbbbbbbbbbbbb\r\n" |
| 681 |
s3 = u"stillokay:bbbbxx\r\n" |
| 682 |
s4 = u"broken!!!!badbad\r\n" |
| 683 |
s5 = u"againokay.\r\n" |
| 684 |
|
| 685 |
s = (s1+s2+s3+s4+s5).encode(self.encoding) |
| 686 |
stream = io.BytesIO(s) |
| 687 |
reader = codecs.getreader(self.encoding)(stream) |
| 688 |
assert reader.readline() == s1 |
| 689 |
assert reader.readline() == s2 |
| 690 |
assert reader.readline() == s3 |
| 691 |
assert reader.readline() == s4 |
| 692 |
assert reader.readline() == s5 |
| 693 |
assert reader.readline() == u"" |
| 694 |
|
| 695 |
def test_seek_utf16le(self): |
| 696 |
|
| 697 |
import codecs, io |
| 698 |
encoding = 'utf-16-le' |
| 699 |
s = "%s\n%s\n" % (10*"abc123", 10*"def456") |
| 700 |
reader = codecs.getreader(encoding)(io.BytesIO(s.encode(encoding))) |
| 701 |
for t in range(5): |
| 702 |
|
| 703 |
reader.seek(0, 0) |
| 704 |
line = reader.readline() |
| 705 |
assert s[:len(line)] == line |
| 706 |
|
| 707 |
def test_raw_unicode_escape(self): |
| 708 |
import _codecs |
| 709 |
assert str(b"\u0663", "raw-unicode-escape") == "\u0663" |
| 710 |
assert "\u0663".encode("raw-unicode-escape") == b"\u0663" |
| 711 |
assert _codecs.raw_unicode_escape_decode(r"\u1234") == ("\u1234", 6) |
| 712 |
|
| 713 |
def test_escape_decode(self): |
| 714 |
import _codecs |
| 715 |
test = _codecs.escape_encode(b'a\n\\b\x00c\td\u2045')[0] |
| 716 |
assert _codecs.escape_decode(test)[0] == b'a\n\\b\x00c\td\u2045' |
| 717 |
assert _codecs.escape_decode(b'\\077')[0] == b'?' |
| 718 |
assert _codecs.escape_decode(b'\\100')[0] == b'@' |
| 719 |
assert _codecs.escape_decode(b'\\253')[0] == bytes([0o253]) |
| 720 |
assert _codecs.escape_decode(b'\\312')[0] == bytes([0o312]) |
| 721 |
|
| 722 |
def test_escape_decode_wrap_around(self): |
| 723 |
import _codecs |
| 724 |
assert _codecs.escape_decode(b'\\400')[0] == b'\0' |
| 725 |
|
| 726 |
def test_escape_decode_ignore_invalid(self): |
| 727 |
import _codecs |
| 728 |
assert _codecs.escape_decode(b'\\9')[0] == b'\\9' |
| 729 |
assert _codecs.escape_decode(b'\\01')[0] == b'\x01' |
| 730 |
assert _codecs.escape_decode(b'\\0f')[0] == b'\0' + b'f' |
| 731 |
assert _codecs.escape_decode(b'\\08')[0] == b'\0' + b'8' |
| 732 |
|
| 733 |
def test_escape_decode_errors(self): |
| 734 |
import _codecs |
| 735 |
raises(ValueError, _codecs.escape_decode, br"\x") |
| 736 |
raises(ValueError, _codecs.escape_decode, br"[\x]") |
| 737 |
raises(ValueError, _codecs.escape_decode, br"\x0") |
| 738 |
raises(ValueError, _codecs.escape_decode, br"[\x0]") |
| 739 |
|
| 740 |
def test_unicode_escape_decode_errors(self): |
| 741 |
from _codecs import unicode_escape_decode, raw_unicode_escape_decode |
| 742 |
import sys |
| 743 |
for decode in [unicode_escape_decode, raw_unicode_escape_decode]: |
| 744 |
for c, d in ('u', 4), ('U', 4): |
| 745 |
for i in range(d): |
| 746 |
raises(UnicodeDecodeError, decode, "\\" + c + "0"*i) |
| 747 |
raises(UnicodeDecodeError, decode, "[\\" + c + "0"*i + "]") |
| 748 |
data = "[\\" + c + "0"*i + "]\\" + c + "0"*i |
| 749 |
lgt = len(data) |
| 750 |
assert decode(data, "ignore") == (u"[]", lgt) |
| 751 |
assert decode(data, "replace") == (u"[\ufffd]\ufffd", lgt) |
| 752 |
raises(UnicodeDecodeError, decode, r"\U00110000") |
| 753 |
lgt = 10 |
| 754 |
assert decode(r"\U00110000", "ignore") == (u"", lgt) |
| 755 |
assert decode(r"\U00110000", "replace") == (u"\ufffd", lgt) |
| 756 |
exc = raises(UnicodeDecodeError, unicode_escape_decode, b"\u1z32z3", 'strict') |
| 757 |
assert str(exc.value) == r"'unicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX escape" |
| 758 |
exc = raises(UnicodeDecodeError, raw_unicode_escape_decode, b"\u1z32z3", 'strict') |
| 759 |
assert str(exc.value) == r"'rawunicodeescape' codec can't decode bytes in position 0-2: truncated \uXXXX escape" |
| 760 |
exc = raises(UnicodeDecodeError, raw_unicode_escape_decode, b"\U1z32z3", 'strict') |
| 761 |
assert str(exc.value) == r"'rawunicodeescape' codec can't decode bytes in position 0-2: truncated \UXXXXXXXX escape" |
| 762 |
|
| 763 |
def test_escape_encode(self): |
| 764 |
import _codecs |
| 765 |
assert _codecs.escape_encode(b'"')[0] == b'"' |
| 766 |
assert _codecs.escape_encode(b"'")[0] == b"\\'" |
| 767 |
|
| 768 |
def test_decode_utf8_different_case(self): |
| 769 |
constant = "a" |
| 770 |
assert constant.encode("utf-8") == constant.encode("UTF-8") |
| 771 |
|
| 772 |
def test_codec_wrong_result(self): |
| 773 |
import _codecs |
| 774 |
def search_function(encoding): |
| 775 |
def f(input, errors="strict"): |
| 776 |
return 42 |
| 777 |
if encoding == 'test.mytestenc': |
| 778 |
return (f, f, None, None) |
| 779 |
return None |
| 780 |
_codecs.register(search_function) |
| 781 |
raises(TypeError, b"hello".decode, "test.mytestenc") |
| 782 |
raises(TypeError, "hello".encode, "test.mytestenc") |
| 783 |
assert _codecs.unregister(search_function) == 0 |
| 784 |
|
| 785 |
raises(LookupError, u"abc".encode, "test.mytestenc") |
| 786 |
|
| 787 |
def test_codec_wrapped_exception(self): |
| 788 |
import _codecs |
| 789 |
def search_function(encoding): |
| 790 |
def f(input, errors="strict"): |
| 791 |
raise to_raise |
| 792 |
if encoding == 'test.failingenc': |
| 793 |
return (f, f, None, None) |
| 794 |
return None |
| 795 |
_codecs.register(search_function) |
| 796 |
to_raise = RuntimeError('should be wrapped') |
| 797 |
exc = raises(RuntimeError, b"hello".decode, "test.failingenc") |
| 798 |
assert str(exc.value) == ( |
| 799 |
"decoding with 'test.failingenc' codec failed " |
| 800 |
"(RuntimeError: should be wrapped)") |
| 801 |
exc = raises(RuntimeError, u"hello".encode, "test.failingenc") |
| 802 |
assert str(exc.value) == ( |
| 803 |
"encoding with 'test.failingenc' codec failed " |
| 804 |
"(RuntimeError: should be wrapped)") |
| 805 |
|
| 806 |
to_raise.attr = "don't wrap" |
| 807 |
exc = raises(RuntimeError, u"hello".encode, "test.failingenc") |
| 808 |
assert exc.value == to_raise |
| 809 |
|
| 810 |
to_raise = RuntimeError("Should", "Not", "Wrap") |
| 811 |
exc = raises(RuntimeError, u"hello".encode, "test.failingenc") |
| 812 |
assert exc.value == to_raise |
| 813 |
|
| 814 |
def test_one_arg_encoder(self): |
| 815 |
import _codecs |
| 816 |
def search_function(encoding): |
| 817 |
def encode_one(u): |
| 818 |
return (b'foo', len(u)) |
| 819 |
def decode_one(u): |
| 820 |
return (u'foo', len(u)) |
| 821 |
if encoding == 'onearg': |
| 822 |
return (encode_one, decode_one, None, None) |
| 823 |
return None |
| 824 |
_codecs.register(search_function) |
| 825 |
assert u"hello".encode("onearg") == b'foo' |
| 826 |
assert b"hello".decode("onearg") == u'foo' |
| 827 |
assert _codecs.encode(u"hello", "onearg") == b'foo' |
| 828 |
assert _codecs.decode(b"hello", "onearg") == u'foo' |
| 829 |
|
| 830 |
def test_codecs_lookup(self): |
| 831 |
import _codecs |
| 832 |
FOUND = (1, 2, 3, 4) |
| 833 |
NOT_FOUND = (None, None, None, None) |
| 834 |
def search_function(encoding): |
| 835 |
if encoding == "aaa_8": |
| 836 |
return FOUND |
| 837 |
else: |
| 838 |
return NOT_FOUND |
| 839 |
|
| 840 |
_codecs.register(search_function) |
| 841 |
assert FOUND == _codecs.lookup('aaa_8') |
| 842 |
assert FOUND == _codecs.lookup('AAA-8') |
| 843 |
assert FOUND == _codecs.lookup('AAA---8') |
| 844 |
assert FOUND == _codecs.lookup('AAA 8') |
| 845 |
assert FOUND == _codecs.lookup('aaa\xe9\u20ac-8') |
| 846 |
assert NOT_FOUND == _codecs.lookup('AAA.8') |
| 847 |
assert NOT_FOUND == _codecs.lookup('AAA...8') |
| 848 |
assert NOT_FOUND == _codecs.lookup('BBB-8') |
| 849 |
assert NOT_FOUND == _codecs.lookup('BBB.8') |
| 850 |
assert NOT_FOUND == _codecs.lookup('a\xe9\u20ac-8') |
| 851 |
_codecs.unregister(search_function) |
| 852 |
|
| 853 |
|
| 854 |
def test_cpytest_decode(self): |
| 855 |
import codecs |
| 856 |
assert codecs.decode(b'\xe4\xf6\xfc', 'latin-1') == '\xe4\xf6\xfc' |
| 857 |
raises(TypeError, codecs.decode) |
| 858 |
assert codecs.decode(b'abc') == 'abc' |
| 859 |
exc = raises(UnicodeDecodeError, codecs.decode, b'\xff', 'ascii') |
| 860 |
|
| 861 |
exc = raises(UnicodeDecodeError, codecs.decode, b'\xe0\x00', 'utf-8') |
| 862 |
assert 'invalid continuation byte' in exc.value.reason |
| 863 |
|
| 864 |
def test_bad_errorhandler_return(self): |
| 865 |
import codecs |
| 866 |
def baddecodereturn1(exc): |
| 867 |
return 42 |
| 868 |
codecs.register_error("test.baddecodereturn1", baddecodereturn1) |
| 869 |
raises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1") |
| 870 |
raises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1") |
| 871 |
raises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1") |
| 872 |
raises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1") |
| 873 |
raises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1") |
| 874 |
raises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1") |
| 875 |
|
| 876 |
def test_cpy_bug1175396(self): |
| 877 |
import codecs, io |
| 878 |
s = [ |
| 879 |
'<%!--===================================================\r\n', |
| 880 |
' BLOG index page: show recent articles,\r\n', |
| 881 |
' today\'s articles, or articles of a specific date.\r\n', |
| 882 |
'========================================================--%>\r\n', |
| 883 |
'<%@inputencoding="ISO-8859-1"%>\r\n', |
| 884 |
'<%@pagetemplate=TEMPLATE.y%>\r\n', |
| 885 |
'<%@import=import frog.util, frog%>\r\n', |
| 886 |
'<%@import=import frog.objects%>\r\n', |
| 887 |
'<%@import=from frog.storageerrors import StorageError%>\r\n', |
| 888 |
'<%\r\n', |
| 889 |
'\r\n', |
| 890 |
'import logging\r\n', |
| 891 |
'log=logging.getLogger("Snakelets.logger")\r\n', |
| 892 |
'\r\n', |
| 893 |
'\r\n', |
| 894 |
'user=self.SessionCtx.user\r\n', |
| 895 |
'storageEngine=self.SessionCtx.storageEngine\r\n', |
| 896 |
'\r\n', |
| 897 |
'\r\n', |
| 898 |
'def readArticlesFromDate(date, count=None):\r\n', |
| 899 |
' entryids=storageEngine.listBlogEntries(date)\r\n', |
| 900 |
' entryids.reverse() # descending\r\n', |
| 901 |
' if count:\r\n', |
| 902 |
' entryids=entryids[:count]\r\n', |
| 903 |
' try:\r\n', |
| 904 |
' return [ frog.objects.BlogEntry.load(storageEngine, date, Id) for Id in entryids ]\r\n', |
| 905 |
' except StorageError,x:\r\n', |
| 906 |
' log.error("Error loading articles: "+str(x))\r\n', |
| 907 |
' self.abort("cannot load articles")\r\n', |
| 908 |
] |
| 909 |
stream = io.BytesIO("".join(s).encode("utf7")) |
| 910 |
assert b"aborrt" not in stream.getvalue() |
| 911 |
reader = codecs.getreader("utf7")(stream) |
| 912 |
for (i, line) in enumerate(reader): |
| 913 |
assert line == s[i] |
| 914 |
|
| 915 |
def test_buffer_encode(self): |
| 916 |
import _codecs, array |
| 917 |
assert (_codecs.readbuffer_encode(array.array('b', b'spam')) == |
| 918 |
(b'spam', 4)) |
| 919 |
assert _codecs.readbuffer_encode(u"test") == (b'test', 4) |
| 920 |
assert _codecs.readbuffer_encode("") == (b"", 0) |
| 921 |
|
| 922 |
def test_utf8sig(self): |
| 923 |
import codecs |
| 924 |
d = codecs.getincrementaldecoder("utf-8-sig")() |
| 925 |
s = "spam" |
| 926 |
assert d.decode(s.encode("utf-8-sig")) == s |
| 927 |
|
| 928 |
def test_incremental_errors(self): |
| 929 |
|
| 930 |
|
| 931 |
import _codecs |
| 932 |
for encoding in ('utf-8', 'utf-16'): |
| 933 |
cases = [b'\x80', b'\xBF', b'\xC0', b'\xC1', b'\xF5', b'\xF6', b'\xFF'] |
| 934 |
for prefix in (b'\xC2', b'\xDF', b'\xE0', b'\xE0\xA0', b'\xEF', |
| 935 |
b'\xEF\xBF', b'\xF0', b'\xF0\x90', b'\xF0\x90\x80', |
| 936 |
b'\xF4', b'\xF4\x8F', b'\xF4\x8F\xBF'): |
| 937 |
for suffix in b'\x7F', b'\xC0': |
| 938 |
cases.append(prefix + suffix) |
| 939 |
cases.extend((b'\xE0\x80', b'\xE0\x9F', b'\xED\xA0\x80', |
| 940 |
b'\xED\xBF\xBF', b'\xF0\x80', b'\xF0\x8F', b'\xF4\x90')) |
| 941 |
|
| 942 |
for data in cases: |
| 943 |
dec = _codecs.lookup("utf-8").incrementaldecoder() |
| 944 |
raises(UnicodeDecodeError, dec.decode, data) |
| 945 |
|
| 946 |
def test_incremental_surrogatepass(self): |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
import codecs |
| 951 |
for encoding in ('utf-8', 'utf-16'): |
| 952 |
data = u'\uD901'.encode(encoding, 'surrogatepass') |
| 953 |
for i in range(1, len(data)): |
| 954 |
dec = codecs.getincrementaldecoder(encoding)('surrogatepass') |
| 955 |
assert dec.decode(data[:i]) == '' |
| 956 |
assert dec.decode(data[i:], True) == '\uD901' |
| 957 |
|
| 958 |
data = '\uDC02'.encode(encoding, 'surrogatepass') |
| 959 |
for i in range(1, len(data)): |
| 960 |
dec = codecs.getincrementaldecoder(encoding)('surrogatepass') |
| 961 |
assert dec.decode(data[:i]) == '' |
| 962 |
assert dec.decode(data[i:], False) == '\uDC02' |
| 963 |
|
| 964 |
def test_decoder_state(self): |
| 965 |
import codecs |
| 966 |
encoding = 'utf16' |
| 967 |
u = 'abc123' |
| 968 |
s = u.encode(encoding) |
| 969 |
for i in range(len(u) + 1): |
| 970 |
d = codecs.getincrementalencoder(encoding)() |
| 971 |
part1 = d.encode(u[:i]) |
| 972 |
state = d.getstate() |
| 973 |
d = codecs.getincrementalencoder(encoding)() |
| 974 |
d.setstate(state) |
| 975 |
part2 = d.encode(u[i:], True) |
| 976 |
assert s == part1 + part2 |
| 977 |
|
| 978 |
def test_utf_8_decode1(self): |
| 979 |
import _codecs |
| 980 |
1') |
| 981 |
utf8 = b'\xe5\xe4\xf6' |
| 982 |
uval, lgt = _codecs.utf_8_decode(utf8, 'replace') |
| 983 |
assert lgt == 3 |
| 984 |
assert [ord(x) for x in uval] == [65533, 65533, 65533] |
| 985 |
|
| 986 |
def test_utf_8_decode2(self): |
| 987 |
import _codecs |
| 988 |
|
| 989 |
utf8 = b'abcdef \xc4' |
| 990 |
uval, lgt = _codecs.utf_8_decode(utf8, 'ignore', False) |
| 991 |
assert lgt == 7 |
| 992 |
|
| 993 |
def test_escape_decode_escaped_newline(self): |
| 994 |
import _codecs |
| 995 |
s = b'\\\n' |
| 996 |
decoded = _codecs.unicode_escape_decode(s)[0] |
| 997 |
assert decoded == '' |
| 998 |
|
| 999 |
def test_charmap_decode_1(self): |
| 1000 |
import codecs |
| 1001 |
assert codecs.charmap_encode(u'xxx') == (b'xxx', 3) |
| 1002 |
assert codecs.charmap_encode(u'xxx', 'strict', {ord('x'): b'XX'}) == (b'XXXXXX', 3) |
| 1003 |
|
| 1004 |
res = codecs.charmap_decode(b"\x00\x01\x02", "replace", u"ab") |
| 1005 |
assert res == ("ab\ufffd", 3) |
| 1006 |
res = codecs.charmap_decode(b"\x00\x01\x02", "replace", u"ab\ufffe") |
| 1007 |
assert res == ('ab\ufffd', 3) |
| 1008 |
|
| 1009 |
def test_errors(self): |
| 1010 |
import codecs |
| 1011 |
assert codecs.replace_errors(UnicodeEncodeError( |
| 1012 |
"ascii", u"\u3042", 0, 1, "ouch")) == (u"?", 1) |
| 1013 |
assert codecs.replace_errors(UnicodeDecodeError( |
| 1014 |
"ascii", b"\xff", 0, 1, "ouch")) == (u"\ufffd", 1) |
| 1015 |
assert codecs.replace_errors(UnicodeTranslateError( |
| 1016 |
"\u3042", 0, 1, "ouch")) == ("\ufffd", 1) |
| 1017 |
|
| 1018 |
assert codecs.replace_errors(UnicodeEncodeError( |
| 1019 |
"ascii", "\u3042\u3042", 0, 2, "ouch")) == (u"??", 2) |
| 1020 |
assert codecs.replace_errors(UnicodeDecodeError( |
| 1021 |
"ascii", b"\xff\xff", 0, 2, "ouch")) == (u"\ufffd", 2) |
| 1022 |
assert codecs.replace_errors(UnicodeTranslateError( |
| 1023 |
"\u3042\u3042", 0, 2, "ouch")) == ("\ufffd\ufffd", 2) |
| 1024 |
|
| 1025 |
class BadStartUnicodeEncodeError(UnicodeEncodeError): |
| 1026 |
def __init__(self): |
| 1027 |
UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad") |
| 1028 |
self.start = [] |
| 1029 |
|
| 1030 |
|
| 1031 |
class BadObjectUnicodeEncodeError(UnicodeEncodeError): |
| 1032 |
def __init__(self): |
| 1033 |
UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad") |
| 1034 |
self.object = [] |
| 1035 |
|
| 1036 |
|
| 1037 |
class NoEndUnicodeDecodeError(UnicodeDecodeError): |
| 1038 |
def __init__(self): |
| 1039 |
UnicodeDecodeError.__init__(self, "ascii", b"", 0, 1, "bad") |
| 1040 |
del self.end |
| 1041 |
|
| 1042 |
|
| 1043 |
class BadObjectUnicodeDecodeError(UnicodeDecodeError): |
| 1044 |
def __init__(self): |
| 1045 |
UnicodeDecodeError.__init__(self, "ascii", b"", 0, 1, "bad") |
| 1046 |
self.object = [] |
| 1047 |
|
| 1048 |
|
| 1049 |
class NoStartUnicodeTranslateError(UnicodeTranslateError): |
| 1050 |
def __init__(self): |
| 1051 |
UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
| 1052 |
del self.start |
| 1053 |
|
| 1054 |
|
| 1055 |
class NoEndUnicodeTranslateError(UnicodeTranslateError): |
| 1056 |
def __init__(self): |
| 1057 |
UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
| 1058 |
del self.end |
| 1059 |
|
| 1060 |
|
| 1061 |
class NoObjectUnicodeTranslateError(UnicodeTranslateError): |
| 1062 |
def __init__(self): |
| 1063 |
UnicodeTranslateError.__init__(self, u"", 0, 1, "bad") |
| 1064 |
del self.object |
| 1065 |
|
| 1066 |
import codecs |
| 1067 |
raises(TypeError, codecs.replace_errors, BadObjectUnicodeEncodeError()) |
| 1068 |
raises(TypeError, codecs.replace_errors, 42) |
| 1069 |
|
| 1070 |
raises(TypeError, codecs.replace_errors, UnicodeError("ouch")) |
| 1071 |
raises(TypeError, codecs.replace_errors, BadObjectUnicodeEncodeError()) |
| 1072 |
raises(TypeError, codecs.replace_errors, BadObjectUnicodeDecodeError() |
| 1073 |
) |
| 1074 |
|
| 1075 |
|
| 1076 |
def test_decode_ignore(self): |
| 1077 |
assert b'\xff'.decode('utf-7', 'ignore') == '' |
| 1078 |
|
| 1079 |
def test_backslashreplace(self): |
| 1080 |
import sys, codecs |
| 1081 |
sin = u"a\xac\u1234\u20ac\u8000\U0010ffff" |
| 1082 |
if sys.maxunicode > 65535: |
| 1083 |
expected_ascii = b"a\\xac\\u1234\\u20ac\\u8000\\U0010ffff" |
| 1084 |
expected_8859 = b"a\xac\\u1234\xa4\\u8000\\U0010ffff" |
| 1085 |
else: |
| 1086 |
expected_ascii = b"a\\xac\\u1234\\u20ac\\u8000\\udbff\\udfff" |
| 1087 |
expected_8859 = b"a\xac\\u1234\xa4\\u8000\\udbff\\udfff" |
| 1088 |
assert sin.encode('ascii', 'backslashreplace') == expected_ascii |
| 1089 |
assert sin.encode("iso-8859-15", "backslashreplace") == expected_8859 |
| 1090 |
|
| 1091 |
assert 'a\xac\u1234\u20ac\u8000'.encode('ascii', 'backslashreplace') == b'a\\xac\u1234\u20ac\u8000' |
| 1092 |
assert b'\x00\x60\x80'.decode( |
| 1093 |
'ascii', 'backslashreplace') == u'\x00\x60\\x80' |
| 1094 |
assert codecs.charmap_decode( |
| 1095 |
b"\x00\x01\x02", "backslashreplace", "ab") == ("ab\\x02", 3) |
| 1096 |
|
| 1097 |
def test_namereplace(self): |
| 1098 |
assert 'a\xac\u1234\u20ac\u8000'.encode('ascii', 'namereplace') == ( |
| 1099 |
b'a\\N{NOT SIGN}\\N{ETHIOPIC SYLLABLE SEE}\\N{EURO SIGN}' |
| 1100 |
b'\\N{CJK UNIFIED IDEOGRAPH-8000}') |
| 1101 |
assert '[\uDC80]'.encode('utf-8', 'namereplace') == b'[\\udc80]' |
| 1102 |
|
| 1103 |
def test_surrogateescape(self): |
| 1104 |
uni = b"\xed\xb0\x80".decode("utf-8", "surrogateescape") |
| 1105 |
assert uni == "\udced\udcb0\udc80" |
| 1106 |
assert "\udce4\udceb\udcef\udcf6\udcfc".encode("latin-1", |
| 1107 |
"surrogateescape") == b"\xe4\xeb\xef\xf6\xfc" |
| 1108 |
assert b'a\x80b'.decode('utf-8', 'surrogateescape') == 'a\udc80b' |
| 1109 |
assert 'a\udc80b'.encode('utf-8', 'surrogateescape') == b'a\x80b' |
| 1110 |
for enc in ('utf-8', 'ascii', 'latin-1', 'charmap'): |
| 1111 |
assert '\udcc3'.encode(enc, 'surrogateescape') == b'\xc3' |
| 1112 |
|
| 1113 |
def test_surrogatepass_handler(self): |
| 1114 |
import _codecs |
| 1115 |
assert _codecs.lookup_error("surrogatepass") |
| 1116 |
assert ("abc\ud800def".encode("utf-8", "surrogatepass") == |
| 1117 |
b"abc\xed\xa0\x80def") |
| 1118 |
assert (b"abc\xed\xa0\x80def".decode("utf-8", "surrogatepass") == |
| 1119 |
"abc\ud800def") |
| 1120 |
assert ('surrogate:\udcff'.encode("utf-8", "surrogatepass") == |
| 1121 |
b'surrogate:\xed\xb3\xbf') |
| 1122 |
assert (b'surrogate:\xed\xb3\xbf'.decode("utf-8", "surrogatepass") == |
| 1123 |
'surrogate:\udcff') |
| 1124 |
raises(UnicodeDecodeError, b"abc\xed\xa0".decode, "utf-8", |
| 1125 |
"surrogatepass") |
| 1126 |
raises(UnicodeDecodeError, b"abc\xed\xa0z".decode, "utf-8", |
| 1127 |
"surrogatepass") |
| 1128 |
assert u'\ud8ae'.encode('utf_16_be', 'surrogatepass') == b'\xd8\xae' |
| 1129 |
assert (u'\U0000d8ae'.encode('utf-32-be', 'surrogatepass') == |
| 1130 |
b'\x00\x00\xd8\xae') |
| 1131 |
assert (u'\x80\ud800'.encode('utf8', 'surrogatepass') == |
| 1132 |
b'\xc2\x80\xed\xa0\x80') |
| 1133 |
assert b'\xd8\x03\xdf\xff\xdc\x80\x00A'.decode('utf_16_be', |
| 1134 |
'surrogatepass') == u'\U00010fff\udc80A' |
| 1135 |
|
| 1136 |
def test_badandgoodsurrogatepassexceptions(self): |
| 1137 |
import codecs |
| 1138 |
surrogatepass_errors = codecs.lookup_error('surrogatepass') |
| 1139 |
|
| 1140 |
raises(TypeError, surrogatepass_errors, 42) |
| 1141 |
|
| 1142 |
raises(TypeError, surrogatepass_errors, UnicodeError("ouch")) |
| 1143 |
|
| 1144 |
raises(TypeError, surrogatepass_errors, |
| 1145 |
UnicodeTranslateError("\ud800", 0, 1, "ouch")) |
| 1146 |
|
| 1147 |
for enc in ("utf-8", "utf-16le", "utf-16be", "utf-32le", "utf-32be"): |
| 1148 |
raises(UnicodeEncodeError, surrogatepass_errors, |
| 1149 |
UnicodeEncodeError(enc, "a", 0, 1, "ouch")) |
| 1150 |
raises(UnicodeDecodeError, surrogatepass_errors, |
| 1151 |
UnicodeDecodeError(enc, "a".encode(enc), 0, 1, "ouch")) |
| 1152 |
for s in ("\ud800", "\udfff", "\ud800\udfff"): |
| 1153 |
raises(UnicodeEncodeError, surrogatepass_errors, |
| 1154 |
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")) |
| 1155 |
tests = [ |
| 1156 |
("utf-8", "\ud800", b'\xed\xa0\x80', 3), |
| 1157 |
("utf-16le", "\ud800", b'\x00\xd8', 2), |
| 1158 |
("utf-16be", "\ud800", b'\xd8\x00', 2), |
| 1159 |
("utf-32le", "\ud800", b'\x00\xd8\x00\x00', 4), |
| 1160 |
("utf-32be", "\ud800", b'\x00\x00\xd8\x00', 4), |
| 1161 |
("utf-8", "\udfff", b'\xed\xbf\xbf', 3), |
| 1162 |
("utf-16le", "\udfff", b'\xff\xdf', 2), |
| 1163 |
("utf-16be", "\udfff", b'\xdf\xff', 2), |
| 1164 |
("utf-32le", "\udfff", b'\xff\xdf\x00\x00', 4), |
| 1165 |
("utf-32be", "\udfff", b'\x00\x00\xdf\xff', 4), |
| 1166 |
("utf-8", "\ud800\udfff", b'\xed\xa0\x80\xed\xbf\xbf', 3), |
| 1167 |
("utf-16le", "\ud800\udfff", b'\x00\xd8\xff\xdf', 2), |
| 1168 |
("utf-16be", "\ud800\udfff", b'\xd8\x00\xdf\xff', 2), |
| 1169 |
("utf-32le", "\ud800\udfff", b'\x00\xd8\x00\x00\xff\xdf\x00\x00', 4), |
| 1170 |
("utf-32be", "\ud800\udfff", b'\x00\x00\xd8\x00\x00\x00\xdf\xff', 4), |
| 1171 |
] |
| 1172 |
for enc, s, b, n in tests: |
| 1173 |
assert surrogatepass_errors( |
| 1174 |
UnicodeEncodeError(enc, "a" + s + "b", 1, 1 + len(s), "ouch") |
| 1175 |
) == (b, 1 + len(s)) |
| 1176 |
assert surrogatepass_errors( |
| 1177 |
UnicodeDecodeError(enc, bytearray(b"a" + b[:n] + b"b"), |
| 1178 |
1, 1 + n, "ouch") |
| 1179 |
) == (s[:1], 1 + n) |
| 1180 |
|
| 1181 |
def test_badhandler(self): |
| 1182 |
import codecs |
| 1183 |
results = ( 42, u"foo", (1,2,3), (u"foo", 1, 3), (u"foo", None), (u"foo",), ("foo", 1, 3), ("foo", None), ("foo",) ) |
| 1184 |
encs = ("ascii", "latin-1", "iso-8859-1", "iso-8859-15") |
| 1185 |
|
| 1186 |
for res in results: |
| 1187 |
codecs.register_error("test.badhandler", lambda x: res) |
| 1188 |
for enc in encs: |
| 1189 |
raises( |
| 1190 |
TypeError, |
| 1191 |
"\u3042".encode, |
| 1192 |
enc, |
| 1193 |
"test.badhandler" |
| 1194 |
) |
| 1195 |
for (enc, bytes) in ( |
| 1196 |
("utf-8", b"\xff"), |
| 1197 |
("ascii", b"\xff"), |
| 1198 |
("utf-7", b"+x-"), |
| 1199 |
): |
| 1200 |
raises( |
| 1201 |
TypeError, |
| 1202 |
bytes.decode, |
| 1203 |
enc, |
| 1204 |
"test.badhandler" |
| 1205 |
) |
| 1206 |
|
| 1207 |
def test_badhandler_longindex(self): |
| 1208 |
import codecs |
| 1209 |
import sys |
| 1210 |
errors = 'test.badhandler_longindex' |
| 1211 |
codecs.register_error(errors, lambda x: (u'', sys.maxsize + 1)) |
| 1212 |
|
| 1213 |
raises((IndexError, OverflowError), b'apple\x92ham\x93spam'.decode, 'utf-8', errors) |
| 1214 |
|
| 1215 |
def test_badhandler_returns_unicode(self): |
| 1216 |
import codecs |
| 1217 |
import sys |
| 1218 |
errors = 'test.badhandler_unicode' |
| 1219 |
codecs.register_error(errors, lambda x: (chr(100000), x.end)) |
| 1220 |
|
| 1221 |
raises(UnicodeEncodeError, u'\udc80\ud800\udfff'.encode, 'utf-8', errors) |
| 1222 |
|
| 1223 |
def test_encode_error_bad_handler(self): |
| 1224 |
import codecs |
| 1225 |
codecs.register_error("test.bad_handler", lambda e: (repl, 1)) |
| 1226 |
assert u"xyz".encode("latin-1", "test.bad_handler") == b"xyz" |
| 1227 |
repl = u"\u1234" |
| 1228 |
raises(UnicodeEncodeError, u"\u5678".encode, "latin-1", |
| 1229 |
"test.bad_handler") |
| 1230 |
repl = u"\u00E9" |
| 1231 |
s = u"\u5678".encode("latin-1", "test.bad_handler") |
| 1232 |
assert s == b'\xe9' |
| 1233 |
raises(UnicodeEncodeError, "\u5678".encode, "ascii", |
| 1234 |
"test.bad_handler") |
| 1235 |
|
| 1236 |
def test_lone_surrogates(self): |
| 1237 |
encodings = ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
| 1238 |
'utf-32', 'utf-32-le', 'utf-32-be') |
| 1239 |
for encoding in encodings: |
| 1240 |
raises(UnicodeEncodeError, u'\ud800'.encode, encoding) |
| 1241 |
assert (u'[\udc80]'.encode(encoding, "backslashreplace") == |
| 1242 |
'[\\udc80]'.encode(encoding)) |
| 1243 |
assert (u'[\udc80]'.encode(encoding, "ignore") == |
| 1244 |
'[]'.encode(encoding)) |
| 1245 |
assert (u'[\udc80]'.encode(encoding, "replace") == |
| 1246 |
'[?]'.encode(encoding)) |
| 1247 |
|
| 1248 |
assert (u'[\ud800\udc80]'.encode(encoding, "ignore") == |
| 1249 |
'[]'.encode(encoding)) |
| 1250 |
assert (u'[\ud800\udc80]'.encode(encoding, "replace") == |
| 1251 |
'[??]'.encode(encoding)) |
| 1252 |
for encoding, ill_surrogate in [('utf-8', b'\xed\xb2\x80'), |
| 1253 |
('utf-16-le', b'\x80\xdc'), |
| 1254 |
('utf-16-be', b'\xdc\x80'), |
| 1255 |
('utf-32-le', b'\x80\xdc\x00\x00'), |
| 1256 |
('utf-32-be', b'\x00\x00\xdc\x80')]: |
| 1257 |
ill_formed_sequence_replace = "\ufffd" |
| 1258 |
if encoding == 'utf-8': |
| 1259 |
ill_formed_sequence_replace *= 3 |
| 1260 |
bom = "".encode(encoding) |
| 1261 |
for before, after in [("\U00010fff", "A"), ("[", "]"), |
| 1262 |
("A", "\U00010fff")]: |
| 1263 |
before_sequence = before.encode(encoding)[len(bom):] |
| 1264 |
after_sequence = after.encode(encoding)[len(bom):] |
| 1265 |
test_string = before + "\uDC80" + after |
| 1266 |
test_sequence = (bom + before_sequence + ill_surrogate + after_sequence) |
| 1267 |
raises(UnicodeDecodeError, test_sequence.decode, encoding) |
| 1268 |
assert test_string.encode(encoding, 'surrogatepass') == test_sequence |
| 1269 |
assert test_sequence.decode(encoding, 'surrogatepass') == test_string |
| 1270 |
assert test_sequence.decode(encoding, 'ignore') == before + after |
| 1271 |
assert test_sequence.decode(encoding, 'replace') == (before + |
| 1272 |
ill_formed_sequence_replace + after), str( |
| 1273 |
(encoding, test_sequence, before + ill_formed_sequence_replace + after)) |
| 1274 |
backslashreplace = ''.join('\\x%02x' % b for b in ill_surrogate) |
| 1275 |
assert test_sequence.decode(encoding, "backslashreplace") == (before + |
| 1276 |
backslashreplace + after) |
| 1277 |
|
| 1278 |
def test_lone_surrogates_utf_8(self): |
| 1279 |
""" |
| 1280 |
utf-8 should not longer allow surrogates, |
| 1281 |
and should return back full surrogate pairs. |
| 1282 |
""" |
| 1283 |
e = raises(UnicodeEncodeError, u"\udc80\ud800\udfff".encode, "utf-8", |
| 1284 |
"surrogateescape").value |
| 1285 |
assert e.start == 1 |
| 1286 |
assert e.end == 3 |
| 1287 |
assert e.object[e.start:e.end] == u'\ud800\udfff' |
| 1288 |
|
| 1289 |
def test_charmap_encode(self): |
| 1290 |
assert 'xxx'.encode('charmap') == b'xxx' |
| 1291 |
|
| 1292 |
import codecs |
| 1293 |
exc = raises(TypeError, codecs.charmap_encode, u'\xff', "replace", {0xff: 300}) |
| 1294 |
assert str(exc.value) == 'character mapping must be in range(256)' |
| 1295 |
exc = raises(TypeError, codecs.charmap_encode, u'\xff', "replace", {0xff: u'a'}) |
| 1296 |
assert str(exc.value) == 'character mapping must return integer, bytes or None, not str' |
| 1297 |
raises(UnicodeError, codecs.charmap_encode, u"\xff", "replace", {0xff: None}) |
| 1298 |
|
| 1299 |
def test_charmap_encode_replace(self): |
| 1300 |
charmap = dict([(c, bytes([c, c]).upper()) for c in b"abcdefgh"]) |
| 1301 |
charmap[ord("?")] = b"XYZ" |
| 1302 |
import codecs |
| 1303 |
sin = u"abcDEF" |
| 1304 |
sout = codecs.charmap_encode(sin, "replace", charmap)[0] |
| 1305 |
assert sout == b"AABBCCXYZXYZXYZ" |
| 1306 |
|
| 1307 |
def test_charmap_decode_2(self): |
| 1308 |
assert b'foo'.decode('charmap') == 'foo' |
| 1309 |
|
| 1310 |
def test_charmap_build(self): |
| 1311 |
import codecs |
| 1312 |
assert codecs.charmap_build(u'123456') == {49: 0, 50: 1, 51: 2, |
| 1313 |
52: 3, 53: 4, 54: 5} |
| 1314 |
|
| 1315 |
def test_utf7_start_end_in_exception(self): |
| 1316 |
try: |
| 1317 |
b'+IC'.decode('utf-7') |
| 1318 |
except UnicodeDecodeError as exc: |
| 1319 |
assert exc.start == 0 |
| 1320 |
assert exc.end == 3 |
| 1321 |
|
| 1322 |
def test_utf7_surrogate(self): |
| 1323 |
assert b'+3ADYAA-'.decode('utf-7') == u'\udc00\ud800' |
| 1324 |
|
| 1325 |
def test_utf_7_decode(self): |
| 1326 |
from _codecs import utf_7_decode |
| 1327 |
res = utf_7_decode(b'+') |
| 1328 |
assert res == (u'', 0) |
| 1329 |
|
| 1330 |
def test_utf7_errors(self): |
| 1331 |
import codecs |
| 1332 |
tests = [ |
| 1333 |
(b'a\xffb', u'a\ufffdb'), |
| 1334 |
(b'a+IK', u'a\ufffd'), |
| 1335 |
(b'a+IK-b', u'a\ufffdb'), |
| 1336 |
(b'a+IK,b', u'a\ufffdb'), |
| 1337 |
(b'a+IKx', u'a\u20ac\ufffd'), |
| 1338 |
(b'a+IKx-b', u'a\u20ac\ufffdb'), |
| 1339 |
(b'a+IKwgr', u'a\u20ac\ufffd'), |
| 1340 |
(b'a+IKwgr-b', u'a\u20ac\ufffdb'), |
| 1341 |
(b'a+IKwgr,', u'a\u20ac\ufffd'), |
| 1342 |
(b'a+IKwgr,-b', u'a\u20ac\ufffd-b'), |
| 1343 |
(b'a+IKwgrB', u'a\u20ac\u20ac\ufffd'), |
| 1344 |
(b'a+IKwgrB-b', u'a\u20ac\u20ac\ufffdb'), |
| 1345 |
(b'a+/,+IKw-b', u'a\ufffd\u20acb'), |
| 1346 |
(b'a+//,+IKw-b', u'a\ufffd\u20acb'), |
| 1347 |
(b'a+///,+IKw-b', u'a\uffff\ufffd\u20acb'), |
| 1348 |
(b'a+////,+IKw-b', u'a\uffff\ufffd\u20acb'), |
| 1349 |
(b'a+2AE\xe1b', u'a\ufffdb'), |
| 1350 |
(b'a+2AEA-b', u'a\ufffdb'), |
| 1351 |
(b'a+2AH-b', u'a\ufffdb'), |
| 1352 |
(b'a+@b', 'a\ufffdb'), |
| 1353 |
] |
| 1354 |
for raw, expected in tests: |
| 1355 |
raises(UnicodeDecodeError, codecs.utf_7_decode, raw, 'strict', True) |
| 1356 |
assert raw.decode('utf-7', 'replace') == expected |
| 1357 |
|
| 1358 |
def test_utf_16_encode_decode(self): |
| 1359 |
import codecs, sys |
| 1360 |
x = u'123abc' |
| 1361 |
if sys.byteorder == 'big': |
| 1362 |
assert codecs.getencoder('utf-16')(x) == ( |
| 1363 |
b'\xfe\xff\x001\x002\x003\x00a\x00b\x00c', 6) |
| 1364 |
assert codecs.getdecoder('utf-16')( |
| 1365 |
b'\xfe\xff\x001\x002\x003\x00a\x00b\x00c') == (x, 14) |
| 1366 |
else: |
| 1367 |
assert codecs.getencoder('utf-16')(x) == ( |
| 1368 |
b'\xff\xfe1\x002\x003\x00a\x00b\x00c\x00', 6) |
| 1369 |
assert codecs.getdecoder('utf-16')( |
| 1370 |
b'\xff\xfe1\x002\x003\x00a\x00b\x00c\x00') == (x, 14) |
| 1371 |
|
| 1372 |
def test_unicode_escape(self): |
| 1373 |
import _codecs |
| 1374 |
assert u'\\'.encode('unicode-escape') == b'\\\\' |
| 1375 |
assert b'\\\\'.decode('unicode-escape') == u'\\' |
| 1376 |
assert u'\ud801'.encode('unicode-escape') == b'\\ud801' |
| 1377 |
assert u'\u0013'.encode('unicode-escape') == b'\\x13' |
| 1378 |
assert _codecs.unicode_escape_decode(r"\u1234") == ("\u1234", 6) |
| 1379 |
|
| 1380 |
def test_mbcs(self): |
| 1381 |
import sys |
| 1382 |
if sys.platform != 'win32': |
| 1383 |
return |
| 1384 |
|
| 1385 |
from _codecs import mbcs_encode, mbcs_decode |
| 1386 |
toencode = u'caf\xe9', 'caf\xe9' |
| 1387 |
|
| 1388 |
try: |
| 1389 |
|
| 1390 |
import winreg |
| 1391 |
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, |
| 1392 |
r'System\CurrentControlSet\Control\Nls\CodePage') |
| 1393 |
if winreg.QueryValueEx(key, 'ACP')[0] == u'1255': |
| 1394 |
toencode = u'caf\xbf',b'caf\xbf' |
| 1395 |
except: |
| 1396 |
assert False, 'cannot test mbcs on this windows system, check code page' |
| 1397 |
assert u'test'.encode('mbcs') == b'test' |
| 1398 |
assert toencode[0].encode('mbcs') == toencode[1] |
| 1399 |
raises(UnicodeEncodeError, u'\u040a'.encode, 'mbcs') |
| 1400 |
assert b'cafx\e9'.decode('mbcs') == u'cafx\e9' |
| 1401 |
assert b'\xe6'.decode('mbcs') == u'\xe6' |
| 1402 |
|
| 1403 |
def test_handler_string_result(self): |
| 1404 |
import _codecs |
| 1405 |
def f(exc): |
| 1406 |
return (b'foo', exc.end) |
| 1407 |
_codecs.register_error("test.test_codecs_not_a_string", f) |
| 1408 |
result = '\u1234'.encode('ascii', 'test.test_codecs_not_a_string') |
| 1409 |
assert result == b'foo' |
| 1410 |
|
| 1411 |
def test_decode_bytearray(self): |
| 1412 |
import _codecs |
| 1413 |
b = bytearray() |
| 1414 |
assert _codecs.ascii_decode(b) == (u'', 0) |
| 1415 |
assert _codecs.latin_1_decode(b) == (u'', 0) |
| 1416 |
assert _codecs.utf_7_decode(b) == (u'', 0) |
| 1417 |
assert _codecs.utf_8_decode(b) == (u'', 0) |
| 1418 |
assert _codecs.utf_16_be_decode(b) == (u'', 0) |
| 1419 |
assert _codecs.utf_16_decode(b) == (u'', 0) |
| 1420 |
assert _codecs.utf_16_le_decode(b) == (u'', 0) |
| 1421 |
assert _codecs.utf_16_ex_decode(b) == (u'', 0, 0) |
| 1422 |
assert _codecs.utf_32_decode(b) == (u'', 0) |
| 1423 |
assert _codecs.utf_32_be_decode(b) == (u'', 0) |
| 1424 |
assert _codecs.utf_32_le_decode(b) == (u'', 0) |
| 1425 |
assert _codecs.utf_32_ex_decode(b) == (u'', 0, 0) |
| 1426 |
assert _codecs.charmap_decode(b) == (u'', 0) |
| 1427 |
assert _codecs.unicode_escape_decode(b) == (u'', 0) |
| 1428 |
assert _codecs.raw_unicode_escape_decode(b) == (u'', 0) |
| 1429 |
|
| 1430 |
def test_xmlcharrefreplace(self): |
| 1431 |
r = u'\u1234\u0080\u2345\u0079\u00AB'.encode('latin1', 'xmlcharrefreplace') |
| 1432 |
assert r == b'ሴ\x80⍅y\xab' |
| 1433 |
r = u'\u1234\u0080\u2345\u0079\u00AB'.encode('ascii', 'xmlcharrefreplace') |
| 1434 |
assert r == b'ሴ€⍅y«' |
| 1435 |
|
| 1436 |
def test_errorhandler_collection(self): |
| 1437 |
import _codecs |
| 1438 |
errors = [] |
| 1439 |
def record_error(exc): |
| 1440 |
if not isinstance(exc, UnicodeEncodeError): |
| 1441 |
raise TypeError("don't know how to handle %r" % exc) |
| 1442 |
errors.append(exc.object[exc.start:exc.end]) |
| 1443 |
return (u'', exc.end) |
| 1444 |
_codecs.register_error("test.record", record_error) |
| 1445 |
|
| 1446 |
sin = u"\xac\u1234\u1234\u20ac\u8000" |
| 1447 |
assert sin.encode("ascii", "test.record") == b"" |
| 1448 |
assert errors == [sin] |
| 1449 |
|
| 1450 |
errors = [] |
| 1451 |
assert sin.encode("latin-1", "test.record") == b"\xac" |
| 1452 |
assert errors == [u'\u1234\u1234\u20ac\u8000'] |
| 1453 |
|
| 1454 |
errors = [] |
| 1455 |
assert sin.encode("iso-8859-15", "test.record") == b"\xac\xa4" |
| 1456 |
assert errors == [u'\u1234\u1234', u'\u8000'] |
| 1457 |
|
| 1458 |
def test_unmapped(self): |
| 1459 |
|
| 1460 |
assert (b"foo\xa5bar".decode("iso-8859-3", "surrogateescape") == |
| 1461 |
"foo\udca5bar") |
| 1462 |
assert ("foo\udca5bar".encode("iso-8859-3", "surrogateescape") == |
| 1463 |
b"foo\xa5bar") |
| 1464 |
|
| 1465 |
def test_warn_escape_decode(self): |
| 1466 |
import warnings |
| 1467 |
import codecs |
| 1468 |
|
| 1469 |
with warnings.catch_warnings(record=True) as l: |
| 1470 |
warnings.simplefilter("always") |
| 1471 |
codecs.unicode_escape_decode(b'\\A') |
| 1472 |
codecs.unicode_escape_decode(b"\\" + b"\xff") |
| 1473 |
codecs.unicode_escape_decode("\\400") |
| 1474 |
|
| 1475 |
assert len(l) == 3 |
| 1476 |
assert isinstance(l[0].message, DeprecationWarning) |
| 1477 |
assert isinstance(l[1].message, DeprecationWarning) |
| 1478 |
assert isinstance(l[2].message, DeprecationWarning) |
| 1479 |
assert "octal" not in l[0].message.args[0] |
| 1480 |
assert "octal" not in l[1].message.args[0] |
| 1481 |
assert "octal" in l[2].message.args[0] |
| 1482 |
|
| 1483 |
def test_invalid_type_errors(self): |
| 1484 |
|
| 1485 |
|
| 1486 |
if not self.appdirect: |
| 1487 |
skip('"hex" only available after translation') |
| 1488 |
import codecs |
| 1489 |
res = codecs.decode(b"aabb", "hex") |
| 1490 |
assert res == b"\xaa\xbb" |
| 1491 |
res = codecs.decode(u"aabb", "hex") |
| 1492 |
assert res == b"\xaa\xbb" |
| 1493 |
res = codecs.encode(b"\xaa\xbb", "hex") |
| 1494 |
assert res == b"aabb" |
| 1495 |
|
| 1496 |
raises(LookupError, u"abc".encode, "hex") |
| 1497 |
|
| 1498 |
def test_non_text_codec(self): |
| 1499 |
import _codecs |
| 1500 |
def search_function(encoding): |
| 1501 |
def f(input, errors="strict"): |
| 1502 |
return 52, len(input) |
| 1503 |
if encoding == 'test.mynontextenc': |
| 1504 |
return (f, f, None, None) |
| 1505 |
return None |
| 1506 |
_codecs.register(search_function) |
| 1507 |
res = _codecs.encode(u"abc", "test.mynontextenc") |
| 1508 |
assert res == 52 |
| 1509 |
res = _codecs.decode(b"abc", "test.mynontextenc") |
| 1510 |
assert res == 52 |
| 1511 |
raises(TypeError, u"abc".encode, "test.mynontextenc") |
| 1512 |
raises(TypeError, b"abc".decode, "test.mynontextenc") |
| 1513 |
|
| 1514 |
def test_last_byte_handler(self): |
| 1515 |
|
| 1516 |
import _codecs |
| 1517 |
_codecs.register_error('custom_replace', lambda exc: (u'\ufffd', exc.start+1)) |
| 1518 |
for s, res in ((b"WORD\xe3\xab", |
| 1519 |
(u'WORD\ufffd\ufffd', u'WORD\ufffd')), |
| 1520 |
(b"\xef\xbb\xbfWORD\xe3\xabWORD2", |
| 1521 |
(u'\ufeffWORD\ufffd\ufffdWORD2', |
| 1522 |
u'\ufeffWORD\ufffdWORD2'))): |
| 1523 |
r = s.decode('utf8', 'replace') |
| 1524 |
assert r == res[1] |
| 1525 |
r = s.decode('utf8', 'custom_replace') |
| 1526 |
assert r == res[0] |
| 1527 |
|
| 1528 |
def test_replace_with_long(self): |
| 1529 |
import _codecs |
| 1530 |
def replace_with_long(exc): |
| 1531 |
if isinstance(exc, UnicodeDecodeError): |
| 1532 |
exc.object = b"\x00" * 8 |
| 1533 |
return ('\ufffd', exc.start) |
| 1534 |
else: |
| 1535 |
raise TypeError("don't know how to handle %r" % exc) |
| 1536 |
_codecs.register_error("test.replace_with_long", replace_with_long) |
| 1537 |
|
| 1538 |
res = b'\x00'.decode('utf-16', 'test.replace_with_long') |
| 1539 |
assert res == u'\ufffd\x00\x00\x00\x00' |
| 1540 |
res = b'\x00'.decode('utf-32', 'test.replace_with_long') |
| 1541 |
assert res == u'\ufffd\x00\x00' |
| 1542 |
|
| 1543 |
def test_replace_with_unencodable(self): |
| 1544 |
import _codecs |
| 1545 |
def replace_with_unencodable(exc): |
| 1546 |
return b"\xbd\xbe", exc.end |
| 1547 |
_codecs.register_error("test.replace_with_unencodable", replace_with_unencodable) |
| 1548 |
res = _codecs.utf_16_le_encode(u'[\udc80]', 'test.replace_with_unencodable') |
| 1549 |
assert res == (b'[\x00\xbd\xbe]\x00', 3) |
| 1550 |
|
| 1551 |
def test_repeated_pos_return(self): |
| 1552 |
import _codecs |
| 1553 |
class RepeatedPosReturn: |
| 1554 |
def __init__(self, repl="<?>"): |
| 1555 |
self.repl = repl |
| 1556 |
self.pos = 0 |
| 1557 |
self.count = 0 |
| 1558 |
|
| 1559 |
def handle(self, exc): |
| 1560 |
assert exc.start == 4 |
| 1561 |
assert exc.end == 5 |
| 1562 |
if self.count > 0: |
| 1563 |
self.count -= 1 |
| 1564 |
return "x", 0 |
| 1565 |
return ("x", exc.end) |
| 1566 |
|
| 1567 |
handler = RepeatedPosReturn() |
| 1568 |
_codecs.register_error("test.bug36819", handler.handle) |
| 1569 |
|
| 1570 |
input = "abcd\udc80" |
| 1571 |
encodings = ["ascii", "utf-8"] |
| 1572 |
|
| 1573 |
handler.repl = "x" |
| 1574 |
for enc in encodings: |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
handler.count = 50 |
| 1579 |
encoded = input.encode(enc, "test.bug36819") |
| 1580 |
assert encoded.decode(enc) == "abcdx" * 51 |
| 1581 |
|
| 1582 |
def test_invalid(self): |
| 1583 |
import _codecs |
| 1584 |
with raises(ValueError): |
| 1585 |
_codecs.lookup_error('utf8\0') |
| 1586 |
with raises(ValueError): |
| 1587 |
_codecs.lookup('utf8\0') |