Skip to content

gh-154859: Keep the iconv shift state across incremental decode calls - #154862

Open
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-iconv-stateful-incremental
Open

gh-154859: Keep the iconv shift state across incremental decode calls#154862
fedonman wants to merge 1 commit into
python:mainfrom
fedonman:fix-iconv-stateful-incremental

Conversation

@fedonman

@fedonman fedonman commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

The iconv codecs opened a fresh conversion on every call, so decoding a stateful encoding in
chunks lost the shift state and silently returned wrong text:

one-shot:    ABC中文DEF
incremental: ABCVPNDDEF

_codecs.iconv_state() now opens a conversion that the incremental decoder and the stream
reader hold and pass to iconv_decode(), so one conversion spans the stream. reset()
starts a new one. The capsule closes the conversion when it is collected.

Only decoding is affected. Incremental encoding re-emits the escape sequences per chunk,
which is more verbose than the one-shot output but decodes back to the same text.

The tests fail without the change. Note that encodings._iconv_codecs is frozen, so a
rebuild is needed to see that. Checked with 50k decoders for a leaked conversion: no RSS
growth. This build has no --with-pydebug, so I could not run -R 3:3.

The iconv codecs are new in 3.16, so there is no NEWS entry.

… calls

The iconv codecs opened a fresh conversion for every call, so decoding a
stateful encoding in chunks lost the shift state and silently produced wrong
text: incremental decoding of ISO-2022-CN dropped the escape sequences and
returned the raw bytes as ASCII.

_codecs.iconv_state() now opens a conversion that the incremental decoder and
the stream reader keep and pass back to iconv_decode(), so one conversion
spans the whole stream. reset() starts a new one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant