Repository navigation
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
| BPO | 20132 |
|---|---|
| Nosy | @malemburg, @loewis, @doerwalter, @ncoghlan, @vstinner, @vadmium, @serhiy-storchaka |
| Dependencies | |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = None
created_at = <Date 2014-01-05.13:48:58.421>
labels = ['type-bug', 'library']
title = 'Many incremental codecs don\xe2\x80\x99t handle fragmented data'
updated_at = <Date 2016-08-19.12:08:31.462>
user = 'https://github.com/vadmium'bugs.python.org fields:
activity = <Date 2016-08-19.12:08:31.462>
actor = 'martin.panter'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2014-01-05.13:48:58.421>
creator = 'martin.panter'
dependencies = ['13881', '16473', '20121', '23231', '27799']
files = ['37718', '37846']
hgrepos = []
issue_num = 20132
keywords = ['patch']
message_count = 12.0
messages = ['207374', '207850', '207900', '226679', '232850', '234099', '234100', '234102', '234104', '234122', '234651', '273108']
nosy_count = 7.0
nosy_names = ['lemburg', 'loewis', 'doerwalter', 'ncoghlan', 'vstinner', 'martin.panter', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'patch review'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue20132'
versions = ['Python 3.4', 'Python 3.5']Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
- StatusShow more project fieldsNo status
- StatusShow more project fieldsNo status
Activity
vadmium commentedon Jan 5, 2014
Many of the incremental codecs do not handle fragmented data very well. In the past I think I was interested in using the Base-64 and Quoted-printable codecs, and playing with other codecs today reveals many more issues. A lot of the issues reflect missing functionality, so maybe the simplest solution would be to document the codecs that don’t work.
Incremental decoding issues:
Incremental encoding issues:
I also noticed iterdecode() does not work with “uu-codec”:
And iterencode() does not work with any of the byte encoders, because it does not know what kind of empty string to pass to IncrementalEncoder.encode(final=True):
Finally, incremental UTF-7 encoding is suboptimal, and the decoder seems to buffer unlimited data, both defeating the purpose of using an incremental codec:
doerwalter commentedon Jan 10, 2014
The best solution IMHO would be to implement real incremental codecs for all of those.
Maybe iterencode() with an empty iterator should never call encode()? (But IMHO it would be better to document that iterencode()/iterdecode() should only be used with "real" codecs.)
Note that the comment before PyUnicode_DecodeUTF7Stateful() in unicodeobject.c reads:
/* The decoder. The only state we preserve is our read position,
Changing that would have to introduce a state object that the codec updates and from which it can be restarted.
Also the encoder does not buffer anything. To implement the suggested behaviour, the encoder might have to buffer unlimited data.
vadmium commentedon Jan 11, 2014
I think calling iterencode() with an empty iterator is a side issue. Even with a non-empty iterator, it tries to encode an empty _text_ string to finalise the encoder:
Similarly, iterdecode(encoding="rot-13") doesn’t work. I agree it would be good to document that iterencode() is limited to text encoders and iterdecode() is limited to byte decoders.
vadmium commentedon Sep 10, 2014
Stream reader interfaces suffer the same problem. Test cases:
codecs.getreader("unicode-escape")(BytesIO(br"\u2013")).read(1)
codecs.getreader("hex-codec")(BytesIO(b"33")).read(1)
codecs.getreader("base64-codec")(BytesIO(b"AA==")).read(1)
TestCase().assertEqual(b"=", codecs.getreader("quopri-codec")(BytesIO(b"=3D")).read(1))
Even though the “zlib” incremental decoder is okay, its stream reader still suffers this problem. I was going to check for zip bomb behaviour when you call read() with a limited number of input “characters”, but got this instead:
Similar problems with stream writers, for instance:
I also noticed StreamWriter.writelines() assumes it is encoding text, and does not work at least with “base64-codec”:
>>>> codecs.getwriter("base64-codec")(BytesIO()).writelines((b"1", b"2")) TypeError: sequence item 0: expected str instance, bytes foundvadmium commentedon Dec 18, 2014
The “unicode-escape” and “utf-7” cases affect the more widely-used TextIOWrapper interface:
For UTF-7 decoding to work optimally I think the amount of data buffering necessary would be limited to only a few bytes.
vadmium commentedon Jan 15, 2015
I opened bpo-23231 about fixing iterencode() and iterdecode() in the general case. I added a patch to bpo-13881 to fix StreamWriter for zlib and bz2, and to fix StreamWriter.writelines() in general.
I am adding a patch here to clarify the StreamReader API and fix the StreamReader for the zlib-codec.
Plan of other things to do:
bz2 StreamReader: Should be implemented similar to the zlib patch above, after bpo-15955 is resolved and we have a max_length parameter to use. Or could be based on Bz2File now.
hex decoder: Shouldn’t be too hard to hack a stateful IncrementalDecoder that saves the leftover digit if given an odd number of digits. Create a generic codecs._IncrementalStreamReader class that uses an IncrementalDecoder and buffers unread decoded data, similar to my _IncrementalStreamWriter for bpo-13881.
base64 encoder: IncrementalEncoder could encode in base64.MAXBINSIZE chunks
base64 decoder: IncrementalDecoder could strip non-alphabet characters using regular expressions, decode in multiples of four characters
quopri encoder: would require new implementation or major refactoring of quopri module
quopri decoder: check for incomplete trailing escape codes (=, =<single hex digit>, =\r) and newlines (\r)
uu encoder: write header and trailer via uu module; encode using b2a_uu()
uu decoder: factor out header parsing from uu module; buffer and decode line by line based on encoded length
unicode-escape, raw-unicode-escape: Stateful decoding would probably require a new -Stateful() function at the C level, though it might be easy to build from the existing implementation. I suggest documenting that stateful decoding is not supported for the time being.
utf-7: As Walter said, proper stateful codec is not supported by the C API, despite PyUnicode_DecodeUTF7Stateful(); doing so would probably require significant changes. I suggest documenting a warning for stateful mode (including with TextIOWrapper) about suboptimal encoding and unlimited data buffering for the time being.
punycode, unicode_internal: According to test_codecs.py, these also don’t work in stateful mode. Not sure on the details though.
malemburg commentedon Jan 15, 2015
On 15.01.2015 23:46, Martin Panter wrote:
Martin, I must be missing some context: what's your master plan with
all these codec patches and open tickets ?
They are very hard to follow and you're making design changes which
need more review and discussion than can easily be done on a few tickets.
Regarding the patch:
The doc patch seems to just change ordering of sentences and paragraphs.
Without additional explanation, it's difficult to determine whether
you are changing semantics or not.
The "strict" error change is not correct. Most codecs raise a UnicodeError
(which is a ValueError subclass) and code expects codecs that work
on Unicode to return a UnicodeError, not a ValueError. Only codecs that
do not work on Unicode are allowed to raise ValueErrors.
malemburg commentedon Jan 15, 2015
This addition is wrong as well:
Where did you get this idea from ?
vadmium commentedon Jan 16, 2015
My “master plan” is basically to make most of the bytes-to-bytes codecs work as documented in the incremental (stateful) modes. I’m less interested in fixing the text codecs, and the quopri and uu codecs might be too hard, so I was going to propose some documentation warnings for those.
If you have a suggestion on how to go about this better, let me know.
With my doc change to StreamReader, I wanted to document the different modes that I saw in the base codecs.StreamReader.read() implementation:
Previously the case of read(-1, chars) was ambiguous. Also I did not find the description “an approximate maximum number of decoded bytes” very helpful, considering more than this maximum was read if necessary to get enough *chars*.
Regarding the end-of-stream behaviour, I made an assumption but I now realize it was wrong. Experimenting with the UTF-8 codec shows that its StreamReader.read() keeps returning "" when the underlying stream returns no data. But if it was in the middle of a multi-byte sequence, no “end of data” error is raised, and the multi-byte sequence can be completed if the underlying stream later returns more data. I think the lack of end-of-data checking should be documented.
The different cases of ValueError and UnicodeError that you describe make sense. I think the various references to ValueError and UnicodeError should be updated (or replaced with pointers) to match.
vadmium commentedon Jan 16, 2015
There is a flaw with inheriting the readline() method in my patch, and I have decided to give up fixing the StreamReader classes. I did update the documentation in my copy of the patch based on Marc-Andre Lemburg’s feedback if anyone is interested in it, otherwise I am going to focus on some of the incremental codecs now.
vadmium commentedon Jan 25, 2015
Here is a new patch which fixes the bytes-to-bytes incremental codecs. It depends on my patches for these other issues being applied first:
In addition, without the fix for bpo-20121 (Quoted printable soft line breaking), the patch will apply, but there will be test failures.
Summary of the changes in this patch:
This patch is actually generated from a series of patches folded together. If anyone is interested, I could try pushing the original series to a Bitbucket repository or something.
vadmium commentedon Aug 19, 2016
Split off bpo-27799 for the base-64 incremental decoder
pythongh-107862: Reference the existing issues for two of the codec e…
pythongh-107862: Reference the existing issues for two of the codec e…