python / python/cpython

iconv codecs corrupt stateful encodings when decoding incrementally

未关闭
#154,859 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.16 stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug description:

The iconv-backed codecs start a fresh conversion on every call, so an incremental decode of a stateful encoding loses the shift state and silently returns wrong text. iso-2022-cn has no built-in codec, so the plain name reaches the iconv codec:

import codecs
blob = 'ABC中文DEF'.encode('iso-2022-cn')
print('one-shot:   ', blob.decode('iso-2022-cn'))
d = codecs.getincrementaldecoder('iso-2022-cn')()
print('incremental:', ''.join(d.decode(bytes([b])) for b in blob) + d.decode(b'', True))
one-shot:    ABC中文DEF
incremental: ABCVPNDDEF

codecs.iterdecode() and StreamReader.read(1) are wrong in the same way. A stateful encoding forced with the iconv: prefix is also affected, even when it has a built-in codec.

Either the codec objects need to keep one conversion across calls, or the iconv search function should refuse stateful encodings, which would make iso-2022-cn a LookupError.

The iconv codecs are new in 3.16 (gh-152997), so no released version is affected.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-154862

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

复现基于 iconv 的 codec 的增量解码示例,然后检查报告中描述的增量解码器、codecs.iterdecode() 和 StreamReader.read(1) 路径。完成标准是:有状态编码在多次调用之间保留转换状态,或者搜索函数始终拒绝不受支持的有状态编码,并且所示案例均有测试覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。