Skip to content

Stateful CJK incremental encoders keep the shift state after a failed encode() #160522

Description

@gpshead

Bug report

Bug description:

When an incremental encode() call of a stateful CJK codec (iso2022_jp*, iso2022_kr, hz) fails, the output of that call is discarded but the shift state it reached is kept. The next encode() then leaves out the escape sequence, so double-byte characters are written while the stream is still in ASCII mode. IncrementalEncoder.encode() (and so io.TextIOWrapper.write()) and StreamWriter.write() are affected. The encoder already restores its pending buffer on this path, just not the codec state.

import codecs

for enc, text in [("iso2022_jp", "世"), ("iso2022_kr", "가"),
                  ("hz", "聊")]:
    good = codecs.getincrementalencoder(enc)().encode(text)
    encoder = codecs.getincrementalencoder(enc)()
    try:
        encoder.encode(text + "\U0001F600")
    except UnicodeEncodeError:
        pass
    print(enc, encoder.encode(text), "expected:", good)

Output on main; every value should equal its "expected":

iso2022_jp b'@$' expected: b'\x1b$B@$'
iso2022_kr b'0!' expected: b'\x1b$)C\x0e0!'
hz b'AD' expected: b'~{AD'

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. self-assigned this
    on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions