bpo-43510: Implement PEP 597 opt-in EncodingWarning. (GH-19481)

See [PEP 597](https://www.python.org/dev/peps/pep-0597/).

* Add `-X warn_default_encoding` and `PYTHONWARNDEFAULTENCODING`.
* Add EncodingWarning
* Add io.text_encoding()
* open(), TextIOWrapper() emits EncodingWarning when encoding is omitted and warn_default_encoding is enabled.
* _pyio.TextIOWrapper() uses UTF-8 as fallback default encoding used when failed to import locale module. (used during building Python)
* bz2, configparser, gzip, lzma, pathlib, tempfile modules use io.text_encoding().
* What's new entry
This commit is contained in:
Inada Naoki 2021-03-29 12:28:14 +09:00 committed by GitHub
parent 261a452a13
commit 4827483f47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 366 additions and 18 deletions

View file

@ -94,6 +94,7 @@ static const char usage_3[] = "\
otherwise activate automatically)\n\
-X pycache_prefix=PATH: enable writing .pyc files to a parallel tree rooted at the\n\
given directory instead of to the code tree\n\
-X warn_default_encoding: enable opt-in EncodingWarning for 'encoding=None'\n\
\n\
--check-hash-based-pycs always|default|never:\n\
control how Python invalidates hash-based .pyc files\n\
@ -129,7 +130,8 @@ static const char usage_6[] =
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
" debugger. It can be set to the callable of your debugger of choice.\n"
"PYTHONDEVMODE: enable the development mode.\n"
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n"
"PYTHONWARNDEFAULTENCODING: enable opt-in EncodingWarning for 'encoding=None'.\n";
#if defined(MS_WINDOWS)
# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
@ -600,6 +602,7 @@ config_check_consistency(const PyConfig *config)
assert(config->malloc_stats >= 0);
assert(config->site_import >= 0);
assert(config->bytes_warning >= 0);
assert(config->warn_default_encoding >= 0);
assert(config->inspect >= 0);
assert(config->interactive >= 0);
assert(config->optimization_level >= 0);
@ -698,6 +701,7 @@ _PyConfig_InitCompatConfig(PyConfig *config)
config->parse_argv = 0;
config->site_import = -1;
config->bytes_warning = -1;
config->warn_default_encoding = 0;
config->inspect = -1;
config->interactive = -1;
config->optimization_level = -1;
@ -906,6 +910,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_ATTR(site_import);
COPY_ATTR(bytes_warning);
COPY_ATTR(warn_default_encoding);
COPY_ATTR(inspect);
COPY_ATTR(interactive);
COPY_ATTR(optimization_level);
@ -1007,6 +1012,7 @@ _PyConfig_AsDict(const PyConfig *config)
SET_ITEM_WSTR(platlibdir);
SET_ITEM_INT(site_import);
SET_ITEM_INT(bytes_warning);
SET_ITEM_INT(warn_default_encoding);
SET_ITEM_INT(inspect);
SET_ITEM_INT(interactive);
SET_ITEM_INT(optimization_level);
@ -1271,6 +1277,7 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
GET_WSTRLIST(warnoptions);
GET_UINT(site_import);
GET_UINT(bytes_warning);
GET_UINT(warn_default_encoding);
GET_UINT(inspect);
GET_UINT(interactive);
GET_UINT(optimization_level);