diff options
author | Inada Naoki <songofacandy@gmail.com> | 2021-03-29 03:28:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-29 03:28:14 (GMT) |
commit | 4827483f47906fecee6b5d9097df2a69a293a85c (patch) | |
tree | c4d7e34163e9583c06003d5335d020ce27aa4559 /Python | |
parent | 261a452a1300eeeae1428ffd6e6623329c085e2c (diff) | |
download | cpython-4827483f47906fecee6b5d9097df2a69a293a85c.zip cpython-4827483f47906fecee6b5d9097df2a69a293a85c.tar.gz cpython-4827483f47906fecee6b5d9097df2a69a293a85c.tar.bz2 |
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
Diffstat (limited to 'Python')
-rw-r--r-- | Python/initconfig.c | 9 | ||||
-rw-r--r-- | Python/preconfig.c | 9 | ||||
-rw-r--r-- | Python/sysmodule.c | 4 |
3 files changed, 20 insertions, 2 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c index 7886d09..27ae48d 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -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); diff --git a/Python/preconfig.c b/Python/preconfig.c index b8b0c3a..ae1cc3f 100644 --- a/Python/preconfig.c +++ b/Python/preconfig.c @@ -169,6 +169,7 @@ _PyPreCmdline_SetConfig(const _PyPreCmdline *cmdline, PyConfig *config) COPY_ATTR(isolated); COPY_ATTR(use_environment); COPY_ATTR(dev_mode); + COPY_ATTR(warn_default_encoding); return _PyStatus_OK(); #undef COPY_ATTR @@ -257,9 +258,17 @@ _PyPreCmdline_Read(_PyPreCmdline *cmdline, const PyPreConfig *preconfig) cmdline->dev_mode = 0; } + // warn_default_encoding + if (_Py_get_xoption(&cmdline->xoptions, L"warn_default_encoding") + || _Py_GetEnv(cmdline->use_environment, "PYTHONWARNDEFAULTENCODING")) + { + cmdline->warn_default_encoding = 1; + } + assert(cmdline->use_environment >= 0); assert(cmdline->isolated >= 0); assert(cmdline->dev_mode >= 0); + assert(cmdline->warn_default_encoding >= 0); return _PyStatus_OK(); } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 686b6ca..54d70ef 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2514,6 +2514,7 @@ static PyStructSequence_Field flags_fields[] = { {"isolated", "-I"}, {"dev_mode", "-X dev"}, {"utf8_mode", "-X utf8"}, + {"warn_default_encoding", "-X warn_default_encoding"}, {0} }; @@ -2521,7 +2522,7 @@ static PyStructSequence_Desc flags_desc = { "sys.flags", /* name */ flags__doc__, /* doc */ flags_fields, /* fields */ - 15 + 16 }; static int @@ -2560,6 +2561,7 @@ set_flags_from_config(PyInterpreterState *interp, PyObject *flags) SetFlag(config->isolated); SetFlagObj(PyBool_FromLong(config->dev_mode)); SetFlag(preconfig->utf8_mode); + SetFlag(config->warn_default_encoding); #undef SetFlagObj #undef SetFlag return 0; |