gh-154859: Keep the iconv shift state across incremental decode calls - #154862
gh-154859: Keep the iconv shift state across incremental decode calls#154862fedonman wants to merge 4 commits 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
left a comment
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)?
|
I wonder if implementing a simple type with the |
An iconv that provides ISO-2022-CN may still be unable to encode Chinese text, as macOS and iOS cannot, and the encode raised before the decoding under test ran. The bytes are now fixed in the test, and it skips if the platform cannot decode them.
The capsule was passed to iconv_decode() next to an encoding argument that it then ignored, so the two could disagree. IconvDecoder owns both, which also leaves iconv_decode() with its original signature.
|
@BHUVANSH855 You were right, and it failed on macOS too, not just iOS. The tests now use fixed bytes instead of encoding with the platform iconv, and they skip if the platform cannot decode them. @serhiy-storchaka Done, thanks. |
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.IconvDecoder(encoding)holds one conversion that the incremental decoder and thestream reader keep, so one conversion spans the stream.
reset()starts a new one. Theconversion is closed when the decoder 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.
iconv_decode()keeps its original signature. The first version of this passed an opaquecapsule to it alongside the encoding, and the two could disagree: it used the capsule's
encoding and ignored the argument. A type that owns both cannot get that wrong. Thanks
@serhiy-storchaka for the suggestion.
The tests use fixed bytes rather than encoding with the platform iconv, which provides
ISO-2022-CN on macOS and iOS but cannot encode the sample. They skip if the platform cannot
decode those bytes either, and they fail without the change. Note that
encodings._iconv_codecsis frozen, so a rebuild is needed to see that.Checked with 50k decoders and 20k incremental decoders for a leaked conversion: no RSS
growth.
test_import,test_embedandtest_interpreterspass, since_codecsnow hasmodule state. 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.