GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing peps.python.org — PEP 538 – Coercing the legacy C locale to a UTF-8 based locale | peps.python.org
PEP 538 – Coercing the legacy C locale to a UTF-8 based locale
PEP 538 – Coercing the legacy C locale to a UTF-8 based locale
Table of Contents
AbstractAn ongoing challenge with Python 3 on *nix systems is the conflict between needing to use the configured locale encoding by default for consistency with other locale-aware components in the same process or subprocesses, and the fact that the standard C locale (as defined in POSIX:2001) typically implies a default text encoding of ASCII, which is entirely inadequate for the development of networked services and client applications in a multilingual world. PEP 540 proposes a change to CPython’s handling of the legacy C locale such that CPython will assume the use of UTF-8 in such environments, rather than persisting with the demonstrably problematic assumption of ASCII as an appropriate encoding for communicating with operating system interfaces. This is a good approach for cases where network encoding interoperability is a more important concern than local encoding interoperability. However, it comes at the cost of making CPython’s encoding assumptions diverge from those of other locale-aware components in the same process, as well as those of components running in subprocesses that share the same environment. This can cause interoperability problems with some extension modules (such as GNU readline’s command line history editing), as well as with components running in subprocesses (such as older Python runtimes). It also requires non-trivial changes to the internals of how CPython itself works, rather than relying primarily on existing configuration settings that are supported by Python versions prior to Python 3.7. Accordingly, this PEP proposes that independently of the UTF-8 mode proposed in PEP 540, the way the CPython implementation handles the default C locale be changed to be roughly equivalent to the following existing configuration settings (supported since Python 3.1): LC_CTYPE=C.UTF-8 PYTHONIOENCODING=utf-8:surrogateescape The exact target locale for coercion will be chosen from a predefined list at runtime based on the actually available locales. The reinterpreted locale settings will be written back to the environment so
they’re visible to other components in the same process and in subprocesses,
but the changed The new legacy locale coercion behavior can be disabled either by setting
With this change, any *nix platform that does not offer at least one of the
As it only reflects a change in default settings rather than a fundamentally new capability, redistributors (such as Linux distributions) with a narrower target audience than the upstream CPython development team may also choose to opt in to this locale coercion behaviour for the Python 3.6.x series by applying the necessary changes as a downstream patch. Implementation NotesAttempting to implement the PEP as originally accepted showed that the proposal to emit locale coercion and compatibility warnings by default simply wasn’t practical (there were too many cases where previously working code failed because of the warnings, rather than because of latent locale handling defects in the affected code). As a result, the The output examples in the PEP itself have also been updated to remove the warnings and make them easier to read. BackgroundWhile the CPython interpreter is starting up, it may need to convert from
the On Windows, the limitations of the On Mac OS X, iOS, and Android, many components, including CPython, already assume the use of UTF-8 as the system encoding, regardless of the locale setting. However, this isn’t the case for all components, and the discrepancy can cause problems in some situations (for example, when using the GNU readline module [16]). On non-Apple and non-Android *nix systems, these operations are handled using the C locale system in glibc, which has the following characteristics [4]:
The specific locale category that covers the APIs that CPython depends on is
(This summary of the locale handling omits several technical details related to exactly where and when the text encoding declared as part of the locale settings is used - see PEP 540 for further discussion, as these particular details matter more when decoupling CPython from the declared C locale than they do when overriding the locale with one based on UTF-8) These calls are usually sufficient to provide sensible behaviour, but they can still fail in the following cases:
The simplest way to deal with this problem for currently released versions of CPython is to explicitly set a more sensible locale when launching the application. For example: LC_CTYPE=C.UTF-8 python3 ... The Mac OS X and other *BSD systems have taken a different approach: instead of
offering a In the specific case of Docker containers and similar technologies, the appropriate locale setting can be specified directly in the container image definition. Another common failure case is developers specifying Relationship with other PEPsThis PEP shares a common problem statement with PEP 540 (improving Python 3’s behaviour in the default C locale), but diverges markedly in the proposed solution:
After reviewing both PEPs, it became clear that they didn’t actually conflict at a technical level, and the proposal in PEP 540 offered a superior option in cases where no suitable locale was available, as well as offering a better reference behaviour for platforms where the notion of a “locale encoding” doesn’t make sense (for example, embedded systems running MicroPython rather than the CPython reference interpreter). Meanwhile, this PEP offered improved compatibility with other locale-aware components, and an approach more amenable to being backported to Python 3.6 by downstream redistributors. As a result, this PEP was amended to refer to PEP 540 as a complementary
solution that offered improved behaviour when none of the standard UTF-8 based
locales were available, as well as extending the changes in the default
settings to APIs that aren’t currently independently configurable (such as
the default encoding and error handler for The availability of PEP 540 also meant that the MotivationWhile Linux container technologies like Docker, Kubernetes, and OpenShift are best known for their use in web service development, the related container formats and execution models are also being adopted for Linux command line application development. Technologies like Gnome Flatpak [7] and Ubuntu Snappy [8] further aim to bring these same techniques to Linux GUI application development. When using Python 3 for application development in these contexts, it isn’t uncommon to see text encoding related errors akin to the following: $ docker run --rm fedora:25 python3 -c 'print("ℙƴ☂ℌøἤ")'
Unable to decode the command from the command line:
UnicodeEncodeError: 'utf-8' codec can't encode character '\udce2' in position 7: surrogates not allowed
$ docker run --rm ncoghlan/debian-python python3 -c 'print("ℙƴ☂ℌøἤ")'
Unable to decode the command from the command line:
UnicodeEncodeError: 'utf-8' codec can't encode character '\udce2' in position 7: surrogates not allowed
Even though the same command is likely to work fine when run locally: $ python3 -c 'print("ℙƴ☂ℌøἤ")'
ℙƴ☂ℌøἤ
The source of the problem can be seen by instead running the $ locale | grep -E 'LC_ALL|LC_CTYPE|LANG' LANG=en_AU.UTF-8 LC_CTYPE="en_AU.UTF-8" LC_ALL= $ docker run --rm fedora:25 locale | grep -E 'LC_ALL|LC_CTYPE|LANG' LANG= LC_CTYPE="POSIX" LC_ALL= $ docker run --rm ncoghlan/debian-python locale | grep -E 'LC_ALL|LC_CTYPE|LANG' LANG= LANGUAGE= LC_CTYPE="POSIX" LC_ALL= In this particular example, we can see that the host system locale is set to “en_AU.UTF-8”, so CPython uses UTF-8 as the default text encoding. By contrast, the base Docker images for Fedora and Debian don’t have any specific locale set, so they use the POSIX locale by default, which is an alias for the ASCII-based default C locale. The simplest way to get Python 3 (regardless of the exact version) to behave
sensibly in Fedora and Debian based containers is to run it in the $ docker run --rm -e LC_CTYPE=C.UTF-8 fedora:25 python3 -c 'print("ℙƴ☂ℌøἤ")'
ℙƴ☂ℌøἤ
$ docker run --rm -e LC_CTYPE=C.UTF-8 ncoghlan/debian-python python3 -c 'print("ℙƴ☂ℌøἤ")'
ℙƴ☂ℌøἤ
$ docker run --rm -e LC_CTYPE=C.UTF-8 fedora:25 locale | grep -E 'LC_ALL|LC_CTYPE|LANG'
LANG=
LC_CTYPE=C.UTF-8
LC_ALL=
$ docker run --rm -e LC_CTYPE=C.UTF-8 ncoghlan/debian-python locale | grep -E 'LC_ALL|LC_CTYPE|LANG'
LANG=
LANGUAGE=
LC_CTYPE=C.UTF-8
LC_ALL=
The Alpine Linux based Python images provided by Docker, Inc. already use the C.UTF-8 locale by default: $ docker run --rm python:3 python3 -c 'print("ℙƴ☂ℌøἤ")'
ℙƴ☂ℌøἤ
$ docker run --rm python:3 locale | grep -E 'LC_ALL|LC_CTYPE|LANG'
LANG=C.UTF-8
LANGUAGE=
LC_CTYPE="C.UTF-8"
LC_ALL=
Similarly, for custom container images (i.e. those adding additional content on top of a base distro image), a more suitable locale can be set in the image definition so everything just works by default. However, it would provide a much nicer and more consistent user experience if CPython were able to just deal with this problem automatically rather than relying on redistributors or end users to handle it through system configuration changes. While the glibc developers are working towards making the C.UTF-8 locale universally available for use by glibc based applications like CPython [6], this unfortunately doesn’t help on platforms that ship older versions of glibc without that feature, and also don’t provide C.UTF-8 (or an equivalent) as an on-disk locale the way Debian and Fedora do. These platforms are considered out of scope for this PEP - see PEP 540 for further discussion of possible options for improving CPython’s default behaviour in such environments. Design PrinciplesThe above motivation leads to the following core design principles for the proposed solution:
Minimizing the negative impact on systems currently correctly configured to use GB-18030 or another partially ASCII compatible universal encoding leads to the following design principle:
Minimizing the negative impact on systems and programs correctly configured to
use an explicit locale category like
Finally, maintaining compatibility with running arbitrary subprocesses in orchestration use cases leads to the following design principle:
SpecificationTo better handle the cases where CPython would otherwise end up attempting
to operate in the It further proposes to emit a warning on stderr if the legacy In addition to these general changes, some additional Android-specific changes
are proposed to handle the differences in the behaviour of Legacy C locale coercion in the standalone Python interpreter binaryWhen run as a standalone application, CPython has the opportunity to reconfigure the C locale before any locale dependent operations are executed in the process. This means that it can change the locale settings not only for the CPython runtime, but also for any other locale-aware components running in the current process (e.g. as part of extension modules), as well as in subprocesses that inherit their environment from the current process. After calling const char *ctype_loc = setlocale(LC_CTYPE, NULL); This cryptic invocation is the API that C provides to query the current locale
setting without changing it. Given that query, it is possible to check for
exactly the ctype_loc != NULL && strcmp(ctype_loc, "C") == 0 # true only in the C locale This call also returns Given this information, CPython can then attempt to coerce the locale to one that uses UTF-8 rather than ASCII as the default encoding. Three such locales will be tried:
The coercion will be implemented by setting the To allow for better cross-platform binary portability and to adjust automatically to future changes in locale availability, these checks will be implemented at runtime on all platforms other than Windows, rather than attempting to determine which locales to try at compile time. When this locale coercion is activated, the following warning will be printed on stderr, with the warning containing whichever locale was successfully configured: Python detected LC_CTYPE=C: LC_CTYPE coerced to C.UTF-8 (set another locale or PYTHONCOERCECLOCALE=0 to disable this locale coercion behaviour). (Note: this warning ended up being silenced by default. See the Implementation Note above for more details) As long as the current platform provides at least one of the candidate UTF-8
based environments, this locale coercion will mean that the standard
Python binary and locale-aware extensions should once again “just work”
in the three main failure cases we’re aware of (missing locale
settings, SSH forwarding of unknown locales via The one case where failures may still occur is when If none of the candidate locales are successfully configured, or the If The interpreter will always check for the Legacy C locale warning during runtime initializationBy the time that Accordingly, when Python runtime initialized with LC_CTYPE=C (a locale with default ASCII encoding), which may cause Unicode compatibility problems. Using C.UTF-8, C.utf8, or UTF-8 (if available) as alternative Unicode-compatible locales is recommended. (Note: this warning ended up being silenced by default. See the Implementation Note above for more details) In this case, no actual change will be made to the locale settings. Instead, the warning informs both system and application integrators that they’re running Python 3 in a configuration that we don’t expect to work properly. The second sentence providing recommendations may eventually be conditionally
compiled based on the operating system (e.g. recommending New build-time configuration optionsWhile both of the above behaviours would be enabled by default, they would also have new associated configuration options and preprocessor definitions for the benefit of redistributors that want to override those default settings. The locale coercion behaviour would be controlled by the flag
The locale warning behaviour would be controlled by the flag
(Note: this compile time warning option ended up being replaced by a runtime
On platforms which don’t use the Changes to the default error handling on the standard streamsSince Python 3.5, CPython has defaulted to using In order to preserve this behaviour without introducing any behavioural
discrepancies between locale coercion and explicitly configuring a locale, the
coercion target locales ( No changes are proposed to the default error handler for Changes to locale settings on AndroidIndependently of the other changes in this PEP, CPython on Android systems
will be updated to call This Android-specific behaviour is being introduced due to the following Android-specific details:
Platform Support ChangesA new “Legacy C Locale” section will be added to PEP 11 that states:
RationaleImproving the handling of the C localeIt has been clear for some time that the C locale’s default encoding of
The challenge for CPython has been the fact that in addition to being used for network service development, it is also extensively used as an embedded scripting language in larger applications, and as a desktop application development language, where it is more important to be consistent with other locale-aware components sharing the same process, as well as with the user’s desktop locale settings, than it is with the emergent conventions of modern network service development. The core premise of this PEP is that for all of these use cases, the assumption of ASCII implied by the default “C” locale is the wrong choice, and furthermore that the following assumptions are valid:
Defaulting to “surrogateescape” error handling on the standard IO streamsBy coercing the locale away from the legacy C default and its assumption of
ASCII as the preferred text encoding, this PEP also disables the implicit use
of the “surrogateescape” error handler on the standard IO streams that was
introduced in Python 3.5 ([15]), as well as the automatic use of
Rather than introducing yet another configuration option to adjust that
behaviour, this PEP instead proposes to extend the “surrogateescape” default
for The aim of this behaviour is to attempt to ensure that operating system provided text values are typically able to be transparently passed through a Python 3 application even if it is incorrect in assuming that that text has been encoded as UTF-8. In particular, GB 18030 [12] is a Chinese national text encoding standard that handles all Unicode code points, that is formally incompatible with both ASCII and UTF-8, but will nevertheless often tolerate processing as surrogate escaped data - the points where GB 18030 reuses ASCII byte values in an incompatible way are likely to be invalid in UTF-8, and will therefore be escaped and opaque to string processing operations that split on or search for the relevant ASCII code points. Operations that don’t involve splitting on or searching for particular ASCII or Unicode code point values are almost certain to work correctly. Similarly, Shift-JIS [13] and ISO-2022-JP [14] remain in widespread use in Japan, and are incompatible with both ASCII and UTF-8, but will tolerate text processing operations that don’t involve splitting on or searching for particular ASCII or Unicode code point values. As an example, consider two files, one encoded with UTF-8 (the default encoding
for $ python3 -c 'open("utf8.txt", "wb").write("ℙƴ☂ℌøἤ\n".encode("utf-8"))'
$ python3 -c 'open("gb18030.txt", "wb").write("ℙƴ☂ℌøἤ\n".encode("gb18030"))'
On disk, we can see that these are two very different files: $ python3 -c 'print("UTF-8: ", open("utf8.txt", "rb").read().strip()); \
print("GB18030:", open("gb18030.txt", "rb").read().strip())'
UTF-8: b'\xe2\x84\x99\xc6\xb4\xe2\x98\x82\xe2\x84\x8c\xc3\xb8\xe1\xbc\xa4\n'
GB18030: b'\x816\xbd6\x810\x9d0\x817\xa29\x816\xbc4\x810\x8b3\x816\x8d6\n'
That nevertheless can both be rendered correctly to the terminal as long as they’re decoded prior to printing: $ python3 -c 'print("UTF-8: ", open("utf8.txt", "r", encoding="utf-8").read().strip()); \
print("GB18030:", open("gb18030.txt", "r", encoding="gb18030").read().strip())'
UTF-8: ℙƴ☂ℌøἤ
GB18030: ℙƴ☂ℌøἤ
By contrast, if we just pass along the raw bytes, as $ LANG=en_AU.UTF-8 cat utf8.txt gb18030.txt ℙƴ☂ℌøἤ �6�6�0�0�7�9�6�4�0�3�6�6 Even setting a specifically Chinese locale won’t help in getting the GB-18030 encoded file rendered correctly: $ LANG=zh_CN.gb18030 cat utf8.txt gb18030.txt ℙƴ☂ℌøἤ �6�6�0�0�7�9�6�4�0�3�6�6 The problem is that the terminal encoding setting remains UTF-8, regardless
of the nominal locale. A GB18030 terminal can be emulated using the $ cat utf8.txt gb18030.txt | iconv -f GB18030 -t UTF-8 鈩櫰粹槀鈩屆羔激 ℙƴ☂ℌøἤ This reverses the problem, such that the GB18030 file is rendered correctly, but the UTF-8 file has been converted to unrelated hanzi characters, rather than the expected rendering of “Python” as non-ASCII characters. With the emulated GB18030 terminal encoding, assuming UTF-8 in Python results in both files being displayed incorrectly: $ python3 -c 'print("UTF-8: ", open("utf8.txt", "r", encoding="utf-8").read().strip()); \
print("GB18030:", open("gb18030.txt", "r", encoding="gb18030").read().strip())' \
| iconv -f GB18030 -t UTF-8
UTF-8: 鈩櫰粹槀鈩屆羔激
GB18030: 鈩櫰粹槀鈩屆羔激
However, setting the locale correctly means that the emulated GB18030 terminal now displays both files as originally intended: $ LANG=zh_CN.gb18030 \
python3 -c 'print("UTF-8: ", open("utf8.txt", "r", encoding="utf-8").read().strip()); \
print("GB18030:", open("gb18030.txt", "r", encoding="gb18030").read().strip())' \
| iconv -f GB18030 -t UTF-8
UTF-8: ℙƴ☂ℌøἤ
GB18030: ℙƴ☂ℌøἤ
The rationale for retaining $ cat gb18030.txt \ | LANG=C python3 -c "import sys; print(sys.stdin.read())" \ | iconv -f GB18030 -t UTF-8 ℙƴ☂ℌøἤ Rather than reverting to the exception currently seen when a UTF-8 based locale is explicitly configured: $ cat gb18030.txt \
| python3 -c "import sys; print(sys.stdin.read())" \
| iconv -f GB18030 -t UTF-8
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte
As an added benefit, environments explicitly configured to use one of the
coercion target locales will implicitly gain the encoding transparency behaviour
currently enabled by default in the Avoiding setting PYTHONIOENCODING during UTF-8 locale coercionRather than changing the default handling of the standard streams during
interpreter initialization, earlier versions of this PEP proposed setting
The current design means that earlier Python versions will instead retain their
default Dropping official support for ASCII based text handling in the legacy C localeWe’ve been trying to get strict bytes/text separation to work reliably in the legacy C locale for over a decade at this point. Not only haven’t we been able to get it to work, neither has anyone else - the only viable alternatives identified have been to pass the bytes along verbatim without eagerly decoding them to text (C/C++, Python 2.x, Ruby, etc), or else to largely ignore the nominal C/C++ locale encoding and assume the use of either UTF-8 (PEP 540, Rust, Go, Node.js, etc) or UTF-16-LE (JVM, .NET CLR). While this PEP ensures that developers that genuinely need to do so can still
opt-in to running their Python code in the legacy C locale (by setting
Providing implicit locale coercion only when running standaloneThe major downside of the proposed design in this PEP is that it introduces a
potential discrepancy between the behaviour of the CPython runtime when it is
run as a standalone application and when it is run as an embedded component
inside a larger system (e.g. Over the course of Python 3.x development, multiple attempts have been made
to improve the handling of incorrect locale settings at the point where the
Python interpreter is initialised. The problem that emerged is that this is
ultimately too late in the interpreter startup process - data such as command
line arguments and the contents of environment variables may have already been
retrieved from the operating system and processed under the incorrect ASCII
text encoding assumption well before The problems created by those inconsistencies were then even harder to diagnose and debug than those created by believing the operating system’s claim that ASCII was a suitable encoding to use for operating system interfaces. This was the case even for the default CPython binary, let alone larger C/C++ applications that embed CPython as a scripting engine. The approach proposed in this PEP handles that problem by moving the locale
coercion as early as possible in the interpreter startup sequence when running
standalone: it takes place directly in the C-level The That said, the reference implementation for this PEP adds most of the functionality to the shared library, with the CLI being updated to unconditionally call two new private APIs: if (_Py_LegacyLocaleDetected()) { _Py_CoerceLegacyLocale(); } These are similar to other “pre-configuration” APIs intended for embedding
applications: they’re designed to be called before If these were made public (either as part of this PEP or in a subsequent RFE), then it would be straightforward for other embedding applications to recreate the same behaviour as is proposed for the CPython CLI. Allowing restoration of the legacy behaviourThe CPython command line interpreter is often used to investigate faults that occur in other applications that embed CPython, and those applications may still be using the C locale even after this PEP is implemented. Providing a simple on/off switch for the locale coercion behaviour makes it much easier to reproduce the behaviour of such applications for debugging purposes, as well as making it easier to reproduce the behaviour of older 3.x runtimes even when running a version with this change applied. Querying LC_CTYPE for C locale detection
As such, it makes sense to check it specifically when attempting to determine whether or not the current locale configuration is likely to cause Unicode handling problems. Explicitly setting LC_CTYPE for UTF-8 locale coercionPython is often used as a glue language, integrating other C/C++ ABI compatible components in the current process, and components written in arbitrary languages in subprocesses. Setting This should be sufficient to ensure that when the locale coercion is activated, the switch to the UTF-8 based locale will be applied consistently across the current process and any subprocesses that inherit the current environment. Avoiding setting LANG for UTF-8 locale coercionEarlier versions of this PEP proposed setting the This was later removed on the grounds that setting only For example, consider a Python application that called the Linux $ LANG=ja_JP.UTF-8 LC_CTYPE=C date 2017年 5月 23日 火曜日 17:31:03 JST $ LANG=ja_JP.UTF-8 LC_CTYPE=C.UTF-8 date # Coercing only LC_CTYPE 2017年 5月 23日 火曜日 17:32:58 JST $ LANG=C.UTF-8 LC_CTYPE=C.UTF-8 date # Coercing both of LC_CTYPE and LANG Tue May 23 17:31:10 JST 2017 With only Avoiding setting LC_ALL for UTF-8 locale coercionEarlier versions of this PEP proposed setting the This was changed after it was determined that just setting $ LANG=C LC_MONETARY=ja_JP.utf8 ./python -c \ "from locale import setlocale, LC_ALL, currency; setlocale(LC_ALL, ''); print(currency(1e6))" ¥1000000 Skipping locale coercion if LC_ALL is set in the current environmentWith locale coercion now only setting Considering locale coercion independently of “UTF-8 mode”With both this PEP’s locale coercion and PEP 540’s UTF-8 mode under consideration for Python 3.7, it makes sense to ask whether or not we can limit ourselves to only doing one or the other, rather than making both changes. The UTF-8 mode proposed in PEP 540 has two major limitations that make it a potential complement to this PEP rather than a potential replacement. First, unlike this PEP, PEP 540’s UTF-8 mode makes it possible to change default
behaviours that are not currently configurable at all. While that’s exactly
what makes the proposal interesting, it’s also what makes it an entirely
unproven approach. By contrast, the approach proposed in this PEP builds
directly atop existing configuration settings for the C locale system (
Secondly, one of the things we know based on that experience is that the proposed locale coercion can resolve problems not only in CPython itself, but also in extension modules that interact with the standard streams, like GNU readline. As an example, consider the following interactive session from a PEP 538 enabled CPython build, where each line after the first is executed by doing “up-arrow, left-arrow x4, delete, enter”: $ LANG=C ./python
Python 3.7.0a0 (heads/pep538-coerce-c-locale:188e780, May 7 2017, 00:21:13)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("ℙƴ☂ℌøἤ")
ℙƴ☂ℌøἤ
>>> print("ℙƴ☂ℌἤ")
ℙƴ☂ℌἤ
>>> print("ℙƴ☂ἤ")
ℙƴ☂ἤ
>>> print("ℙƴἤ")
ℙƴἤ
>>> print("ℙἤ")
ℙἤ
>>> print("ἤ")
ἤ
>>>
This is exactly what we’d expect from a well-behaved command history editor. By contrast, the following is what currently happens on an older release if you only change the Python level stream encoding settings without updating the locale settings: $ LANG=C PYTHONIOENCODING=utf-8:surrogateescape python3
Python 3.5.3 (default, Apr 24 2017, 13:32:13)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("ℙƴ☂ℌøἤ")
ℙƴ☂ℌøἤ
>>> print("ℙƴ☂ℌ�")
File "<stdin>", line 0
^
SyntaxError: 'utf-8' codec can't decode bytes in position 20-21:
invalid continuation byte
That particular misbehaviour is coming from GNU readline, not CPython - because the command history editing wasn’t UTF-8 aware, it corrupted the history buffer and fed such nonsense to stdin that even the surrogateescape error handler was bypassed. While PEP 540’s UTF-8 mode could technically be updated to also reconfigure readline, that’s just one extension module that might be interacting with the standard streams without going through the CPython C API, and any change made by CPython would only apply when readline is running directly as part of Python 3.7 rather than in a separate subprocess. However, if we actually change the configured locale, GNU readline starts behaving itself, without requiring any changes to the embedding application: $ LANG=C.UTF-8 python3
Python 3.5.3 (default, Apr 24 2017, 13:32:13)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("ℙƴ☂ℌøἤ")
ℙƴ☂ℌøἤ
>>> print("ℙƴ☂ℌἤ")
ℙƴ☂ℌἤ
>>> print("ℙƴ☂ἤ")
ℙƴ☂ἤ
>>> print("ℙƴἤ")
ℙƴἤ
>>> print("ℙἤ")
ℙἤ
>>> print("ἤ")
ἤ
>>>
$ LC_CTYPE=C.UTF-8 python3
Python 3.5.3 (default, Apr 24 2017, 13:32:13)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("ℙƴ☂ℌøἤ")
ℙƴ☂ℌøἤ
>>> print("ℙƴ☂ℌἤ")
ℙƴ☂ℌἤ
>>> print("ℙƴ☂ἤ")
ℙƴ☂ἤ
>>> print("ℙƴἤ")
ℙƴἤ
>>> print("ℙἤ")
ℙἤ
>>> print("ἤ")
ἤ
>>>
Enabling C locale coercion and warnings on Mac OS X, iOS and AndroidOn Mac OS X, iOS, and Android, CPython already assumes the use of UTF-8 for system interfaces, and we expect most other locale-aware components to do the same. Accordingly, this PEP originally proposed to disable locale coercion and warnings at build time for these platforms, on the assumption that it would be entirely redundant. However, that assumption turned out to be incorrect, as subsequent
investigations showed that if you explicitly configure In addition, Mac OS X is also frequently used as a development and testing platform for Python software intended for deployment to other *nix environments (such as Linux or Android), and Linux is similarly often used as a development and testing platform for mobile and Mac OS X applications. Accordingly, this PEP enables the locale coercion and warning features by
default on all platforms that use CPython’s ImplementationThe reference implementation is being developed in the
This reference implementation covers not only the enhancement request in issue 28180 [1], but also the Android compatibility fixes needed to resolve issue 28997 [16]. Backporting to earlier Python 3 releasesBackporting to Python 3.6.xIf this PEP is accepted for Python 3.7, redistributors backporting the change specifically to their initial Python 3.6.x release will be both allowed and encouraged. However, such backports should only be undertaken either in conjunction with the changes needed to also provide a suitable locale by default, or else specifically for platforms where such a locale is already consistently available. At least the Fedora project is planning to pursue this approach for the upcoming Fedora 26 release [19]. Backporting to other 3.x releasesWhile the proposed behavioural change is seen primarily as a bug fix addressing Python 3’s current misbehaviour in the default ASCII-based C locale, it still represents a reasonably significant change in the way CPython interacts with the C locale system. As such, while some redistributors may still choose to backport it to even earlier Python 3.x releases based on the needs and interests of their particular user base, this wouldn’t be encouraged as a general practice. However, configuring Python 3 environments (such as base container images) to use these configuration settings by default is both allowed and recommended. AcknowledgementsThe locale coercion approach proposed in this PEP is inspired directly by
Armin Ronacher’s handling of this problem in the $ LANG=C python3 -c 'import click; cli = click.command()(lambda:None); cli()'
Traceback (most recent call last):
...
RuntimeError: Click will abort further execution because Python 3 was
configured to use ASCII as encoding for the environment. Either run this
under Python 2 or consult http://click.pocoo.org/python3/ for mitigation
steps.
This system supports the C.UTF-8 locale which is recommended.
You might be able to resolve your issue by exporting the
following environment variables:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
The change was originally proposed as a downstream patch for Fedora’s system Python 3.6 package [3], and then reformulated as a PEP for Python 3.7 with a section allowing for backports to earlier versions by redistributors. In parallel with the development of the upstream patch, Charalampos Stratakis has been working on the Fedora 26 backport and providing feedback on the practical viability of the proposed changes. The initial draft was posted to the Python Linux SIG for discussion [10] and then amended based on both that discussion and Victor Stinner’s work in PEP 540 [11]. The “ℙƴ☂ℌøἤ” string used in the Unicode handling examples throughout this PEP is taken from Ned Batchelder’s excellent “Pragmatic Unicode” presentation [9]. Stephen Turnbull has long provided valuable insight into the text encoding handling challenges he regularly encounters at the University of Tsukuba (筑波大学). ReferencesCopyrightThis document has been placed in the public domain under the terms of the CC0 1.0 license: https://creativecommons.org/publicdomain/zero/1.0/ |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |