Skip to content

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

Closed

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    3.16new features, bugs and security fixes
    on Jul 29, 2026
  2. tekinertekin commented on Jul 29, 2026

    @tekinertekin
  3. added a commit that references this issue on Jul 31, 2026
  4. encukou commented on Aug 3, 2026

    @encukou
    Member

    @serhiy-storchaka, this broke some buildbots, see e.g. here

  5. reopened this on Aug 3, 2026
  6. added a commit that references this issue on Aug 4, 2026
  7. serhiy-storchaka commented on Aug 4, 2026

    @serhiy-storchaka
    Member

    The test only failed on macOS 15, not 13 or 26, and not other platform. Seems a platform bug. I cannot check details, so I just made the test more lenient.

  8. encukou commented on Aug 4, 2026

    @encukou
    Member

    For the record, it also failed on FreeBSD.

  9. serhiy-storchaka commented on Aug 4, 2026

    @serhiy-storchaka
    Member

    Oh, the ISO-2022-CN encoder crashes on two buildbots. Those workers are running libc with an unpatched security fix. freebsd-update on the 14.x worker should clear it, and the 15.0-CURRENT worker needs a rebuild.

  10. added 2 commits that reference this issue on Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions