`codecs.encode` with `utf-*` encoding and errors returing `str` rejects surrogates blindly
未关闭
还没有人认领这个 Issue。
interpreter-core
stdlib
type-feature
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
For codecs.encode,
with utf-* encoding, and a custom errors which returns str,
if you pass some characters that are not valid UTF characters (e.g. surrogates),
UnicodeEncodeError is just raised and there's not the expected (and documented) case
where the returned str is appended.
import codecs
ERRORS_NAME = "returning non-ascii"
# something being not encod-able via `utf-*`
BAD_UTF = "\uD800" # the first high surrogate character
def register_repl_error(repl: str):
def error_handle(exc: UnicodeEncodeError) -> tuple[str, int]:
return (repl, exc.end)
codecs.register_error(ERRORS_NAME, error_handle)
def encode_surrogate(encoding: str, repl: str):
register_repl_error(repl)
max_enc_len = 9
pre = f"codecs.encode({BAD_UTF!r}, {encoding=:{max_enc_len}}) "
try:
res = codecs.encode(BAD_UTF, encoding, ERRORS_NAME)
except UnicodeEncodeError as err:
reason = err.reason
print(pre + f"raises with {reason=}")
else:
print(pre + f"returns {res}")
NON_ASCII = "龍" # loong in Chinese
## utf-*
for i in ('8', '16', '32', '16-le', '16-be'):
encode_surrogate("utf-" + i, NON_ASCII)
print('-'*3)
# The following is some non-utf* encoding, which works fine
## cjk
### zh
for enc in ("gbk", "big5"):
encode_surrogate(enc, NON_ASCII)
### jp
for enc in ("Shift_JIS", "EUC-JP"):
encode_surrogate(enc, NON_ASCII)
Output:
codecs.encode('\ud800', encoding=utf-8 ) raises with reason='surrogates not allowed'
codecs.encode('\ud800', encoding=utf-16 ) raises with reason='surrogates not allowed'
codecs.encode('\ud800', encoding=utf-32 ) raises with reason='surrogates not allowed'
codecs.encode('\ud800', encoding=utf-16-le) raises with reason='surrogates not allowed'
codecs.encode('\ud800', encoding=utf-16-be) raises with reason='surrogates not allowed'
---
codecs.encode('\ud800', encoding=gbk ) returns b'\xfd\x88'
codecs.encode('\ud800', encoding=big5 ) returns b'\xc0s'
codecs.encode('\ud800', encoding=Shift_JIS) returns b'\x97\xb4'
codecs.encode('\ud800', encoding=EUC-JP ) returns b'\xce\xb6'
CPython versions tested on:
3.9, 3.11, 3.12, 3.13, 3.14
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-139554
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行为 utf-* encodings 提供的 reproducer,并将其行为与 non-UTF encodings 进行比较。检查链接的 PR gh-139554 和 codecs.encode 的错误处理路径;完成的标准是,按照文档说明,返回 str 的自定义 handler 会针对无效 surrogate 被接受。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100