Repository navigation
PEP 540: Add a new UTF-8 mode #73426
Copy link
Copy link
Closed
Labels
3.7 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-unicodetype-featureA feature request or enhancementA feature request or enhancement
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = None
closed_at = <Date 2018-01-17.14:28:41.827>
created_at = <Date 2017-01-11.11:19:52.086>
labels = ['interpreter-core', 'type-feature', 'library', 'expert-unicode', '3.7']
title = 'PEP 540: Add a new UTF-8 mode'
updated_at = <Date 2022-02-08.11:52:04.506>
user = 'https://github.com/vstinner'bugs.python.org fields:
activity = <Date 2022-02-08.11:52:04.506>
actor = 'yan12125'
assignee = 'none'
closed = True
closed_date = <Date 2018-01-17.14:28:41.827>
closer = 'vstinner'
components = ['Interpreter Core', 'Library (Lib)', 'Unicode']
creation = <Date 2017-01-11.11:19:52.086>
creator = 'vstinner'
dependencies = []
files = ['47385']
hgrepos = []
issue_num = 29240
keywords = ['patch']
message_count = 37.0
messages = ['285214', '285215', '285216', '285275', '285276', '285277', '285278', '285280', '285296', '285298', '285325', '285332', '285357', '285407', '285482', '307694', '307695', '308182', '308183', '308198', '308213', '308217', '308430', '308448', '308915', '308916', '309782', '309798', '309958', '309959', '310029', '310092', '310097', '310177', '310443', '310444', '412665']
nosy_count = 4.0
nosy_names = ['vstinner', 'ezio.melotti', 'methane', 'eryksun']
pr_nums = ['855', '4838', '4895', '4899', '4968', '5145', '5148', '5170', '4174', '5203', '5204', '5272']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue29240'
versions = ['Python 3.7']Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.7 (EOL)end of lifeend of lifeinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-unicodetype-featureA feature request or enhancementA feature request or enhancement
Activity
vstinner commentedon Jan 11, 2017
This issue tracks the implementation of the PEP-540.
Attached pep540_cli.py script can be used to play with it.
vstinner commentedon Jan 11, 2017
PEP-540.patch: first draft
Changes:
Allowed options:
Prioririties (highest to lowest):
TODO:
vstinner commentedon Jan 11, 2017
Examples with pep540_cli.py.
Python 3.5:
Patched Python 3.7:
vstinner commentedon Jan 11, 2017
PEP-540-2.patch: Patch version 2, updated to the latest version of the PEP-540. It has no more FIXME/TODO and has more unit tests. The main change is that the strict mode doesn't use strict anymore for OS data, but keeps surrogateescape. See the PEP for the rationale (especially the "Use the strict error handler for operating system data" alternative).
vstinner commentedon Jan 11, 2017
Oops, I introduced an obvious bug in my latest refactoring. It's now fixed in the patch version 3: PEP-540-3.patch.
vstinner commentedon Jan 11, 2017
Hum, PEP-540-3.patch doesn't work if the locale encoding is different than ASCII and UTF-8. argv must be reencoded:
The result should not depend on the locale, it should be the same than:
vstinner commentedon Jan 11, 2017
I only tested the the PEP-540 implementation on Linux.
The PEP and its implementation should adjusted for Windows, especially Windows-only env vars like PYTHONLEGACYWINDOWSFSENCODING.
Changes are maybe also needed for Mac OS X and Android, which always use UTF-8. Currently, the locale encoding is still used on these platforms (ex: by open()). Is it possible to a locale encoding different than UTF-8 on Android for example?
methane commentedon Jan 11, 2017
I want to skip reencoding.
On UTF-8 mode, arbitrary bytes in cmdline (e.g. broken filename passed by xarg) should be able to roundtrip by UTF-8/surrogateescape.
I don't trust wcstombs/mbstowcs. It may not guarantee round tripping of arbitrary bytes.
Can -X utf8 option be processed before Py_Main()?
vstinner commentedon Jan 12, 2017
I'm trying to implement that, but it's hard to factorize the code. I will probably have to duplicate the code handling -E, -X utf8, PYTHONMALLOC and PYTHONUTF8 for wchar_t* (UCS4 or UTF-16) and char* (bytes).
vstinner commentedon Jan 12, 2017
Hum, test_utf8mode lacks an unit test on the -E command line option:
PYTHONUTF8 should be ignored if -E is used.
16 remaining items
vstinner commentedon Dec 21, 2017
New changeset 424315f by Victor Stinner in branch 'master':
bpo-29240: Skip test_readline.test_nonascii() (bpo-4968)
424315f
vstinner commentedon Dec 21, 2017
IHMO test_readline should be fixed by ignoring the UTF-8 mode in Py_EncodeLocale/Py_DecodeLocale, but only when called from the Python readline module. We need maybe new functions, something like: Py_EncodeCurrentLocale/Py_DecodeCurrentLocale.
I will work on a patch when I will be back from holiday. In the meanwhile, I skipped the test to repair FreeBSD 3.x buildbots.
vstinner commentedon Jan 10, 2018
New changeset 2cba6b8 by Victor Stinner in branch 'master':
bpo-29240: readline now ignores the UTF-8 Mode (bpo-5145)
2cba6b8
vstinner commentedon Jan 11, 2018
New changeset cb3ae55 by Victor Stinner in branch 'master':
bpo-29240: Ignore UTF-8 Mode in time module (bpo-5148)
cb3ae55
vstinner commentedon Jan 15, 2018
Attached test_all_locales.py is a test suite for locale functions: os.strerror(), locale.localeconv(), time.strftime(). I tested it on Linux Fedora 27, FreeBSD 11.0 and macOS 10.13.2.
The test should always pass on Python 2.7. On Python 3.6 and the master branch with PR 5170, 2 tests on numeric localeconv() fail because Python uses the wrong encoding: see bpo-31900. master with PR 5170 now has less encoding bugs than Python 3.6.
vstinner commentedon Jan 15, 2018
New changeset 7ed7aea by Victor Stinner in branch 'master':
bpo-29240: Fix locale encodings in UTF-8 Mode (bpo-5170)
7ed7aea
vstinner commentedon Jan 16, 2018
Oh, this change broke test_nonascii() of test_readline() on FreeBSD.
Previsously, readline used ASCII/surrogateescape encoding for the POSIX locale. Now, mbstowcs() / wcstombs() is called directly, with the surrogateescape error handler.
vstinner commentedon Jan 16, 2018
New changeset c495e79 by Victor Stinner in branch 'master':
Skip test_readline.test_nonascii() on C locale (bpo-5203)
c495e79
vstinner commentedon Jan 16, 2018
New changeset c2740e8 by Victor Stinner (Miss Islington (bot)) in branch '3.6':
Skip test_readline.test_nonascii() on C locale (GH-5203) (bpo-5204)
c2740e8
vstinner commentedon Jan 17, 2018
test_readline pass again on all buildbots, especially on FreeBSD 3.6 and 3.x buildbots.
There are no more known issues, the implementation of the PEP-540 (UTF-8 Mode) is now complete!
vstinner commentedon Jan 22, 2018
New changeset 9089a26 by Victor Stinner in branch 'master':
bpo-29240: PyUnicode_DecodeLocale() uses UTF-8 on Android (bpo-5272)
9089a26
vstinner commentedon Jan 22, 2018
I partially reverted the commit 7ed7aea: on Android, UTF-8 is now always used, again. Paul Peny (aka pmpp) confirmed me that my commit broke Python on Android, at least with API 19 (locales don't work properly before API 21).
vstinner commentedon Feb 6, 2022
Oh, this change broke the mbcs alias on Windows and the test_codecs and test_site tests (2 tests!) missed the bug :-( I fixed it in:
New changeset 04dd60e by Victor Stinner in branch 'main':
bpo-46659: Update the test on the mbcs codec alias (GH-31168)
04dd60e