1 | n/a | # |
---|
2 | n/a | # multibytecodec_support.py |
---|
3 | n/a | # Common Unittest Routines for CJK codecs |
---|
4 | n/a | # |
---|
5 | n/a | |
---|
6 | n/a | import codecs |
---|
7 | n/a | import os |
---|
8 | n/a | import re |
---|
9 | n/a | import sys |
---|
10 | n/a | import unittest |
---|
11 | n/a | from http.client import HTTPException |
---|
12 | n/a | from test import support |
---|
13 | n/a | from io import BytesIO |
---|
14 | n/a | |
---|
15 | n/a | class TestBase: |
---|
16 | n/a | encoding = '' # codec name |
---|
17 | n/a | codec = None # codec tuple (with 4 elements) |
---|
18 | n/a | tstring = None # must set. 2 strings to test StreamReader |
---|
19 | n/a | |
---|
20 | n/a | codectests = None # must set. codec test tuple |
---|
21 | n/a | roundtriptest = 1 # set if roundtrip is possible with unicode |
---|
22 | n/a | has_iso10646 = 0 # set if this encoding contains whole iso10646 map |
---|
23 | n/a | xmlcharnametest = None # string to test xmlcharrefreplace |
---|
24 | n/a | unmappedunicode = '\udeee' # a unicode code point that is not mapped. |
---|
25 | n/a | |
---|
26 | n/a | def setUp(self): |
---|
27 | n/a | if self.codec is None: |
---|
28 | n/a | self.codec = codecs.lookup(self.encoding) |
---|
29 | n/a | self.encode = self.codec.encode |
---|
30 | n/a | self.decode = self.codec.decode |
---|
31 | n/a | self.reader = self.codec.streamreader |
---|
32 | n/a | self.writer = self.codec.streamwriter |
---|
33 | n/a | self.incrementalencoder = self.codec.incrementalencoder |
---|
34 | n/a | self.incrementaldecoder = self.codec.incrementaldecoder |
---|
35 | n/a | |
---|
36 | n/a | def test_chunkcoding(self): |
---|
37 | n/a | tstring_lines = [] |
---|
38 | n/a | for b in self.tstring: |
---|
39 | n/a | lines = b.split(b"\n") |
---|
40 | n/a | last = lines.pop() |
---|
41 | n/a | assert last == b"" |
---|
42 | n/a | lines = [line + b"\n" for line in lines] |
---|
43 | n/a | tstring_lines.append(lines) |
---|
44 | n/a | for native, utf8 in zip(*tstring_lines): |
---|
45 | n/a | u = self.decode(native)[0] |
---|
46 | n/a | self.assertEqual(u, utf8.decode('utf-8')) |
---|
47 | n/a | if self.roundtriptest: |
---|
48 | n/a | self.assertEqual(native, self.encode(u)[0]) |
---|
49 | n/a | |
---|
50 | n/a | def test_errorhandle(self): |
---|
51 | n/a | for source, scheme, expected in self.codectests: |
---|
52 | n/a | if isinstance(source, bytes): |
---|
53 | n/a | func = self.decode |
---|
54 | n/a | else: |
---|
55 | n/a | func = self.encode |
---|
56 | n/a | if expected: |
---|
57 | n/a | result = func(source, scheme)[0] |
---|
58 | n/a | if func is self.decode: |
---|
59 | n/a | self.assertTrue(type(result) is str, type(result)) |
---|
60 | n/a | self.assertEqual(result, expected, |
---|
61 | n/a | '%a.decode(%r, %r)=%a != %a' |
---|
62 | n/a | % (source, self.encoding, scheme, result, |
---|
63 | n/a | expected)) |
---|
64 | n/a | else: |
---|
65 | n/a | self.assertTrue(type(result) is bytes, type(result)) |
---|
66 | n/a | self.assertEqual(result, expected, |
---|
67 | n/a | '%a.encode(%r, %r)=%a != %a' |
---|
68 | n/a | % (source, self.encoding, scheme, result, |
---|
69 | n/a | expected)) |
---|
70 | n/a | else: |
---|
71 | n/a | self.assertRaises(UnicodeError, func, source, scheme) |
---|
72 | n/a | |
---|
73 | n/a | def test_xmlcharrefreplace(self): |
---|
74 | n/a | if self.has_iso10646: |
---|
75 | n/a | self.skipTest('encoding contains full ISO 10646 map') |
---|
76 | n/a | |
---|
77 | n/a | s = "\u0b13\u0b23\u0b60 nd eggs" |
---|
78 | n/a | self.assertEqual( |
---|
79 | n/a | self.encode(s, "xmlcharrefreplace")[0], |
---|
80 | n/a | b"ଓଣୠ nd eggs" |
---|
81 | n/a | ) |
---|
82 | n/a | |
---|
83 | n/a | def test_customreplace_encode(self): |
---|
84 | n/a | if self.has_iso10646: |
---|
85 | n/a | self.skipTest('encoding contains full ISO 10646 map') |
---|
86 | n/a | |
---|
87 | n/a | from html.entities import codepoint2name |
---|
88 | n/a | |
---|
89 | n/a | def xmlcharnamereplace(exc): |
---|
90 | n/a | if not isinstance(exc, UnicodeEncodeError): |
---|
91 | n/a | raise TypeError("don't know how to handle %r" % exc) |
---|
92 | n/a | l = [] |
---|
93 | n/a | for c in exc.object[exc.start:exc.end]: |
---|
94 | n/a | if ord(c) in codepoint2name: |
---|
95 | n/a | l.append("&%s;" % codepoint2name[ord(c)]) |
---|
96 | n/a | else: |
---|
97 | n/a | l.append("&#%d;" % ord(c)) |
---|
98 | n/a | return ("".join(l), exc.end) |
---|
99 | n/a | |
---|
100 | n/a | codecs.register_error("test.xmlcharnamereplace", xmlcharnamereplace) |
---|
101 | n/a | |
---|
102 | n/a | if self.xmlcharnametest: |
---|
103 | n/a | sin, sout = self.xmlcharnametest |
---|
104 | n/a | else: |
---|
105 | n/a | sin = "\xab\u211c\xbb = \u2329\u1234\u232a" |
---|
106 | n/a | sout = b"«ℜ» = ⟨ሴ⟩" |
---|
107 | n/a | self.assertEqual(self.encode(sin, |
---|
108 | n/a | "test.xmlcharnamereplace")[0], sout) |
---|
109 | n/a | |
---|
110 | n/a | def test_callback_returns_bytes(self): |
---|
111 | n/a | def myreplace(exc): |
---|
112 | n/a | return (b"1234", exc.end) |
---|
113 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
114 | n/a | enc = self.encode("abc" + self.unmappedunicode + "def", "test.cjktest")[0] |
---|
115 | n/a | self.assertEqual(enc, b"abc1234def") |
---|
116 | n/a | |
---|
117 | n/a | def test_callback_wrong_objects(self): |
---|
118 | n/a | def myreplace(exc): |
---|
119 | n/a | return (ret, exc.end) |
---|
120 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
121 | n/a | |
---|
122 | n/a | for ret in ([1, 2, 3], [], None, object()): |
---|
123 | n/a | self.assertRaises(TypeError, self.encode, self.unmappedunicode, |
---|
124 | n/a | 'test.cjktest') |
---|
125 | n/a | |
---|
126 | n/a | def test_callback_long_index(self): |
---|
127 | n/a | def myreplace(exc): |
---|
128 | n/a | return ('x', int(exc.end)) |
---|
129 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
130 | n/a | self.assertEqual(self.encode('abcd' + self.unmappedunicode + 'efgh', |
---|
131 | n/a | 'test.cjktest'), (b'abcdxefgh', 9)) |
---|
132 | n/a | |
---|
133 | n/a | def myreplace(exc): |
---|
134 | n/a | return ('x', sys.maxsize + 1) |
---|
135 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
136 | n/a | self.assertRaises(IndexError, self.encode, self.unmappedunicode, |
---|
137 | n/a | 'test.cjktest') |
---|
138 | n/a | |
---|
139 | n/a | def test_callback_None_index(self): |
---|
140 | n/a | def myreplace(exc): |
---|
141 | n/a | return ('x', None) |
---|
142 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
143 | n/a | self.assertRaises(TypeError, self.encode, self.unmappedunicode, |
---|
144 | n/a | 'test.cjktest') |
---|
145 | n/a | |
---|
146 | n/a | def test_callback_backward_index(self): |
---|
147 | n/a | def myreplace(exc): |
---|
148 | n/a | if myreplace.limit > 0: |
---|
149 | n/a | myreplace.limit -= 1 |
---|
150 | n/a | return ('REPLACED', 0) |
---|
151 | n/a | else: |
---|
152 | n/a | return ('TERMINAL', exc.end) |
---|
153 | n/a | myreplace.limit = 3 |
---|
154 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
155 | n/a | self.assertEqual(self.encode('abcd' + self.unmappedunicode + 'efgh', |
---|
156 | n/a | 'test.cjktest'), |
---|
157 | n/a | (b'abcdREPLACEDabcdREPLACEDabcdREPLACEDabcdTERMINALefgh', 9)) |
---|
158 | n/a | |
---|
159 | n/a | def test_callback_forward_index(self): |
---|
160 | n/a | def myreplace(exc): |
---|
161 | n/a | return ('REPLACED', exc.end + 2) |
---|
162 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
163 | n/a | self.assertEqual(self.encode('abcd' + self.unmappedunicode + 'efgh', |
---|
164 | n/a | 'test.cjktest'), (b'abcdREPLACEDgh', 9)) |
---|
165 | n/a | |
---|
166 | n/a | def test_callback_index_outofbound(self): |
---|
167 | n/a | def myreplace(exc): |
---|
168 | n/a | return ('TERM', 100) |
---|
169 | n/a | codecs.register_error("test.cjktest", myreplace) |
---|
170 | n/a | self.assertRaises(IndexError, self.encode, self.unmappedunicode, |
---|
171 | n/a | 'test.cjktest') |
---|
172 | n/a | |
---|
173 | n/a | def test_incrementalencoder(self): |
---|
174 | n/a | UTF8Reader = codecs.getreader('utf-8') |
---|
175 | n/a | for sizehint in [None] + list(range(1, 33)) + \ |
---|
176 | n/a | [64, 128, 256, 512, 1024]: |
---|
177 | n/a | istream = UTF8Reader(BytesIO(self.tstring[1])) |
---|
178 | n/a | ostream = BytesIO() |
---|
179 | n/a | encoder = self.incrementalencoder() |
---|
180 | n/a | while 1: |
---|
181 | n/a | if sizehint is not None: |
---|
182 | n/a | data = istream.read(sizehint) |
---|
183 | n/a | else: |
---|
184 | n/a | data = istream.read() |
---|
185 | n/a | |
---|
186 | n/a | if not data: |
---|
187 | n/a | break |
---|
188 | n/a | e = encoder.encode(data) |
---|
189 | n/a | ostream.write(e) |
---|
190 | n/a | |
---|
191 | n/a | self.assertEqual(ostream.getvalue(), self.tstring[0]) |
---|
192 | n/a | |
---|
193 | n/a | def test_incrementaldecoder(self): |
---|
194 | n/a | UTF8Writer = codecs.getwriter('utf-8') |
---|
195 | n/a | for sizehint in [None, -1] + list(range(1, 33)) + \ |
---|
196 | n/a | [64, 128, 256, 512, 1024]: |
---|
197 | n/a | istream = BytesIO(self.tstring[0]) |
---|
198 | n/a | ostream = UTF8Writer(BytesIO()) |
---|
199 | n/a | decoder = self.incrementaldecoder() |
---|
200 | n/a | while 1: |
---|
201 | n/a | data = istream.read(sizehint) |
---|
202 | n/a | if not data: |
---|
203 | n/a | break |
---|
204 | n/a | else: |
---|
205 | n/a | u = decoder.decode(data) |
---|
206 | n/a | ostream.write(u) |
---|
207 | n/a | |
---|
208 | n/a | self.assertEqual(ostream.getvalue(), self.tstring[1]) |
---|
209 | n/a | |
---|
210 | n/a | def test_incrementalencoder_error_callback(self): |
---|
211 | n/a | inv = self.unmappedunicode |
---|
212 | n/a | |
---|
213 | n/a | e = self.incrementalencoder() |
---|
214 | n/a | self.assertRaises(UnicodeEncodeError, e.encode, inv, True) |
---|
215 | n/a | |
---|
216 | n/a | e.errors = 'ignore' |
---|
217 | n/a | self.assertEqual(e.encode(inv, True), b'') |
---|
218 | n/a | |
---|
219 | n/a | e.reset() |
---|
220 | n/a | def tempreplace(exc): |
---|
221 | n/a | return ('called', exc.end) |
---|
222 | n/a | codecs.register_error('test.incremental_error_callback', tempreplace) |
---|
223 | n/a | e.errors = 'test.incremental_error_callback' |
---|
224 | n/a | self.assertEqual(e.encode(inv, True), b'called') |
---|
225 | n/a | |
---|
226 | n/a | # again |
---|
227 | n/a | e.errors = 'ignore' |
---|
228 | n/a | self.assertEqual(e.encode(inv, True), b'') |
---|
229 | n/a | |
---|
230 | n/a | def test_streamreader(self): |
---|
231 | n/a | UTF8Writer = codecs.getwriter('utf-8') |
---|
232 | n/a | for name in ["read", "readline", "readlines"]: |
---|
233 | n/a | for sizehint in [None, -1] + list(range(1, 33)) + \ |
---|
234 | n/a | [64, 128, 256, 512, 1024]: |
---|
235 | n/a | istream = self.reader(BytesIO(self.tstring[0])) |
---|
236 | n/a | ostream = UTF8Writer(BytesIO()) |
---|
237 | n/a | func = getattr(istream, name) |
---|
238 | n/a | while 1: |
---|
239 | n/a | data = func(sizehint) |
---|
240 | n/a | if not data: |
---|
241 | n/a | break |
---|
242 | n/a | if name == "readlines": |
---|
243 | n/a | ostream.writelines(data) |
---|
244 | n/a | else: |
---|
245 | n/a | ostream.write(data) |
---|
246 | n/a | |
---|
247 | n/a | self.assertEqual(ostream.getvalue(), self.tstring[1]) |
---|
248 | n/a | |
---|
249 | n/a | def test_streamwriter(self): |
---|
250 | n/a | readfuncs = ('read', 'readline', 'readlines') |
---|
251 | n/a | UTF8Reader = codecs.getreader('utf-8') |
---|
252 | n/a | for name in readfuncs: |
---|
253 | n/a | for sizehint in [None] + list(range(1, 33)) + \ |
---|
254 | n/a | [64, 128, 256, 512, 1024]: |
---|
255 | n/a | istream = UTF8Reader(BytesIO(self.tstring[1])) |
---|
256 | n/a | ostream = self.writer(BytesIO()) |
---|
257 | n/a | func = getattr(istream, name) |
---|
258 | n/a | while 1: |
---|
259 | n/a | if sizehint is not None: |
---|
260 | n/a | data = func(sizehint) |
---|
261 | n/a | else: |
---|
262 | n/a | data = func() |
---|
263 | n/a | |
---|
264 | n/a | if not data: |
---|
265 | n/a | break |
---|
266 | n/a | if name == "readlines": |
---|
267 | n/a | ostream.writelines(data) |
---|
268 | n/a | else: |
---|
269 | n/a | ostream.write(data) |
---|
270 | n/a | |
---|
271 | n/a | self.assertEqual(ostream.getvalue(), self.tstring[0]) |
---|
272 | n/a | |
---|
273 | n/a | def test_streamwriter_reset_no_pending(self): |
---|
274 | n/a | # Issue #23247: Calling reset() on a fresh StreamWriter instance |
---|
275 | n/a | # (without pending data) must not crash |
---|
276 | n/a | stream = BytesIO() |
---|
277 | n/a | writer = self.writer(stream) |
---|
278 | n/a | writer.reset() |
---|
279 | n/a | |
---|
280 | n/a | |
---|
281 | n/a | class TestBase_Mapping(unittest.TestCase): |
---|
282 | n/a | pass_enctest = [] |
---|
283 | n/a | pass_dectest = [] |
---|
284 | n/a | supmaps = [] |
---|
285 | n/a | codectests = [] |
---|
286 | n/a | |
---|
287 | n/a | def setUp(self): |
---|
288 | n/a | try: |
---|
289 | n/a | self.open_mapping_file().close() # test it to report the error early |
---|
290 | n/a | except (OSError, HTTPException): |
---|
291 | n/a | self.skipTest("Could not retrieve "+self.mapfileurl) |
---|
292 | n/a | |
---|
293 | n/a | def open_mapping_file(self): |
---|
294 | n/a | return support.open_urlresource(self.mapfileurl) |
---|
295 | n/a | |
---|
296 | n/a | def test_mapping_file(self): |
---|
297 | n/a | if self.mapfileurl.endswith('.xml'): |
---|
298 | n/a | self._test_mapping_file_ucm() |
---|
299 | n/a | else: |
---|
300 | n/a | self._test_mapping_file_plain() |
---|
301 | n/a | |
---|
302 | n/a | def _test_mapping_file_plain(self): |
---|
303 | n/a | unichrs = lambda s: ''.join(map(chr, map(eval, s.split('+')))) |
---|
304 | n/a | urt_wa = {} |
---|
305 | n/a | |
---|
306 | n/a | with self.open_mapping_file() as f: |
---|
307 | n/a | for line in f: |
---|
308 | n/a | if not line: |
---|
309 | n/a | break |
---|
310 | n/a | data = line.split('#')[0].strip().split() |
---|
311 | n/a | if len(data) != 2: |
---|
312 | n/a | continue |
---|
313 | n/a | |
---|
314 | n/a | csetval = eval(data[0]) |
---|
315 | n/a | if csetval <= 0x7F: |
---|
316 | n/a | csetch = bytes([csetval & 0xff]) |
---|
317 | n/a | elif csetval >= 0x1000000: |
---|
318 | n/a | csetch = bytes([(csetval >> 24), ((csetval >> 16) & 0xff), |
---|
319 | n/a | ((csetval >> 8) & 0xff), (csetval & 0xff)]) |
---|
320 | n/a | elif csetval >= 0x10000: |
---|
321 | n/a | csetch = bytes([(csetval >> 16), ((csetval >> 8) & 0xff), |
---|
322 | n/a | (csetval & 0xff)]) |
---|
323 | n/a | elif csetval >= 0x100: |
---|
324 | n/a | csetch = bytes([(csetval >> 8), (csetval & 0xff)]) |
---|
325 | n/a | else: |
---|
326 | n/a | continue |
---|
327 | n/a | |
---|
328 | n/a | unich = unichrs(data[1]) |
---|
329 | n/a | if ord(unich) == 0xfffd or unich in urt_wa: |
---|
330 | n/a | continue |
---|
331 | n/a | urt_wa[unich] = csetch |
---|
332 | n/a | |
---|
333 | n/a | self._testpoint(csetch, unich) |
---|
334 | n/a | |
---|
335 | n/a | def _test_mapping_file_ucm(self): |
---|
336 | n/a | with self.open_mapping_file() as f: |
---|
337 | n/a | ucmdata = f.read() |
---|
338 | n/a | uc = re.findall('<a u="([A-F0-9]{4})" b="([0-9A-F ]+)"/>', ucmdata) |
---|
339 | n/a | for uni, coded in uc: |
---|
340 | n/a | unich = chr(int(uni, 16)) |
---|
341 | n/a | codech = bytes.fromhex(coded) |
---|
342 | n/a | self._testpoint(codech, unich) |
---|
343 | n/a | |
---|
344 | n/a | def test_mapping_supplemental(self): |
---|
345 | n/a | for mapping in self.supmaps: |
---|
346 | n/a | self._testpoint(*mapping) |
---|
347 | n/a | |
---|
348 | n/a | def _testpoint(self, csetch, unich): |
---|
349 | n/a | if (csetch, unich) not in self.pass_enctest: |
---|
350 | n/a | self.assertEqual(unich.encode(self.encoding), csetch) |
---|
351 | n/a | if (csetch, unich) not in self.pass_dectest: |
---|
352 | n/a | self.assertEqual(str(csetch, self.encoding), unich) |
---|
353 | n/a | |
---|
354 | n/a | def test_errorhandle(self): |
---|
355 | n/a | for source, scheme, expected in self.codectests: |
---|
356 | n/a | if isinstance(source, bytes): |
---|
357 | n/a | func = source.decode |
---|
358 | n/a | else: |
---|
359 | n/a | func = source.encode |
---|
360 | n/a | if expected: |
---|
361 | n/a | if isinstance(source, bytes): |
---|
362 | n/a | result = func(self.encoding, scheme) |
---|
363 | n/a | self.assertTrue(type(result) is str, type(result)) |
---|
364 | n/a | self.assertEqual(result, expected, |
---|
365 | n/a | '%a.decode(%r, %r)=%a != %a' |
---|
366 | n/a | % (source, self.encoding, scheme, result, |
---|
367 | n/a | expected)) |
---|
368 | n/a | else: |
---|
369 | n/a | result = func(self.encoding, scheme) |
---|
370 | n/a | self.assertTrue(type(result) is bytes, type(result)) |
---|
371 | n/a | self.assertEqual(result, expected, |
---|
372 | n/a | '%a.encode(%r, %r)=%a != %a' |
---|
373 | n/a | % (source, self.encoding, scheme, result, |
---|
374 | n/a | expected)) |
---|
375 | n/a | else: |
---|
376 | n/a | self.assertRaises(UnicodeError, func, self.encoding, scheme) |
---|
377 | n/a | |
---|
378 | n/a | def load_teststring(name): |
---|
379 | n/a | dir = os.path.join(os.path.dirname(__file__), 'cjkencodings') |
---|
380 | n/a | with open(os.path.join(dir, name + '.txt'), 'rb') as f: |
---|
381 | n/a | encoded = f.read() |
---|
382 | n/a | with open(os.path.join(dir, name + '-utf8.txt'), 'rb') as f: |
---|
383 | n/a | utf8 = f.read() |
---|
384 | n/a | return encoded, utf8 |
---|