[issue9821] Support PEP 383 on Windows: mbcs support of surrogateescape error handler
report at bugs.python.org
Fri Sep 10 13:04:41 CEST 2010
New submission from STINNER Victor <victor.stinner at haypocalc.com>:
It would be nice to support PEP 383 (surrogateescape) on Windows, but the mbcs codec doesn't support it for performance reason. The Windows functions to encode/decode MBCS don't give the index of the unencodable/undecodable character/byte. For encoding, we can try to encode character by character (but be careful of surrogate pairs) and check that the character is a Python lone surrogate character or not (character in range U+DC80..U+DCFF). For decoding, it is more complex because MBCS can be a multibyte encoding, eg. cp65001 (Microsoft variant of utf-8, see #6058). So it's not possible to encode byte per byte and we should write an heuristic to guess the right number of bytes for each call to the decode function.
A completly different solution is to get the MBCS code page and use the Python code page codec (eg. "cp1252") instead of "mbcs" encoding, because Python cpXXXX codecs support all Python error handlers. Example (with Python 2.6):
>>> print(u"abcŁdef".encode("cp1252", "replace"))
>>> print(u"abcŁdef".encode("cp1252", "ignore"))
>>> print(u"abcŁdef".encode("cp1252", "backslashreplace"))
See also #8611 for the problem if the Python path cannot be encoded to mbcs (work in progress, see #9425).
components: Interpreter Core, Library (Lib), Unicode, Windows
nosy: haypo, loewis
title: Support PEP 383 on Windows: mbcs support of surrogateescape error handler
versions: Python 3.2
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list