← 输入框|bugs.python.org|刷新

➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
标题: Fix codecs.iterencode/decode() by allowing data parameter to be omitted
类型: behavior Stage: resolved
Components: Documentation, Unicode Versions: Python 3.7, Python 3.6, Python 3.5
process
状态: closed Resolution: fixed
Dependencies: 后续:
分配给: martin.panter 抄送列表: doerwalter, ezio.melotti, lemburg, martin.panter, python-dev, r.david.murray, serhiy.storchaka, vstinner
优先级: normal 关键字: patch

Created on 2015-01-13 12:48 by martin.panter, last changed 2022-04-11 14:58 by admin. This issue is now closed.

文件
文件名 上传时间 Description 编辑
final-no-object.patch martin.panter, 2015-01-13 12:48 review
final-no-object.ignore-space.diff martin.panter, 2015-01-13 12:50 diff --ignore-all-space review
iter-unsupported.patch martin.panter, 2016-08-20 09:22 review
Messages (8)
msg233932 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2015-01-13 12:48
As mentioned in Issue 20132, iterencode() and iterdecode() only work on text-to-byte codecs, because they assume particular data types when finalizing the incremental codecs. This patch changes the signature of the IncrementalEncoder and IncrementalDecoder methods from

IncrementalEncoder.encode(object[, final])
IncrementalEncoder.decode(object[, final])

to

IncrementalEncoder.encode([object,] [final])
IncrementalEncoder.decode([object,] [final])

so that iteren/decode(), and perhaps in the future, StreamWriter/Reader, can operate the incremental codec without knowing what kind of data should be processed.
msg233933 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2015-01-13 12:50
Original patch has lots of whitespace changes, probably due to generated codec code not being regenerated for a long time. This diff ignores the space changes, so should be easier to review.
msg234206 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2015-01-18 00:19
Another idea that doesn’t involve changing the incremental codec APIs is kind of described in <https://bugs.python.org/issue7475#msg145986>: to add format parameters to iterencode() and iterdecode(), which would allow it to determine the right data type to finalize the codecs with.
msg256746 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2015-12-19 23:50
The patch changes public interface. This breaks compatibility with third-party codecs implementing it.

We have found other solution to iterencode/iterdecode problem. For example we can buffer iterated values and encode with one step delay:

    prev = sentinel = object()
    for input in iterator:
        if prev is not sentinel:
            output = encoder.encode(prev)
            if output:
                yield output
        prev = input
    if prev is not sentinel:
        output = encoder.encode(prev, True)
        if output:
            yield output

Or remember the previous value and use it to calculate the empty value at the end (works only if input type supports slicing):

    prev = sentinel = object()
    for input in iterator:
        output = encoder.encode(input)
        if output:
            yield output
        prev = input
    if prev is not sentinel:
        output = encoder.encode(prev[:0], True)
        if output:
            yield output
msg273101 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2016-08-19 09:06
Serhiy’s two proposals won’t work for codecs that include non-zero output for zero input:

>>> tuple(iterencode((), "utf-8-sig"))
(b'\xef\xbb\xbf',)
>>> encode(b"", "uu")
b'begin 666 <data>\n \nend\n'
>>> encode(b"", "zlib")
b'x\x9c\x03\x00\x00\x00\x00\x01'

However I agree that changing the incremental codec APIs is not ideal. Since nobody seems to care that much, it might be simpler to document that:

* iterencode() only works where text str objects can be encoded, so base64-codec is not supported, but rot13-codec is supported
* iterdecode() only works where bytes objects can be decoded, so rot13-codec is not supported, but base64-codec should be supported (pending other aspects of Issue 20132)
msg273198 - (view) Author: Martin Panter (martin.panter) * (Python committer) 日期: 2016-08-20 09:22
Here is my documentation proposal.
msg273203 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2016-08-20 10:25
> it might be simpler to document that

Agreed.
msg278678 - (view) Author: Roundup Robot (python-dev) (Python triager) 日期: 2016-10-15 01:05
New changeset 402eba63650c by Martin Panter in branch '3.5':
Issue #23231: Document codecs.iterencode(), iterdecode() shortcomings
https://hg.python.org/cpython/rev/402eba63650c

New changeset 0837940bcb9f by Martin Panter in branch '3.6':
Issue #23231: Merge codecs doc from 3.5 into 3.6
https://hg.python.org/cpython/rev/0837940bcb9f

New changeset 1955dcc27332 by Martin Panter in branch 'default':
Issue #23231: Merge codecs doc from 3.6
https://hg.python.org/cpython/rev/1955dcc27332
历史
日期 用户 动作 参数
2022-04-11 14:58:11admin修改github: 67420
2016-10-15 01:37:36martin.panter修改状态: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.7
2016-10-15 01:05:11python-dev修改抄送: + python-dev
消息: + msg278678
2016-08-20 10:25:41serhiy.storchaka修改assignee: serhiy.storchaka -> martin.panter

消息: + msg273203
抄送: + r.david.murray
2016-08-20 09:22:54martin.panter修改文件: + iter-unsupported.patch
versions: + Python 3.5
消息: + msg273198

components: + Documentation, - Library (Lib)
stage: patch review
2016-08-19 09:06:30martin.panter修改消息: + msg273101
2015-12-20 05:30:29r.david.murray修改抄送: - Ruel Net1400
2015-12-20 05:30:11r.david.murray修改消息: - msg256747
2015-12-20 01:03:26Ruel Net1400修改抄送: + Ruel Net1400
消息: + msg256747
2015-12-19 23:50:07serhiy.storchaka修改抄送: + lemburg, doerwalter

消息: + msg256746
versions: + Python 3.6, - Python 3.5
2015-07-23 01:54:38martin.panter链接issue20132 dependencies
2015-07-16 02:08:14martin.panter链接issue13881 dependencies
2015-02-28 13:37:26serhiy.storchaka修改assignee: serhiy.storchaka

抄送: + serhiy.storchaka
2015-01-18 00:19:04martin.panter修改消息: + msg234206
2015-01-13 12:50:06martin.panter修改文件: + final-no-object.ignore-space.diff

消息: + msg233933
2015-01-13 12:48:19martin.panter创建