Debian Codegraph @

pypy/interpreter/test/test_unicodehelper.py

raw symbols SCIP
1 # encoding: utf-8
2 import pytest
3
4 from pypy.interpreter.unicodehelper import (
5 utf8_encode_utf_8, decode_utf8sp,
6 )
7
8 from pypy.interpreter.unicodehelper import str_decode_utf8, utf8_encode_latin_1
9 from pypy.interpreter.unicodehelper import utf8_encode_ascii, str_decode_ascii
10 from pypy.interpreter.unicodehelper import str_decode_unicode_escape
11 from pypy.interpreter.unicodehelper import str_decode_raw_unicode_escape
12 from pypy.interpreter.unicodehelper import utf8_encode_utf_16_le
13 from pypy.interpreter.unicodehelper import utf8_encode_utf_32_le
14 from pypy.interpreter import unicodehelper as uh
15 from pypy.module._codecs.interp_codecs import CodecState
16
17 class Hit(Exception):
18 pass
19
20 class FakeSpace:
21 def __init__(self, space):
22 self.space = space
23 def __getattr__(self, name):
24 if name in ('w_UnicodeEncodeError', 'w_UnicodeDecodeError'):
25 raise Hit
26 raise AttributeError(name)
27 def newbytes(self, s):
28 return s
29 def newtext(self, s):
30 return self.space.newtext(s)
31
32 def test_encode_utf_8_combine_surrogates(space):
33 """
34 In the case of a surrogate pair, the error handler should
35 called with a start and stop position of the full surrogate
36 pair (new behavior in python3.6)
37 """
38 # /--surrogate pair--\
39 # \udc80 \ud800 \udfff
40 b = "\xed\xb2\x80\xed\xa0\x80\xed\xbf\xbf"
41 _space = FakeSpace(space)
42
43 calls = []
44
45 def errorhandler(errors, encoding, msg, w_s, start, end):
46 """
47 This handler will be called twice, so asserting both times:
48
49 1. the first time, 0xDC80 will be handled as a single surrogate,
50 since it is a standalone character and an invalid surrogate.
51 2. the second time, the characters will be 0xD800 and 0xDFFF, since
52 that is a valid surrogate pair.
53 """
54 s = w_s._utf8
55 calls.append(s.decode("utf-8")[start:end])
56 return 'abc', end, 'b', s, w_s
57 w_b = space.newtext(b)
58 res = utf8_encode_utf_8(
59 space, b, w_b, 'strict',
60 errorhandler=errorhandler,
61 allow_surrogates=False
62 )
63 assert res == "abcabc"
64 assert calls == [u'\udc80', u'\uD800\uDFFF']
65
66 #def test_bad_error_handler():
67 # replaced by the test test_repeated_pos_return
68 # in test_codecs. following CPython's approach
69
70 def test_decode_utf8sp():
71 space = FakeSpace(None)
72 assert decode_utf8sp(space, "\xed\xa0\x80") == ("\xed\xa0\x80", 1, 3)
73 assert decode_utf8sp(space, "\xed\xb0\x80") == ("\xed\xb0\x80", 1, 3)
74 got = decode_utf8sp(space, "\xed\xa0\x80\xed\xb0\x80")
75 assert map(ord, got[0].decode('utf8')) == [0xd800, 0xdc00]
76 got = decode_utf8sp(space, "\xf0\x90\x80\x80")
77 assert map(ord, got[0].decode('utf8')) == [0x10000]
78
79
80 def test_utf8_encode_latin1_ascii_prefix():
81 space = FakeSpace(None)
82 utf8 = b'abcde\xc3\xa4g'
83 b = utf8_encode_latin_1(space, utf8, utf8, None, None)
84 assert b == b'abcde\xe4g'
85
86 def test_latin1_shortcut_bug(space):
87 state = space.fromcache(CodecState)
88 handler = state.encode_error_handler
89
90 sin = u"a\xac\u1234\u20ac\u8000"
91 sin_utf8 = sin.encode("utf-8")
92 assert utf8_encode_latin_1(space, sin_utf8, space.newtext(sin_utf8), "backslashreplace", handler) == sin.encode("latin-1", "backslashreplace")
93
94 def test_unicode_escape_incremental_bug(space):
95 class FakeUnicodeDataHandler:
96 def call(self, name):
97 assert name == "QUESTION MARK"
98 return ord("?")
99 unicodedata_handler = FakeUnicodeDataHandler()
100 input = u"äҰ𐀂?"
101 data = b'\\xe4\\u04b0\\U00010002\\N{QUESTION MARK}'
102 for i in range(1, len(data)):
103 s = data[:i]
104 w_s = space.newtext(s)
105 result1, _, lgt1, _ = str_decode_unicode_escape(space, s, w_s, 'strict', False, None, unicodedata_handler)
106 s1 = data[lgt1:i] + data[i:]
107 w_s1 = space.newtext(s1)
108 result2, _, lgt2, _ = str_decode_unicode_escape(space, s1, w_s1, 'strict', True, None, unicodedata_handler)
109 assert lgt1 + lgt2 == len(data)
110 assert input == (result1 + result2).decode("utf-8")
111
112 def test_raw_unicode_escape_incremental_bug(space):
113 input = u"xҰa𐀂"
114 data = b'x\\u04b0a\\U00010002'
115 for i in range(1, len(data)):
116 s = data[:i]
117 w_s = space.newtext(s)
118 result1, _, lgt1 = str_decode_raw_unicode_escape(space, s, w_s, 'strict', False, None)
119 s = data[lgt1:i] + data[i:]
120 w_s = space.newtext(s)
121 result2, _, lgt2 = str_decode_raw_unicode_escape(space, s, w_s, 'strict', True, None)
122 assert lgt1 + lgt2 == len(data)
123 assert input == (result1 + result2).decode("utf-8")
124
125 def test_raw_unicode_escape_backslash_without_escape():
126 data = b'[:/?#[\\]@]\\'
127 space = FakeSpace(None)
128 result, _, l = str_decode_raw_unicode_escape(space, data, data, 'strict', True, None)
129 assert l == len(data)
130 assert result == data
131
132 def test_raw_unicode_escape_bug_escape_backslash():
133 data = b'\\\\'
134 space = FakeSpace(None)
135 res = str_decode_raw_unicode_escape(space, data, data, 'strict', True, None)
136 assert res[0] == '\\\\'
137
138 data = b'\\\xef'
139 res = str_decode_raw_unicode_escape(space, data, data, 'strict', True, None)
140 assert res[0].decode("utf-8") == u'\\\xef'
141
142 def test_utf16_encode_bytes_replacement_is_simply_copied():
143 space = FakeSpace(None)
144 def errorhandler(errors, encoding, msg, s, start, end):
145 return 'abcd', end, 'b', s, s
146
147 s = b'[\xed\xb2\x80]'
148 res = utf8_encode_utf_16_le(
149 space, s, s, 'strict',
150 errorhandler=errorhandler,
151 allow_surrogates=False
152 )
153 assert res == "[\x00abcd]\x00"
154
155
156 def test_utf32_encode_bytes_replacement_is_simply_copied(space):
157 _space = FakeSpace(space)
158 s = b'[\xed\xb2\x80]'
159 def errorhandler(errors, encoding, msg, w_s, start, end):
160 return 'abcd', end, 'b', s, w_s
161
162 res = utf8_encode_utf_32_le(
163 _space, s, space.newtext(s), 'strict',
164 errorhandler=errorhandler,
165 allow_surrogates=False
166 )
167 assert res == "[\x00\x00\x00abcd]\x00\x00\x00"
168