Skip to content

iso-2022-cn-ext encoding silently returns b'' for non-ASCII text #154863

Description

@fedonman

Bug description:

Encoding non-ASCII text to ISO-2022-CN-EXT returns b'' with no error. The ASCII part is
lost with it, and the codec reports the whole input as consumed:

>>> 'ABC中DEF'.encode('iso-2022-cn-ext')
b''
>>> codecs.iconv_encode('ISO-2022-CN-EXT', '中')   # (output, consumed)
(b'', 1)
>>> codecs.iconv_encode('ISO-2022-CN', '中')       # without EXT
(b'\x1b$)A\x0eVP\x0f', 1)

No error handler changes it: strict, replace, ignore and xmlcharrefreplace all give
b''. Only strings stored as 2-byte characters are affected; a pure-ASCII string encodes
normally, and an unencodable character in a 1-byte or 4-byte string raises
UnicodeEncodeError as expected.

This is not an iconv limitation. iconv -f UCS-2LE -t ISO-2022-CN-EXT, which is the
conversion the codec asks for on a 2-byte string, produces the expected
\x1b$)A\x0eVP\x0f.

The iconv codecs are new in 3.16 (gh-152997), so no released version is affected.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions