blob: 44480723baf1fa149f45ed108959edd11b64938a [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#!/usr/bin/env python3
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00002#
3# test_multibytecodec.py
4# Unit test for multibytecodec itself
5#
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +00006
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test import support
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test.support import TESTFN
Guido van Rossum024da5c2007-05-17 23:59:11 +00009import unittest, io, codecs, sys, os
Georg Brandl3dbca812008-07-23 16:10:53 +000010import _multibytecodec
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011
12ALL_CJKENCODINGS = [
13# _codecs_cn
14 'gb2312', 'gbk', 'gb18030', 'hz',
15# _codecs_hk
16 'big5hkscs',
17# _codecs_jp
18 'cp932', 'shift_jis', 'euc_jp', 'euc_jisx0213', 'shift_jisx0213',
19 'euc_jis_2004', 'shift_jis_2004',
20# _codecs_kr
21 'cp949', 'euc_kr', 'johab',
22# _codecs_tw
23 'big5', 'cp950',
24# _codecs_iso2022
25 'iso2022_jp', 'iso2022_jp_1', 'iso2022_jp_2', 'iso2022_jp_2004',
26 'iso2022_jp_3', 'iso2022_jp_ext', 'iso2022_kr',
27]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028
29class Test_MultibyteCodec(unittest.TestCase):
30
31 def test_nullcoding(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000032 for enc in ALL_CJKENCODINGS:
Guido van Rossum024da5c2007-05-17 23:59:11 +000033 self.assertEqual(b''.decode(enc), '')
34 self.assertEqual(str(b'', enc), '')
35 self.assertEqual(''.encode(enc), b'')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036
37 def test_str_decode(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000038 for enc in ALL_CJKENCODINGS:
Guido van Rossum024da5c2007-05-17 23:59:11 +000039 self.assertEqual('abcd'.encode(enc), b'abcd')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040
41 def test_errorcallback_longindex(self):
42 dec = codecs.getdecoder('euc-kr')
Christian Heimesa37d4c62007-12-04 23:02:19 +000043 myreplace = lambda exc: ('', sys.maxsize+1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044 codecs.register_error('test.cjktest', myreplace)
45 self.assertRaises(IndexError, dec,
Antoine Pitrou44bf6312009-01-22 11:59:55 +000046 b'apple\x92ham\x93spam', 'test.cjktest')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047
Thomas Wouters0e3f5912006-08-11 14:57:12 +000048 def test_codingspec(self):
49 try:
50 for enc in ALL_CJKENCODINGS:
Antoine Pitrou92f60ed2010-10-14 22:11:44 +000051 code = '# coding: {}\n'.format(enc)
52 exec(code)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000053 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000054 support.unlink(TESTFN)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000055
Georg Brandl3dbca812008-07-23 16:10:53 +000056 def test_init_segfault(self):
57 # bug #3305: this used to segfault
58 self.assertRaises(AttributeError,
59 _multibytecodec.MultibyteStreamReader, None)
60 self.assertRaises(AttributeError,
61 _multibytecodec.MultibyteStreamWriter, None)
62
Antoine Pitrou44bf6312009-01-22 11:59:55 +000063 def test_decode_unicode(self):
64 # Trying to decode an unicode string should raise a TypeError
65 for enc in ALL_CJKENCODINGS:
66 self.assertRaises(TypeError, codecs.getdecoder(enc), "")
Georg Brandl3dbca812008-07-23 16:10:53 +000067
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068class Test_IncrementalEncoder(unittest.TestCase):
69
70 def test_stateless(self):
71 # cp949 encoder isn't stateful at all.
72 encoder = codecs.getincrementalencoder('cp949')()
Guido van Rossumef87d6e2007-05-02 19:09:54 +000073 self.assertEqual(encoder.encode('\ud30c\uc774\uc36c \ub9c8\uc744'),
Guido van Rossum024da5c2007-05-17 23:59:11 +000074 b'\xc6\xc4\xc0\xcc\xbd\xe3 \xb8\xb6\xc0\xbb')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075 self.assertEqual(encoder.reset(), None)
Guido van Rossumef87d6e2007-05-02 19:09:54 +000076 self.assertEqual(encoder.encode('\u2606\u223c\u2606', True),
Guido van Rossum024da5c2007-05-17 23:59:11 +000077 b'\xa1\xd9\xa1\xad\xa1\xd9')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 self.assertEqual(encoder.reset(), None)
Guido van Rossum024da5c2007-05-17 23:59:11 +000079 self.assertEqual(encoder.encode('', True), b'')
80 self.assertEqual(encoder.encode('', False), b'')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000081 self.assertEqual(encoder.reset(), None)
82
83 def test_stateful(self):
84 # jisx0213 encoder is stateful for a few codepoints. eg)
85 # U+00E6 => A9DC
86 # U+00E6 U+0300 => ABC4
87 # U+0300 => ABDC
88
89 encoder = codecs.getincrementalencoder('jisx0213')()
Guido van Rossum024da5c2007-05-17 23:59:11 +000090 self.assertEqual(encoder.encode('\u00e6\u0300'), b'\xab\xc4')
91 self.assertEqual(encoder.encode('\u00e6'), b'')
92 self.assertEqual(encoder.encode('\u0300'), b'\xab\xc4')
93 self.assertEqual(encoder.encode('\u00e6', True), b'\xa9\xdc')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094
95 self.assertEqual(encoder.reset(), None)
Guido van Rossum024da5c2007-05-17 23:59:11 +000096 self.assertEqual(encoder.encode('\u0300'), b'\xab\xdc')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097
Guido van Rossum024da5c2007-05-17 23:59:11 +000098 self.assertEqual(encoder.encode('\u00e6'), b'')
99 self.assertEqual(encoder.encode('', True), b'\xa9\xdc')
100 self.assertEqual(encoder.encode('', True), b'')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101
102 def test_stateful_keep_buffer(self):
103 encoder = codecs.getincrementalencoder('jisx0213')()
Guido van Rossum024da5c2007-05-17 23:59:11 +0000104 self.assertEqual(encoder.encode('\u00e6'), b'')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000105 self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000106 self.assertEqual(encoder.encode('\u0300\u00e6'), b'\xab\xc4')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000107 self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 self.assertEqual(encoder.reset(), None)
Guido van Rossum024da5c2007-05-17 23:59:11 +0000109 self.assertEqual(encoder.encode('\u0300'), b'\xab\xdc')
110 self.assertEqual(encoder.encode('\u00e6'), b'')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000111 self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000112 self.assertEqual(encoder.encode('', True), b'\xa9\xdc')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113
Hye-Shik Chang944820b2009-04-02 10:33:16 +0000114 def test_issue5640(self):
115 encoder = codecs.getincrementalencoder('shift-jis')('backslashreplace')
116 self.assertEqual(encoder.encode('\xff'), b'\\xff')
117 self.assertEqual(encoder.encode('\n'), b'\n')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118
119class Test_IncrementalDecoder(unittest.TestCase):
120
121 def test_dbcs(self):
122 # cp949 decoder is simple with only 1 or 2 bytes sequences.
123 decoder = codecs.getincrementaldecoder('cp949')()
Guido van Rossum024da5c2007-05-17 23:59:11 +0000124 self.assertEqual(decoder.decode(b'\xc6\xc4\xc0\xcc\xbd'),
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000125 '\ud30c\uc774')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000126 self.assertEqual(decoder.decode(b'\xe3 \xb8\xb6\xc0\xbb'),
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000127 '\uc36c \ub9c8\uc744')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000128 self.assertEqual(decoder.decode(b''), '')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129
130 def test_dbcs_keep_buffer(self):
131 decoder = codecs.getincrementaldecoder('cp949')()
Guido van Rossum024da5c2007-05-17 23:59:11 +0000132 self.assertEqual(decoder.decode(b'\xc6\xc4\xc0'), '\ud30c')
Guido van Rossume22905a2007-08-27 23:09:25 +0000133 self.assertRaises(UnicodeDecodeError, decoder.decode, b'', True)
Guido van Rossum024da5c2007-05-17 23:59:11 +0000134 self.assertEqual(decoder.decode(b'\xcc'), '\uc774')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135
Guido van Rossum024da5c2007-05-17 23:59:11 +0000136 self.assertEqual(decoder.decode(b'\xc6\xc4\xc0'), '\ud30c')
Guido van Rossume22905a2007-08-27 23:09:25 +0000137 self.assertRaises(UnicodeDecodeError, decoder.decode,
138 b'\xcc\xbd', True)
Guido van Rossum024da5c2007-05-17 23:59:11 +0000139 self.assertEqual(decoder.decode(b'\xcc'), '\uc774')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140
141 def test_iso2022(self):
142 decoder = codecs.getincrementaldecoder('iso2022-jp')()
Guido van Rossume22905a2007-08-27 23:09:25 +0000143 ESC = b'\x1b'
144 self.assertEqual(decoder.decode(ESC + b'('), '')
145 self.assertEqual(decoder.decode(b'B', True), '')
146 self.assertEqual(decoder.decode(ESC + b'$'), '')
147 self.assertEqual(decoder.decode(b'B@$'), '\u4e16')
148 self.assertEqual(decoder.decode(b'@$@'), '\u4e16')
149 self.assertEqual(decoder.decode(b'$', True), '\u4e16')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 self.assertEqual(decoder.reset(), None)
Guido van Rossume22905a2007-08-27 23:09:25 +0000151 self.assertEqual(decoder.decode(b'@$'), '@$')
152 self.assertEqual(decoder.decode(ESC + b'$'), '')
153 self.assertRaises(UnicodeDecodeError, decoder.decode, b'', True)
154 self.assertEqual(decoder.decode(b'B@$'), '\u4e16')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155
Antoine Pitrou44bf6312009-01-22 11:59:55 +0000156 def test_decode_unicode(self):
157 # Trying to decode an unicode string should raise a TypeError
158 for enc in ALL_CJKENCODINGS:
159 decoder = codecs.getincrementaldecoder(enc)()
160 self.assertRaises(TypeError, decoder.decode, "")
161
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000162class Test_StreamReader(unittest.TestCase):
163 def test_bug1728403(self):
164 try:
Guido van Rossuma222e4c2007-07-09 09:18:12 +0000165 f = open(TESTFN, 'wb')
166 try:
167 f.write(b'\xa1')
168 finally:
169 f.close()
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000170 f = codecs.open(TESTFN, encoding='cp949')
Guido van Rossuma222e4c2007-07-09 09:18:12 +0000171 try:
172 self.assertRaises(UnicodeDecodeError, f.read, 2)
173 finally:
174 f.close()
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000175 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000176 support.unlink(TESTFN)
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000177
178class Test_StreamWriter(unittest.TestCase):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000179 if len('\U00012345') == 2: # UCS2
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000180 def test_gb18030(self):
Guido van Rossum024da5c2007-05-17 23:59:11 +0000181 s= io.BytesIO()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 c = codecs.getwriter('gb18030')(s)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000183 c.write('123')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000184 self.assertEqual(s.getvalue(), b'123')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000185 c.write('\U00012345')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000186 self.assertEqual(s.getvalue(), b'123\x907\x959')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000187 c.write('\U00012345'[0])
Guido van Rossum024da5c2007-05-17 23:59:11 +0000188 self.assertEqual(s.getvalue(), b'123\x907\x959')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000189 c.write('\U00012345'[1] + '\U00012345' + '\uac00\u00ac')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000190 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000191 b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000192 c.write('\U00012345'[0])
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000193 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000194 b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000195 self.assertRaises(UnicodeError, c.reset)
196 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000197 b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199 def test_utf_8(self):
Guido van Rossum024da5c2007-05-17 23:59:11 +0000200 s= io.BytesIO()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201 c = codecs.getwriter('utf-8')(s)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000202 c.write('123')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000203 self.assertEqual(s.getvalue(), b'123')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000204 c.write('\U00012345')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000205 self.assertEqual(s.getvalue(), b'123\xf0\x92\x8d\x85')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206
207 # Python utf-8 codec can't buffer surrogate pairs yet.
208 if 0:
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000209 c.write('\U00012345'[0])
Guido van Rossum024da5c2007-05-17 23:59:11 +0000210 self.assertEqual(s.getvalue(), b'123\xf0\x92\x8d\x85')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000211 c.write('\U00012345'[1] + '\U00012345' + '\uac00\u00ac')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000212 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000213 b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
214 b'\xea\xb0\x80\xc2\xac')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000215 c.write('\U00012345'[0])
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000216 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000217 b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
218 b'\xea\xb0\x80\xc2\xac')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000219 c.reset()
220 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000221 b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
222 b'\xea\xb0\x80\xc2\xac\xed\xa0\x88')
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000223 c.write('\U00012345'[1])
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000224 self.assertEqual(s.getvalue(),
Guido van Rossum024da5c2007-05-17 23:59:11 +0000225 b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
226 b'\xea\xb0\x80\xc2\xac\xed\xa0\x88\xed\xbd\x85')
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000227
228 else: # UCS4
229 pass
230
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000231 def test_streamwriter_strwrite(self):
Guido van Rossum024da5c2007-05-17 23:59:11 +0000232 s = io.BytesIO()
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000233 wr = codecs.getwriter('gb18030')(s)
234 wr.write('abcd')
Guido van Rossum024da5c2007-05-17 23:59:11 +0000235 self.assertEqual(s.getvalue(), b'abcd')
Hye-Shik Chang2bb146f2004-07-18 03:06:29 +0000236
Hye-Shik Changabb903f2006-03-13 10:20:08 +0000237class Test_ISO2022(unittest.TestCase):
238 def test_g2(self):
Guido van Rossume22905a2007-08-27 23:09:25 +0000239 iso2022jp2 = b'\x1b(B:hu4:unit\x1b.A\x1bNi de famille'
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000240 uni = ':hu4:unit\xe9 de famille'
Hye-Shik Changabb903f2006-03-13 10:20:08 +0000241 self.assertEqual(iso2022jp2.decode('iso2022-jp-2'), uni)
242
Thomas Wouters89f507f2006-12-13 04:49:30 +0000243 def test_iso2022_jp_g0(self):
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000244 self.assertNotIn(b'\x0e', '\N{SOFT HYPHEN}'.encode('iso-2022-jp-2'))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000245 for encoding in ('iso-2022-jp-2004', 'iso-2022-jp-3'):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000246 e = '\u3406'.encode(encoding)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000247 self.assertFalse(any(x > 0x80 for x in e))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000248
249 def test_bug1572832(self):
250 if sys.maxunicode >= 0x10000:
Guido van Rossum84fc66d2007-05-03 17:18:26 +0000251 myunichr = chr
Thomas Wouters89f507f2006-12-13 04:49:30 +0000252 else:
Guido van Rossum84fc66d2007-05-03 17:18:26 +0000253 myunichr = lambda x: chr(0xD7C0+(x>>10)) + chr(0xDC00+(x&0x3FF))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000254
Guido van Rossum805365e2007-05-07 22:24:25 +0000255 for x in range(0x10000, 0x110000):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000256 # Any ISO 2022 codec will cause the segfault
257 myunichr(x).encode('iso_2022_jp', 'ignore')
258
Victor Stinner6bcbef72011-05-24 22:17:55 +0200259class TestStateful(unittest.TestCase):
260 text = '\u4E16\u4E16'
261 encoding = 'iso-2022-jp'
262 expected = b'\x1b$B@$@$'
Victor Stinnere15dce32011-05-30 22:56:00 +0200263 reset = b'\x1b(B'
264 expected_reset = expected + reset
Victor Stinner6bcbef72011-05-24 22:17:55 +0200265
266 def test_encode(self):
267 self.assertEqual(self.text.encode(self.encoding), self.expected_reset)
268
269 def test_incrementalencoder(self):
270 encoder = codecs.getincrementalencoder(self.encoding)()
271 output = b''.join(
272 encoder.encode(char)
273 for char in self.text)
274 self.assertEqual(output, self.expected)
Victor Stinnere15dce32011-05-30 22:56:00 +0200275 self.assertEqual(encoder.encode('', final=True), self.reset)
276 self.assertEqual(encoder.encode('', final=True), b'')
Victor Stinner6bcbef72011-05-24 22:17:55 +0200277
278 def test_incrementalencoder_final(self):
279 encoder = codecs.getincrementalencoder(self.encoding)()
280 last_index = len(self.text) - 1
281 output = b''.join(
282 encoder.encode(char, index == last_index)
283 for index, char in enumerate(self.text))
284 self.assertEqual(output, self.expected_reset)
Victor Stinnere15dce32011-05-30 22:56:00 +0200285 self.assertEqual(encoder.encode('', final=True), b'')
Victor Stinner6bcbef72011-05-24 22:17:55 +0200286
287class TestHZStateful(TestStateful):
288 text = '\u804a\u804a'
289 encoding = 'hz'
290 expected = b'~{ADAD'
Victor Stinnere15dce32011-05-30 22:56:00 +0200291 reset = b'~}'
292 expected_reset = expected + reset
Victor Stinner6bcbef72011-05-24 22:17:55 +0200293
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000294def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000295 support.run_unittest(__name__)
Hye-Shik Chang3e2a3062004-01-17 14:29:29 +0000296
297if __name__ == "__main__":
298 test_main()