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 540 – Add a new UTF-8 Mode | peps.python.org
PEP 540 – Add a new UTF-8 Mode
PEP 540 – Add a new UTF-8 Mode
AbstractAdd a new “UTF-8 Mode” to enhance Python’s use of UTF-8. When UTF-8 Mode is active, Python will:
This mode is off by default, but is automatically activated when using the “POSIX” locale. Add the RationaleLocale encoding and UTF-8Python 3.6 uses the locale encoding for filenames, environment variables, standard streams, etc. The locale encoding is inherited from the locale; the encoding and the locale are tightly coupled. Many users inherit the ASCII encoding from the POSIX locale, aka the “C” locale, but are unable change the locale for various reasons. This encoding is very limited in term of Unicode support: any non-ASCII character is likely to cause trouble. It isn’t always easy to get an accurate locale. Locales don’t get the
exact same name on different Linux distributions, FreeBSD, macOS, etc.
And some locales, like the recent On the flip side, Python 3.6 is already using UTF-8 by default on macOS,
Android and Windows (PEP 529) for most functions – although
UTF-8 support is nearly ubiquitous for data read and written by modern platforms. It also has excellent support in Python. The problem is simply that the locale is frequently misconfigured. An obvious solution suggests itself: ignore the locale encoding and use UTF-8. Passthrough for undecodable bytes: surrogateescapeWhen decoding bytes from UTF-8 using the default Unix command line tools like Python 3 already has a solution to behave like Unix tools and Python 2:
the UTF-8 Mode sets the However, users have a different expectation on files. Files are expected
to be properly encoded, and Python is expected to fail early when
No change by default for best backward compatibilityWhile UTF-8 is perfect in most cases, sometimes the locale encoding is actually the best encoding. This PEP changes the behaviour for the POSIX locale since this locale is usually equivalent to the ASCII encoding, whereas UTF-8 is a much better choice. It does not change the behaviour for other locales to prevent any risk or regression. As users are responsible to enable explicitly the new UTF-8 Mode for these other locales, they are responsible for any potential mojibake issues caused by UTF-8 Mode. ProposalAdd a new UTF-8 Mode to use the UTF-8 encoding, ignore the locale
encoding, and change Add the new This mode is disabled by default and enabled by the POSIX locale. Users
can explicitly disable UTF-8 Mode with the command-line option For standard streams, the On Windows, the Effects of UTF-8 Mode:
Side effects:
Relationship with the locale coercion (PEP 538)The POSIX locale enables the locale coercion (PEP 538) and the UTF-8 mode (PEP 540). When the locale coercion is enabled, enabling the UTF-8 mode has no additional effect. The UTF-8 Mode has the same effect as locale coercion:
These changes only affect Python code. But the locale coercion has
additional effects: the On platforms like Centos 7 where locale coercion is not supported, the POSIX locale only enables UTF-8 Mode. In this case, Python code uses the UTF-8 encoding and ignores the locale encoding, whereas non-Python code uses the locale encoding, which is usually ASCII for the POSIX locale. While the UTF-8 Mode is supported on all platforms and can be enabled with any locale, the locale coercion is not supported by all platforms and is restricted to the POSIX locale. The UTF-8 Mode has only an impact on Python child processes when the
The benefit of the locale coercion approach is that it helps ensure that encoding handling in binary extension modules and child processes is consistent with Python’s encoding handling. The upside of the UTF-8 Mode approach is that it allows an embedding application to change the interpreter’s behaviour without having to change the process global locale settings. Backward CompatibilityThe only backward incompatible change is that the POSIX locale now
enables the UTF-8 Mode by default: it will now use the UTF-8 encoding,
ignore the locale encoding, and change Annex: Encodings And Error HandlersUTF-8 Mode changes the default encoding and error handler used by
Encoding and error handler
By comparison, Python 3.6 uses:
Encoding and error handler on WindowsOn Windows, the encodings and error handlers are different:
By comparison, Python 3.6 uses:
The “Legacy Windows FS encoding” is enabled by the
If stdin and/or stdout is redirected to a pipe, Note There is no POSIX locale on Windows. The ANSI code page is used as the locale encoding, and this code page never uses the ASCII encoding. Links
Post History
Version History
CopyrightThis document has been placed in the public domain. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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. |