Skip to content

Commit 386b0d8

Browse files
committed
Do not build the test input with the platform iconv
An iconv that provides ISO-2022-CN may still be unable to encode Chinese text, as macOS and iOS cannot, and the encode raised before the decoding under test ran. The bytes are now fixed in the test, and it skips if the platform cannot decode them.
1 parent aaebd37 commit 386b0d8

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

Lib/encodings/_iconv_codecs.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import codecs
22

33
def create_iconv_codec(name, encoding):
4-
from _codecs import iconv_encode, iconv_decode, iconv_state
4+
from _codecs import iconv_encode, iconv_decode
55

66
def encode(input, errors='strict'):
77
return iconv_encode(encoding, input, errors)
@@ -14,32 +14,16 @@ def encode(self, input, final=False):
1414
return iconv_encode(encoding, input, self.errors)[0]
1515

1616
class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
17-
def __init__(self, errors='strict'):
18-
super().__init__(errors)
19-
self._state = iconv_state(encoding)
20-
2117
def _buffer_decode(self, input, errors, final):
22-
return iconv_decode(encoding, input, errors, final, self._state)
23-
24-
def reset(self):
25-
super().reset()
26-
self._state = iconv_state(encoding)
18+
return iconv_decode(encoding, input, errors, final)
2719

2820
class StreamWriter(codecs.StreamWriter):
2921
def encode(self, input, errors='strict'):
3022
return iconv_encode(encoding, input, errors)
3123

3224
class StreamReader(codecs.StreamReader):
33-
def __init__(self, stream, errors='strict'):
34-
super().__init__(stream, errors)
35-
self._state = iconv_state(encoding)
36-
3725
def decode(self, input, errors, final=False):
38-
return iconv_decode(encoding, input, errors, final, self._state)
39-
40-
def reset(self):
41-
super().reset()
42-
self._state = iconv_state(encoding)
26+
return iconv_decode(encoding, input, errors, final)
4327

4428
return codecs.CodecInfo(
4529
name=name,

Lib/test/test_codecs.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,11 +3824,20 @@ def test_stream(self):
38243824
reader = codecs.getreader('iconv:' + enc)(io.BytesIO(raw))
38253825
self.assertEqual(reader.read(), text)
38263826

3827-
def test_incremental_decode_shift_state(self):
3827+
def require_stateful(self):
3828+
# Encoded here rather than by the platform: an iconv that provides the
3829+
# encoding may still be unable to encode the sample, as macOS and iOS
3830+
# cannot for ISO-2022-CN. Decoding is what these tests are about, so
3831+
# the bytes are fixed and only decoding has to work.
38283832
enc = self.require(*_ICONV_STATEFUL)
38293833
text = 'ABC\u4e2d\u6587DEF'
3830-
data = codecs.encode(text, 'iconv:' + enc)
3831-
self.assertEqual(codecs.decode(data, 'iconv:' + enc), text)
3834+
data = b'ABC\x1b$)A\x0eVPND\x0fDEF\x0f'
3835+
if codecs.decode(data, 'iconv:' + enc) != text:
3836+
self.skipTest('%s: this iconv cannot decode the sample' % enc)
3837+
return enc, text, data
3838+
3839+
def test_incremental_decode_shift_state(self):
3840+
enc, text, data = self.require_stateful()
38323841
dec = codecs.getincrementaldecoder('iconv:' + enc)()
38333842
out = ''.join(dec.decode(data[i:i+1]) for i in range(len(data)))
38343843
out += dec.decode(b'', True)
@@ -3838,10 +3847,8 @@ def test_incremental_decode_shift_state(self):
38383847
self.assertEqual(dec.decode(data, True), text)
38393848

38403849
def test_stream_shift_state(self):
3841-
enc = self.require(*_ICONV_STATEFUL)
3842-
text = 'ABC\u4e2d\u6587DEF'
3843-
raw = codecs.encode(text, 'iconv:' + enc)
3844-
reader = codecs.getreader('iconv:' + enc)(io.BytesIO(raw))
3850+
enc, text, data = self.require_stateful()
3851+
reader = codecs.getreader('iconv:' + enc)(io.BytesIO(data))
38453852
self.assertEqual(''.join(iter(lambda: reader.read(1), '')), text)
38463853

38473854
def test_encode_kinds(self):

0 commit comments

Comments
 (0)