python / python/cpython

Windows code page codec cannot encode with code pages that require dwFlags=0

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

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

3.13 3.14 3.15 3.16 OS-windows topic-unicode type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

On Windows, codecs.code_page_encode() fails with OSError: [WinError 87] The parameter is incorrect for every code page that WideCharToMultiByte() requires to be called with dwFlags = 0 and lpUsedDefaultChar = NULL: 50220, 50221, 50222, 50225, 50227, 50229, 52936 (HZ-GB2312), 54936 (GB18030), 57002-57011 (ISCII), 65000 and 42. Decoding these code pages works.

>>> import codecs
>>> codecs.code_page_decode(50220, b'\x1b$B$"\x1b(B')
('あ', 8)
>>> codecs.code_page_encode(50220, 'あ')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [WinError 87] The parameter is incorrect

encode_code_page_flags() returns WC_NO_BEST_FIT_CHARS, and encode_code_page_strict() passes a non-NULL lpUsedDefaultChar, for every code page except CP_UTF8 and CP_UTF7. decode_code_page_strict() retries with flags = 0 when MultiByteToWideChar() fails with ERROR_INVALID_FLAGS; the encoder has no such fallback. With dwFlags = 0 and lpUsedDefaultChar = NULL the conversion works:

>>> import ctypes
>>> buf = ctypes.create_string_buffer(64)
>>> n = ctypes.windll.kernel32.WideCharToMultiByte(50220, 0, 'あ', 1, buf, 64, None, None)
>>> buf.raw[:n]
b'\x1b$B$"\x1b(B'

Note that usedDefaultChar is how the encoder detects unencodable characters, so dropping it needs a replacement (for example decoding the result back and comparing).

cp51932, cp51936 and cp51950 fail in both directions; those code pages are not supported by MultiByteToWideChar()/WideCharToMultiByte() at all.

Linked PRs
  • gh-155018

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず codecs.code_page_encode() とヘルパーの encode_code_page_flags() および encode_code_page_strict() から始め、その後、それらの動作を decode_code_page_strict() と比較します。コードページ 50220 での失敗を再現し、一覧にある Windows コードページが正常にエンコードされる一方で、エンコードできない文字の検出も引き続きカバーされていることを確認します。リンクされた PR gh-155018 は、作業がすでに進行中であることを示しています。

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

評価

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

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

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