mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
Make all the multibyte codec tests pass.
Changes to io.py, necessary to make this work: - Redid io.StringIO as a TextIOWrapper on top of a BytesIO instance. - Got rid of _MemoryIOMixin, folding it into BytesIO instead. - The read() functions that take -1 to mean "eveything" now also take None. - Added readline() support to BufferedIOBase. :-(
This commit is contained in:
parent
f4cfc8f6bb
commit
024da5c257
10 changed files with 1225 additions and 1227 deletions
|
@ -7,7 +7,7 @@
|
|||
from test import test_support
|
||||
from test import test_multibytecodec_support
|
||||
from test.test_support import TESTFN
|
||||
import unittest, StringIO, codecs, sys, os
|
||||
import unittest, io, codecs, sys, os
|
||||
|
||||
ALL_CJKENCODINGS = [
|
||||
# _codecs_cn
|
||||
|
@ -30,13 +30,13 @@ class Test_MultibyteCodec(unittest.TestCase):
|
|||
|
||||
def test_nullcoding(self):
|
||||
for enc in ALL_CJKENCODINGS:
|
||||
self.assertEqual(''.decode(enc), '')
|
||||
self.assertEqual(str('', enc), '')
|
||||
self.assertEqual(''.encode(enc), '')
|
||||
self.assertEqual(b''.decode(enc), '')
|
||||
self.assertEqual(str(b'', enc), '')
|
||||
self.assertEqual(''.encode(enc), b'')
|
||||
|
||||
def test_str_decode(self):
|
||||
for enc in ALL_CJKENCODINGS:
|
||||
self.assertEqual('abcd'.encode(enc), 'abcd')
|
||||
self.assertEqual('abcd'.encode(enc), b'abcd')
|
||||
|
||||
def test_errorcallback_longindex(self):
|
||||
dec = codecs.getdecoder('euc-kr')
|
||||
|
@ -48,7 +48,7 @@ class Test_MultibyteCodec(unittest.TestCase):
|
|||
def test_codingspec(self):
|
||||
try:
|
||||
for enc in ALL_CJKENCODINGS:
|
||||
print('# coding:', enc, file=open(TESTFN, 'w'))
|
||||
print('# coding:', enc, file=io.open(TESTFN, 'w'))
|
||||
execfile(TESTFN)
|
||||
finally:
|
||||
os.unlink(TESTFN)
|
||||
|
@ -59,13 +59,13 @@ class Test_IncrementalEncoder(unittest.TestCase):
|
|||
# cp949 encoder isn't stateful at all.
|
||||
encoder = codecs.getincrementalencoder('cp949')()
|
||||
self.assertEqual(encoder.encode('\ud30c\uc774\uc36c \ub9c8\uc744'),
|
||||
'\xc6\xc4\xc0\xcc\xbd\xe3 \xb8\xb6\xc0\xbb')
|
||||
b'\xc6\xc4\xc0\xcc\xbd\xe3 \xb8\xb6\xc0\xbb')
|
||||
self.assertEqual(encoder.reset(), None)
|
||||
self.assertEqual(encoder.encode('\u2606\u223c\u2606', True),
|
||||
'\xa1\xd9\xa1\xad\xa1\xd9')
|
||||
b'\xa1\xd9\xa1\xad\xa1\xd9')
|
||||
self.assertEqual(encoder.reset(), None)
|
||||
self.assertEqual(encoder.encode('', True), '')
|
||||
self.assertEqual(encoder.encode('', False), '')
|
||||
self.assertEqual(encoder.encode('', True), b'')
|
||||
self.assertEqual(encoder.encode('', False), b'')
|
||||
self.assertEqual(encoder.reset(), None)
|
||||
|
||||
def test_stateful(self):
|
||||
|
@ -75,29 +75,29 @@ class Test_IncrementalEncoder(unittest.TestCase):
|
|||
# U+0300 => ABDC
|
||||
|
||||
encoder = codecs.getincrementalencoder('jisx0213')()
|
||||
self.assertEqual(encoder.encode('\u00e6\u0300'), '\xab\xc4')
|
||||
self.assertEqual(encoder.encode('\u00e6'), '')
|
||||
self.assertEqual(encoder.encode('\u0300'), '\xab\xc4')
|
||||
self.assertEqual(encoder.encode('\u00e6', True), '\xa9\xdc')
|
||||
self.assertEqual(encoder.encode('\u00e6\u0300'), b'\xab\xc4')
|
||||
self.assertEqual(encoder.encode('\u00e6'), b'')
|
||||
self.assertEqual(encoder.encode('\u0300'), b'\xab\xc4')
|
||||
self.assertEqual(encoder.encode('\u00e6', True), b'\xa9\xdc')
|
||||
|
||||
self.assertEqual(encoder.reset(), None)
|
||||
self.assertEqual(encoder.encode('\u0300'), '\xab\xdc')
|
||||
self.assertEqual(encoder.encode('\u0300'), b'\xab\xdc')
|
||||
|
||||
self.assertEqual(encoder.encode('\u00e6'), '')
|
||||
self.assertEqual(encoder.encode('', True), '\xa9\xdc')
|
||||
self.assertEqual(encoder.encode('', True), '')
|
||||
self.assertEqual(encoder.encode('\u00e6'), b'')
|
||||
self.assertEqual(encoder.encode('', True), b'\xa9\xdc')
|
||||
self.assertEqual(encoder.encode('', True), b'')
|
||||
|
||||
def test_stateful_keep_buffer(self):
|
||||
encoder = codecs.getincrementalencoder('jisx0213')()
|
||||
self.assertEqual(encoder.encode('\u00e6'), '')
|
||||
self.assertEqual(encoder.encode('\u00e6'), b'')
|
||||
self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123')
|
||||
self.assertEqual(encoder.encode('\u0300\u00e6'), '\xab\xc4')
|
||||
self.assertEqual(encoder.encode('\u0300\u00e6'), b'\xab\xc4')
|
||||
self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123')
|
||||
self.assertEqual(encoder.reset(), None)
|
||||
self.assertEqual(encoder.encode('\u0300'), '\xab\xdc')
|
||||
self.assertEqual(encoder.encode('\u00e6'), '')
|
||||
self.assertEqual(encoder.encode('\u0300'), b'\xab\xdc')
|
||||
self.assertEqual(encoder.encode('\u00e6'), b'')
|
||||
self.assertRaises(UnicodeEncodeError, encoder.encode, '\u0123')
|
||||
self.assertEqual(encoder.encode('', True), '\xa9\xdc')
|
||||
self.assertEqual(encoder.encode('', True), b'\xa9\xdc')
|
||||
|
||||
|
||||
class Test_IncrementalDecoder(unittest.TestCase):
|
||||
|
@ -105,21 +105,21 @@ class Test_IncrementalDecoder(unittest.TestCase):
|
|||
def test_dbcs(self):
|
||||
# cp949 decoder is simple with only 1 or 2 bytes sequences.
|
||||
decoder = codecs.getincrementaldecoder('cp949')()
|
||||
self.assertEqual(decoder.decode('\xc6\xc4\xc0\xcc\xbd'),
|
||||
self.assertEqual(decoder.decode(b'\xc6\xc4\xc0\xcc\xbd'),
|
||||
'\ud30c\uc774')
|
||||
self.assertEqual(decoder.decode('\xe3 \xb8\xb6\xc0\xbb'),
|
||||
self.assertEqual(decoder.decode(b'\xe3 \xb8\xb6\xc0\xbb'),
|
||||
'\uc36c \ub9c8\uc744')
|
||||
self.assertEqual(decoder.decode(''), '')
|
||||
self.assertEqual(decoder.decode(b''), '')
|
||||
|
||||
def test_dbcs_keep_buffer(self):
|
||||
decoder = codecs.getincrementaldecoder('cp949')()
|
||||
self.assertEqual(decoder.decode('\xc6\xc4\xc0'), '\ud30c')
|
||||
self.assertEqual(decoder.decode(b'\xc6\xc4\xc0'), '\ud30c')
|
||||
self.assertRaises(UnicodeDecodeError, decoder.decode, '', True)
|
||||
self.assertEqual(decoder.decode('\xcc'), '\uc774')
|
||||
self.assertEqual(decoder.decode(b'\xcc'), '\uc774')
|
||||
|
||||
self.assertEqual(decoder.decode('\xc6\xc4\xc0'), '\ud30c')
|
||||
self.assertEqual(decoder.decode(b'\xc6\xc4\xc0'), '\ud30c')
|
||||
self.assertRaises(UnicodeDecodeError, decoder.decode, '\xcc\xbd', True)
|
||||
self.assertEqual(decoder.decode('\xcc'), '\uc774')
|
||||
self.assertEqual(decoder.decode(b'\xcc'), '\uc774')
|
||||
|
||||
def test_iso2022(self):
|
||||
decoder = codecs.getincrementaldecoder('iso2022-jp')()
|
||||
|
@ -140,61 +140,61 @@ class Test_IncrementalDecoder(unittest.TestCase):
|
|||
class Test_StreamWriter(unittest.TestCase):
|
||||
if len('\U00012345') == 2: # UCS2
|
||||
def test_gb18030(self):
|
||||
s= StringIO.StringIO()
|
||||
s= io.BytesIO()
|
||||
c = codecs.getwriter('gb18030')(s)
|
||||
c.write('123')
|
||||
self.assertEqual(s.getvalue(), '123')
|
||||
self.assertEqual(s.getvalue(), b'123')
|
||||
c.write('\U00012345')
|
||||
self.assertEqual(s.getvalue(), '123\x907\x959')
|
||||
self.assertEqual(s.getvalue(), b'123\x907\x959')
|
||||
c.write('\U00012345'[0])
|
||||
self.assertEqual(s.getvalue(), '123\x907\x959')
|
||||
self.assertEqual(s.getvalue(), b'123\x907\x959')
|
||||
c.write('\U00012345'[1] + '\U00012345' + '\uac00\u00ac')
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
|
||||
b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
|
||||
c.write('\U00012345'[0])
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
|
||||
b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
|
||||
self.assertRaises(UnicodeError, c.reset)
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
|
||||
b'123\x907\x959\x907\x959\x907\x959\x827\xcf5\x810\x851')
|
||||
|
||||
def test_utf_8(self):
|
||||
s= StringIO.StringIO()
|
||||
s= io.BytesIO()
|
||||
c = codecs.getwriter('utf-8')(s)
|
||||
c.write('123')
|
||||
self.assertEqual(s.getvalue(), '123')
|
||||
self.assertEqual(s.getvalue(), b'123')
|
||||
c.write('\U00012345')
|
||||
self.assertEqual(s.getvalue(), '123\xf0\x92\x8d\x85')
|
||||
self.assertEqual(s.getvalue(), b'123\xf0\x92\x8d\x85')
|
||||
|
||||
# Python utf-8 codec can't buffer surrogate pairs yet.
|
||||
if 0:
|
||||
c.write('\U00012345'[0])
|
||||
self.assertEqual(s.getvalue(), '123\xf0\x92\x8d\x85')
|
||||
self.assertEqual(s.getvalue(), b'123\xf0\x92\x8d\x85')
|
||||
c.write('\U00012345'[1] + '\U00012345' + '\uac00\u00ac')
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
'\xea\xb0\x80\xc2\xac')
|
||||
b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
b'\xea\xb0\x80\xc2\xac')
|
||||
c.write('\U00012345'[0])
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
'\xea\xb0\x80\xc2\xac')
|
||||
b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
b'\xea\xb0\x80\xc2\xac')
|
||||
c.reset()
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
'\xea\xb0\x80\xc2\xac\xed\xa0\x88')
|
||||
b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
b'\xea\xb0\x80\xc2\xac\xed\xa0\x88')
|
||||
c.write('\U00012345'[1])
|
||||
self.assertEqual(s.getvalue(),
|
||||
'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
'\xea\xb0\x80\xc2\xac\xed\xa0\x88\xed\xbd\x85')
|
||||
b'123\xf0\x92\x8d\x85\xf0\x92\x8d\x85\xf0\x92\x8d\x85'
|
||||
b'\xea\xb0\x80\xc2\xac\xed\xa0\x88\xed\xbd\x85')
|
||||
|
||||
else: # UCS4
|
||||
pass
|
||||
|
||||
def test_streamwriter_strwrite(self):
|
||||
s = StringIO.StringIO()
|
||||
s = io.BytesIO()
|
||||
wr = codecs.getwriter('gb18030')(s)
|
||||
wr.write('abcd')
|
||||
self.assertEqual(s.getvalue(), 'abcd')
|
||||
self.assertEqual(s.getvalue(), b'abcd')
|
||||
|
||||
class Test_ISO2022(unittest.TestCase):
|
||||
def test_g2(self):
|
||||
|
@ -203,10 +203,10 @@ class Test_ISO2022(unittest.TestCase):
|
|||
self.assertEqual(iso2022jp2.decode('iso2022-jp-2'), uni)
|
||||
|
||||
def test_iso2022_jp_g0(self):
|
||||
self.failIf('\x0e' in '\N{SOFT HYPHEN}'.encode('iso-2022-jp-2'))
|
||||
self.failIf(b'\x0e' in '\N{SOFT HYPHEN}'.encode('iso-2022-jp-2'))
|
||||
for encoding in ('iso-2022-jp-2004', 'iso-2022-jp-3'):
|
||||
e = '\u3406'.encode(encoding)
|
||||
self.failIf(filter(lambda x: x >= '\x80', e))
|
||||
self.failIf(any(x > 0x80 for x in e))
|
||||
|
||||
def test_bug1572832(self):
|
||||
if sys.maxunicode >= 0x10000:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue