python / python/cpython

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

オープン
#155,004 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

_PyUnicode_EncodeIconv() のエントリポイントから開始し、レポートにある ISO-2022 および状態を持つ EBCDIC の例を再現してください。置換バイトの出力とデコードを組み込みの ISO-2022-JP の動作と比較します。処理された置換が初期の shift 状態に戻り、ignore は影響を受けないことが完了条件です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
internationalization
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。