summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-11-04 10:20:10 (GMT)
committerGitHub <noreply@github.com>2020-11-04 10:20:10 (GMT)
commit3529718925f40d14ed48d281d809187bc7314a14 (patch)
tree1f51f7fb13e93df52a45291fe5384de6e9e09a16 /Python
parent0001a1b69ecda47b0406daa88c2943877580bcae (diff)
downloadcpython-3529718925f40d14ed48d281d809187bc7314a14.zip
cpython-3529718925f40d14ed48d281d809187bc7314a14.tar.gz
cpython-3529718925f40d14ed48d281d809187bc7314a14.tar.bz2
bpo-42236: os.device_encoding() respects UTF-8 Mode (GH-23119)
On Unix, the os.device_encoding() function now returns 'UTF-8' rather than the device encoding if the Python UTF-8 Mode is enabled.
Diffstat (limited to 'Python')
-rw-r--r--Python/fileutils.c18
-rw-r--r--Python/initconfig.c13
2 files changed, 14 insertions, 17 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 5177b37..b589d73 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -55,9 +55,6 @@ get_surrogateescape(_Py_error_handler errors, int *surrogateescape)
PyObject *
_Py_device_encoding(int fd)
{
-#if defined(MS_WINDOWS)
- UINT cp;
-#endif
int valid;
_Py_BEGIN_SUPPRESS_IPH
valid = isatty(fd);
@@ -66,6 +63,7 @@ _Py_device_encoding(int fd)
Py_RETURN_NONE;
#if defined(MS_WINDOWS)
+ UINT cp;
if (fd == 0)
cp = GetConsoleCP();
else if (fd == 1 || fd == 2)
@@ -74,16 +72,14 @@ _Py_device_encoding(int fd)
cp = 0;
/* GetConsoleCP() and GetConsoleOutputCP() return 0 if the application
has no console */
- if (cp != 0)
- return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
-#elif defined(CODESET)
- {
- char *codeset = nl_langinfo(CODESET);
- if (codeset != NULL && codeset[0] != 0)
- return PyUnicode_FromString(codeset);
+ if (cp == 0) {
+ Py_RETURN_NONE;
}
+
+ return PyUnicode_FromFormat("cp%u", (unsigned int)cp);
+#else
+ return _Py_GetLocaleEncodingObject();
#endif
- Py_RETURN_NONE;
}
#if !defined(_Py_FORCE_UTF8_FS_ENCODING) && !defined(MS_WINDOWS)
diff --git a/Python/initconfig.c b/Python/initconfig.c
index 7bb28ed..15fb3e4 100644
--- a/Python/initconfig.c
+++ b/Python/initconfig.c
@@ -1515,8 +1515,8 @@ config_init_stdio_encoding(PyConfig *config,
{
PyStatus status;
- /* If Py_SetStandardStreamEncoding() have been called, use these
- parameters. */
+ /* If Py_SetStandardStreamEncoding() has been called, use its
+ arguments if they are not NULL. */
if (config->stdio_encoding == NULL && _Py_StandardStreamEncoding != NULL) {
status = CONFIG_SET_BYTES_STR(config, &config->stdio_encoding,
_Py_StandardStreamEncoding,
@@ -1535,6 +1535,7 @@ config_init_stdio_encoding(PyConfig *config,
}
}
+ // Exit if encoding and errors are defined
if (config->stdio_encoding != NULL && config->stdio_errors != NULL) {
return _PyStatus_OK();
}
@@ -1634,12 +1635,12 @@ config_get_fs_encoding(PyConfig *config, const PyPreConfig *preconfig,
if (preconfig->utf8_mode) {
return PyConfig_SetString(config, fs_encoding, L"utf-8");
}
- else if (_Py_GetForceASCII()) {
+
+ if (_Py_GetForceASCII()) {
return PyConfig_SetString(config, fs_encoding, L"ascii");
}
- else {
- return config_get_locale_encoding(config, preconfig, fs_encoding);
- }
+
+ return config_get_locale_encoding(config, preconfig, fs_encoding);
#endif // !MS_WINDOWS
}