Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 2 | # |
| 3 | # test_multibytecodec.py |
| 4 | # Unit test for multibytecodec itself |
| 5 | # |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 6 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test.support import TESTFN |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 9 | import unittest, io, codecs, sys, os |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 10 | import _multibytecodec |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 11 | |
| 12 | ALL_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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 28 | |
| 29 | class Test_MultibyteCodec(unittest.TestCase): |
| 30 | |
| 31 | def test_nullcoding(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 32 | for enc in ALL_CJKENCODINGS: |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 33 | self.assertEqual(b''.decode(enc), '') |
| 34 | self.assertEqual(str(b'', enc), '') |
| 35 | self.assertEqual(''.encode(enc), b'') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | |
| 37 | def test_str_decode(self): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 38 | for enc in ALL_CJKENCODINGS: |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 39 | self.assertEqual('abcd'.encode(enc), b'abcd') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 40 | |
| 41 | def test_errorcallback_longindex(self): |
| 42 | dec = codecs.getdecoder('euc-kr') |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 43 | myreplace = lambda exc: ('', sys.maxsize+1) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 44 | codecs.register_error('test.cjktest', myreplace) |
| 45 | self.assertRaises(IndexError, dec, |
Antoine Pitrou | 44bf631 | 2009-01-22 11:59:55 +0000 | [diff] [blame] | 46 | b'apple\x92ham\x93spam', 'test.cjktest') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 47 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 48 | def test_codingspec(self): |
| 49 | try: |
| 50 | for enc in ALL_CJKENCODINGS: |
Antoine Pitrou | 92f60ed | 2010-10-14 22:11:44 +0000 | [diff] [blame] | 51 | code = '# coding: {}\n'.format(enc) |
| 52 | exec(code) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 53 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 54 | support.unlink(TESTFN) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 55 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 56 | 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 Pitrou | 44bf631 | 2009-01-22 11:59:55 +0000 | [diff] [blame] | 63 | 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 Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | class 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 Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 73 | self.assertEqual(encoder.encode('\ud30c\uc774\uc36c \ub9c8\uc744'), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 74 | b'\xc6\xc4\xc0\xcc\xbd\xe3 \xb8\xb6\xc0\xbb') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 75 | self.assertEqual(encoder.reset(), None) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 76 | self.assertEqual(encoder.encode('\u2606\u223c\u2606', True), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 77 | b'\xa1\xd9\xa1\xad\xa1\xd9') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | self.assertEqual(encoder.reset(), None) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 79 | self.assertEqual(encoder.encode('', True), b'') |
| 80 | self.assertEqual(encoder.encode('', False), b'') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 81 | 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 Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 90 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 94 | |
| 95 | self.assertEqual(encoder.reset(), None) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 96 | self.assertEqual(encoder.encode('\u0300'), b'\xab\xdc') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 98 | self.assertEqual(encoder.encode('\u00e6'), b'') |
| 99 | self.assertEqual(encoder.encode('', True), b'\xa9\xdc') |
| 100 | self.assertEqual(encoder.encode('', True), b'') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | |
| 102 | def test_stateful_keep_buffer(self): |
| 103 | encoder = codecs.getincrementalencoder('jisx0213')() |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 104 | self.assertEqual(encoder.encode('\u00e6'), b'') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 105 | self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 106 | self.assertEqual(encoder.encode('\u0300\u00e6'), b'\xab\xc4') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 107 | self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 108 | self.assertEqual(encoder.reset(), None) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 109 | self.assertEqual(encoder.encode('\u0300'), b'\xab\xdc') |
| 110 | self.assertEqual(encoder.encode('\u00e6'), b'') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 111 | self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 112 | self.assertEqual(encoder.encode('', True), b'\xa9\xdc') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | |
Hye-Shik Chang | 944820b | 2009-04-02 10:33:16 +0000 | [diff] [blame] | 114 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 118 | |
| 119 | class 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 Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 124 | self.assertEqual(decoder.decode(b'\xc6\xc4\xc0\xcc\xbd'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 125 | '\ud30c\uc774') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 126 | self.assertEqual(decoder.decode(b'\xe3 \xb8\xb6\xc0\xbb'), |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 127 | '\uc36c \ub9c8\uc744') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 128 | self.assertEqual(decoder.decode(b''), '') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 129 | |
| 130 | def test_dbcs_keep_buffer(self): |
| 131 | decoder = codecs.getincrementaldecoder('cp949')() |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 132 | self.assertEqual(decoder.decode(b'\xc6\xc4\xc0'), '\ud30c') |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 133 | self.assertRaises(UnicodeDecodeError, decoder.decode, b'', True) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 134 | self.assertEqual(decoder.decode(b'\xcc'), '\uc774') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 136 | self.assertEqual(decoder.decode(b'\xc6\xc4\xc0'), '\ud30c') |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 137 | self.assertRaises(UnicodeDecodeError, decoder.decode, |
| 138 | b'\xcc\xbd', True) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 139 | self.assertEqual(decoder.decode(b'\xcc'), '\uc774') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 140 | |
| 141 | def test_iso2022(self): |
| 142 | decoder = codecs.getincrementaldecoder('iso2022-jp')() |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 143 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 150 | self.assertEqual(decoder.reset(), None) |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 151 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 155 | |
Antoine Pitrou | 44bf631 | 2009-01-22 11:59:55 +0000 | [diff] [blame] | 156 | 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 Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 162 | class Test_StreamReader(unittest.TestCase): |
| 163 | def test_bug1728403(self): |
| 164 | try: |
Guido van Rossum | a222e4c | 2007-07-09 09:18:12 +0000 | [diff] [blame] | 165 | f = open(TESTFN, 'wb') |
| 166 | try: |
| 167 | f.write(b'\xa1') |
| 168 | finally: |
| 169 | f.close() |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 170 | f = codecs.open(TESTFN, encoding='cp949') |
Guido van Rossum | a222e4c | 2007-07-09 09:18:12 +0000 | [diff] [blame] | 171 | try: |
| 172 | self.assertRaises(UnicodeDecodeError, f.read, 2) |
| 173 | finally: |
| 174 | f.close() |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 175 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 176 | support.unlink(TESTFN) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 177 | |
| 178 | class Test_StreamWriter(unittest.TestCase): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 179 | if len('\U00012345') == 2: # UCS2 |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 180 | def test_gb18030(self): |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 181 | s= io.BytesIO() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 182 | c = codecs.getwriter('gb18030')(s) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 183 | c.write('123') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 184 | self.assertEqual(s.getvalue(), b'123') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 185 | c.write('\U00012345') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 186 | self.assertEqual(s.getvalue(), b'123\x907\x959') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 187 | c.write('\U00012345'[0]) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 188 | self.assertEqual(s.getvalue(), b'123\x907\x959') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 189 | c.write('\U00012345'[1] + '\U00012345' + '\uac00\u00ac') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 190 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 191 | b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 192 | c.write('\U00012345'[0]) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 193 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 194 | b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 195 | self.assertRaises(UnicodeError, c.reset) |
| 196 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 197 | b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 198 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 199 | def test_utf_8(self): |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 200 | s= io.BytesIO() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | c = codecs.getwriter('utf-8')(s) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 202 | c.write('123') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 203 | self.assertEqual(s.getvalue(), b'123') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 204 | c.write('\U00012345') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 205 | self.assertEqual(s.getvalue(), b'123\xf0\x92\x8d\x85') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 206 | |
| 207 | # Python utf-8 codec can't buffer surrogate pairs yet. |
| 208 | if 0: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 209 | c.write('\U00012345'[0]) |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 210 | self.assertEqual(s.getvalue(), b'123\xf0\x92\x8d\x85') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 211 | c.write('\U00012345'[1] + '\U00012345' + '\uac00\u00ac') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 212 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 213 | b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' |
| 214 | b'\xea\xb0\x80\xc2\xac') |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 215 | c.write('\U00012345'[0]) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 216 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 217 | b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85' |
| 218 | b'\xea\xb0\x80\xc2\xac') |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 219 | c.reset() |
| 220 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 221 | 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 Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 223 | c.write('\U00012345'[1]) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 224 | self.assertEqual(s.getvalue(), |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 225 | 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 Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 227 | |
| 228 | else: # UCS4 |
| 229 | pass |
| 230 | |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 231 | def test_streamwriter_strwrite(self): |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 232 | s = io.BytesIO() |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 233 | wr = codecs.getwriter('gb18030')(s) |
| 234 | wr.write('abcd') |
Guido van Rossum | 024da5c | 2007-05-17 23:59:11 +0000 | [diff] [blame] | 235 | self.assertEqual(s.getvalue(), b'abcd') |
Hye-Shik Chang | 2bb146f | 2004-07-18 03:06:29 +0000 | [diff] [blame] | 236 | |
Hye-Shik Chang | abb903f | 2006-03-13 10:20:08 +0000 | [diff] [blame] | 237 | class Test_ISO2022(unittest.TestCase): |
| 238 | def test_g2(self): |
Guido van Rossum | e22905a | 2007-08-27 23:09:25 +0000 | [diff] [blame] | 239 | iso2022jp2 = b'\x1b(B:hu4:unit\x1b.A\x1bNi de famille' |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 240 | uni = ':hu4:unit\xe9 de famille' |
Hye-Shik Chang | abb903f | 2006-03-13 10:20:08 +0000 | [diff] [blame] | 241 | self.assertEqual(iso2022jp2.decode('iso2022-jp-2'), uni) |
| 242 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 243 | def test_iso2022_jp_g0(self): |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 244 | self.assertNotIn(b'\x0e', '\N{SOFT HYPHEN}'.encode('iso-2022-jp-2')) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 245 | for encoding in ('iso-2022-jp-2004', 'iso-2022-jp-3'): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 246 | e = '\u3406'.encode(encoding) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 247 | self.assertFalse(any(x > 0x80 for x in e)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 248 | |
| 249 | def test_bug1572832(self): |
| 250 | if sys.maxunicode >= 0x10000: |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 251 | myunichr = chr |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 252 | else: |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 253 | myunichr = lambda x: chr(0xD7C0+(x>>10)) + chr(0xDC00+(x&0x3FF)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 255 | for x in range(0x10000, 0x110000): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | # Any ISO 2022 codec will cause the segfault |
| 257 | myunichr(x).encode('iso_2022_jp', 'ignore') |
| 258 | |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 259 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 260 | support.run_unittest(__name__) |
Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 261 | |
| 262 | if __name__ == "__main__": |
| 263 | test_main() |