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
utf-7-imapregisters 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.iterdecodeandcodecs.StreamReader.readall fail on bytes the codec's own encoder produced.Expected:
True.Doc/library/codecs.rstsays the joined output of calls to the incrementaldecodemethod 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 tob'~peter/mail/&U,A-&Uxc-/&ZeU-&Zyw-&ip4-'where one shot givesb'~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