summaryrefslogtreecommitdiffstats
path: root/Modules/_io/textio.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-10-31 00:02:09 (GMT)
committerGitHub <noreply@github.com>2020-10-31 00:02:09 (GMT)
commit710e82630775774dceba5e8f24b1b10e6dfaf9b7 (patch)
treeb4415da3ed5f8bc2ad2a52ec9a8544b9e50d975f /Modules/_io/textio.c
parent06f8c3328dcd81c84d1ee2b3a57b5381dcb38482 (diff)
downloadcpython-710e82630775774dceba5e8f24b1b10e6dfaf9b7.zip
cpython-710e82630775774dceba5e8f24b1b10e6dfaf9b7.tar.gz
cpython-710e82630775774dceba5e8f24b1b10e6dfaf9b7.tar.bz2
bpo-42208: Add _Py_GetLocaleEncoding() (GH-23050)
_io.TextIOWrapper no longer calls getpreferredencoding(False) of _bootlocale to get the locale encoding, but calls _Py_GetLocaleEncoding() instead. Add config_get_fs_encoding() sub-function. Reorganize also config_get_locale_encoding() code.
Diffstat (limited to 'Modules/_io/textio.c')
-rw-r--r--Modules/_io/textio.c26
1 files changed, 4 insertions, 22 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 699b7e9..2078bb3 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -10,6 +10,7 @@
#include "Python.h"
#include "pycore_interp.h" // PyInterpreterState.fs_codec
#include "pycore_long.h" // _PyLong_GetZero()
+#include "pycore_fileutils.h" // _Py_GetLocaleEncoding()
#include "pycore_object.h"
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "structmember.h" // PyMemberDef
@@ -27,7 +28,6 @@ _Py_IDENTIFIER(_dealloc_warn);
_Py_IDENTIFIER(decode);
_Py_IDENTIFIER(fileno);
_Py_IDENTIFIER(flush);
-_Py_IDENTIFIER(getpreferredencoding);
_Py_IDENTIFIER(isatty);
_Py_IDENTIFIER(mode);
_Py_IDENTIFIER(name);
@@ -1155,29 +1155,11 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
}
}
if (encoding == NULL && self->encoding == NULL) {
- PyObject *locale_module = _PyIO_get_locale_module(state);
- if (locale_module == NULL)
- goto catch_ImportError;
- self->encoding = _PyObject_CallMethodIdOneArg(
- locale_module, &PyId_getpreferredencoding, Py_False);
- Py_DECREF(locale_module);
+ self->encoding = _Py_GetLocaleEncoding();
if (self->encoding == NULL) {
- catch_ImportError:
- /*
- Importing locale can raise an ImportError because of
- _functools, and locale.getpreferredencoding can raise an
- ImportError if _locale is not available. These will happen
- during module building.
- */
- if (PyErr_ExceptionMatches(PyExc_ImportError)) {
- PyErr_Clear();
- self->encoding = PyUnicode_FromString("ascii");
- }
- else
- goto error;
+ goto error;
}
- else if (!PyUnicode_Check(self->encoding))
- Py_CLEAR(self->encoding);
+ assert(PyUnicode_Check(self->encoding));
}
if (self->encoding != NULL) {
encoding = PyUnicode_AsUTF8(self->encoding);