gh-154859: Keep the iconv shift state across incremental decode calls - #154862
Open
fedonman wants to merge 1 commit into
Open
gh-154859: Keep the iconv shift state across incremental decode calls#154862fedonman wants to merge 1 commit into
fedonman wants to merge 1 commit into
Conversation
… 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.
BHUVANSH855
reviewed
Jul 29, 2026
BHUVANSH855
left a comment
Contributor
There was a problem hiding this comment.
These regression tests currently construct the test input with:
data = codecs.encode(text, 'iconv:' + enc)From the iOS CI failures, it looks like iconv:ISO-2022-CN is available but cannot encode the sample text on that platform, so codecs.encode() raises UnicodeEncodeError before the incremental decoding path is exercised.
Would it make sense to either skip the test when the sample text cannot be encoded on the current platform, or make the test independent of platform-specific iconv encoding support (for example by using precomputed encoded bytes)?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
_codecs.iconv_state()now opens a conversion that the incremental decoder and the streamreader 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_codecsis frozen, so arebuild 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.