Skip to content

utf-7-imap incremental decoder and stream reader fail on split input #156965

Description

@fedonman

utf-7-imap registers an incremental decoder and a stream reader that call the one-shot decoder with no state, so a shift sequence split across chunks raises instead of being buffered. io.TextIOWrapper.read(n), codecs.iterdecode and codecs.StreamReader.read all fail on bytes the codec's own encoder produced.

import io

text = '台' * 5000
data = text.encode('utf-7-imap')
f = io.TextIOWrapper(io.BytesIO(data), encoding='utf-7-imap')
print(f.read(100) == text[:100])
UnicodeDecodeError: 'utf-7-imap' codec can't decode bytes in position 0-8191: unterminated shift sequence

Expected: True. Doc/library/codecs.rst says the joined output of calls to the incremental decode method is the same as decoding the joined input with the stateless decoder.

The incremental encoder breaks the same sentence more mildly: encoding '~peter/mail/台北/日本語' one character at a time joins to b'~peter/mail/&U,A-&Uxc-/&ZeU-&Zyw-&ip4-' where one shot gives b'~peter/mail/&U,BTFw-/&ZeVnLIqe-', though both decode back to the same text.

The codec is new in 3.16, so no released version is affected.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions