Skip to content

gh-160308: Buffer input in the punycode incremental codec until final - #160599

Closed
OsmnvAslan wants to merge 1 commit into
python:mainfrom
OsmnvAslan:fix-punycode-incremental
Closed

OsmnvAslan wants to merge 1 commit into
python:mainfrom
OsmnvAslan:fix-punycode-incremental

Conversation

@OsmnvAslan

Copy link
Copy Markdown
Contributor

The punycode IncrementalEncoder and IncrementalDecoder ran the whole-string transform on each chunk and ignored final, so chunked input produced wrong output (e.g. b'a-b-' instead of b'ab-'). Punycode encodes the positions of non-ASCII characters relative to the whole string, so a chunk cannot be transformed on its own.

They now derive from BufferedIncrementalEncoder / BufferedIncrementalDecoder and buffer the input until final is true, as suggested in the issue. codecs.iterencode()/iterdecode() now give correct results, and getstate()/setstate()/reset() work through the buffered base classes.

Behaviour note: a caller that uses the incremental classes directly with a whole label but never passes final=True now gets empty output, since nothing is emitted until the input is known to be complete. That is the documented contract of incremental codecs, but it is a visible change for such code.

punycode stays in broken_unicode_with_stateful in test_codecs, because the StreamReader/StreamWriter classes still use the stateless codec per chunk; this PR only fixes the incremental classes.

Closes gh-160308.

Prepared with AI assistance, then reviewed and tested before submission.

… final

Punycode encodes the positions of non-ASCII characters relative to the whole string, so chunks cannot be transformed independently. Use BufferedIncrementalEncoder/Decoder and only transform the input once final is true.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The punycode incremental encoder and decoder transform each chunk separately

1 participant