python / python/cpython

iconv codecs write an error handler's replacement in a non-initial shift state

未關閉
#155,004 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.16 stdlib topic-unicode type-bug
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

An error handler's replacement bytes are copied to the output without returning it to the initial shift state first, so in a stateful encoding they are read back as encoded data.

>>> '中😀中'.encode('iso-2022-cn', 'replace')
b'\x1b$)A\x0eVP?\x1b$)A\x0eVP\x0f'
>>> _.decode('iso-2022-cn')
UnicodeDecodeError: 'iso-2022-cn' codec can't decode byte 0x3f in position 7: invalid multibyte sequence
>>> '中😀中'.encode('iso-2022-cn', 'backslashreplace').decode('iso-2022-cn')
'中苷鞍氨娑鞍中'

The built-in codec of the same family shifts back to ASCII first:

>>> '日😀日'.encode('iso2022_jp', 'replace')            # built-in
b'\x1b$BF|\x1b(B?\x1b$BF|\x1b(B'
>>> '日😀日'.encode('iconv:ISO-2022-JP', 'replace')     # iconv
b'\x1b$BF|?\x1b$BF|\x1b(B'

_PyUnicode_EncodeIconv() does reset the shift state after each handled error, but with a NULL output buffer, so the conversion forgets the state while the stream never gets the closing sequence.

This affects the ISO-2022 family and the stateful EBCDIC code pages (CP1364, CP1371, ...). ignore is unaffected, it injects nothing.

Linked PRs
  • gh-155005

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 _PyUnicode_EncodeIconv() 入口點開始,重現報告中的 ISO-2022 和具狀態 EBCDIC 範例。將替換位元組輸出和解碼與內建的 ISO-2022-JP 行為進行比較;完成標準是已處理的替換會返回初始 shift 狀態,而 ignore 維持不受影響。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
internationalization
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。