Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3679,6 +3679,10 @@ def iconv_encoding_available(name):
# Encodings iconv may provide but for which CPython has no built-in codec
# (cp1047 is EBCDIC, i.e. not ASCII-compatible).
_ICONV_ONLY = ['cp1047', 'cp1133', 'GEORGIAN-PS', 'ARMSCII-8']
# Encodings that leave a shift state pending, so encoding ends with a flush.
_ICONV_SHIFT_STATE = [('ISO-2022-CN-EXT', 'ABC\u4e2dDEF'),
('ISO-2022-CN', 'ABC\u4e2dDEF'),
('ISO-2022-JP', 'ABC\u65e5DEF')]


@unittest.skipUnless(hasattr(codecs, 'iconv_encode'),
Expand Down Expand Up @@ -3812,6 +3816,33 @@ def test_encode_kinds(self):
with self.subTest(text=text):
self.assertEqual(text.encode('iconv:' + enc), text.encode(enc))

def test_encode_shift_state_flush(self):
# Encoding ends with a flush that emits the pending shift sequence. Its
# return value counts nonreversible conversions, and some iconv
# implementations make it positive for the flush itself (glibc does for
# ISO-2022-CN-EXT). That must not be read as a substituted character:
# doing so discarded the whole output, ASCII included.
#
# Only the ASCII around the character is checked, not a full round-trip.
# An iconv that cannot represent the character either rejects it or
# substitutes for it silently, as macOS does for ISO-2022-CN.
tested = False
for enc, text in _ICONV_SHIFT_STATE:
if not iconv_encoding_available(enc):
continue
with self.subTest(encoding=enc):
try:
data = codecs.iconv_encode(enc, text)[0]
except UnicodeEncodeError:
continue
tested = True
self.assertNotEqual(data, b'')
decoded = codecs.iconv_decode(enc, data, 'strict', True)[0]
self.assertStartsWith(decoded, 'ABC')
self.assertEndsWith(decoded, 'DEF')
if not tested:
self.skipTest('no shift-state iconv encoding is available')

def test_encode_surrogateescape(self):
# A lone surrogate lives in the 2-byte kind and round-trips.
enc = self.require('ASCII')
Expand Down
6 changes: 3 additions & 3 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8462,6 +8462,9 @@ _PyUnicode_EncodeIconv(const char *encoding, PyObject *unicode,
}

if (ret != (size_t)-1) {
if (flushing) {
break;
}
/* A positive result counts nonreversible conversions: iconv()
substituted an unencodable character instead of failing with
EILSEQ (musl and *BSD citrus do this). Treat it as unencodable
Expand All @@ -8479,9 +8482,6 @@ _PyUnicode_EncodeIconv(const char *encoding, PyObject *unicode,
out = out_before;
up -= unit;
}
else if (flushing) {
break;
}
else if (careful && up < uend) {
continue;
}
Expand Down
Loading