python / python/cpython

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

Aperta
#155,016 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.13 3.14 3.15 3.16 OS-windows topic-unicode type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con codecs.code_page_encode() e gli helper encode_code_page_flags() e encode_code_page_strict(), quindi confronta il loro comportamento con decode_code_page_strict(). Riproduci il fallimento per la code page 50220 e verifica che le code page Windows elencate vengano codificate correttamente, mentre il rilevamento dei caratteri non codificabili rimanga coperto. Il PR collegato gh-155018 indica che il lavoro è già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
operating-systems
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.