bpo-36775: _PyCoreConfig only uses wchar_t* (GH-13062)

_PyCoreConfig: Change filesystem_encoding, filesystem_errors,
stdio_encoding and stdio_errors fields type from char* to wchar_t*.

Changes:

* PyInterpreterState: replace fscodec_initialized (int) with fs_codec
  structure.
* Add get_error_handler_wide() and unicode_encode_utf8() helper
  functions.
* Add error_handler parameter to unicode_encode_locale()
  and unicode_decode_locale().
* Remove _PyCoreConfig_SetString().
* Rename _PyCoreConfig_SetWideString() to _PyCoreConfig_SetString().
* Rename _PyCoreConfig_SetWideStringFromString()
  to _PyCoreConfig_DecodeLocale().
This commit is contained in:
Victor Stinner 2019-05-02 14:56:30 -04:00 committed by GitHub
parent 6ae2bbbdfc
commit 709d23dee6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 357 additions and 220 deletions

View file

@ -1668,7 +1668,7 @@ is_valid_fd(int fd)
static PyObject*
create_stdio(const _PyCoreConfig *config, PyObject* io,
int fd, int write_mode, const char* name,
const char* encoding, const char* errors)
const wchar_t* encoding, const wchar_t* errors)
{
PyObject *buf = NULL, *stream = NULL, *text = NULL, *raw = NULL, *res;
const char* mode;
@ -1718,7 +1718,7 @@ create_stdio(const _PyCoreConfig *config, PyObject* io,
#ifdef MS_WINDOWS
/* Windows console IO is always UTF-8 encoded */
if (PyWindowsConsoleIO_Check(raw))
encoding = "utf-8";
encoding = L"utf-8";
#endif
text = PyUnicode_FromString(name);
@ -1754,10 +1754,25 @@ create_stdio(const _PyCoreConfig *config, PyObject* io,
newline = "\n";
#endif
stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OsssOO",
buf, encoding, errors,
PyObject *encoding_str = PyUnicode_FromWideChar(encoding, -1);
if (encoding_str == NULL) {
Py_CLEAR(buf);
goto error;
}
PyObject *errors_str = PyUnicode_FromWideChar(errors, -1);
if (errors_str == NULL) {
Py_CLEAR(buf);
Py_CLEAR(encoding_str);
goto error;
}
stream = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "OOOsOO",
buf, encoding_str, errors_str,
newline, line_buffering, write_through);
Py_CLEAR(buf);
Py_CLEAR(encoding_str);
Py_CLEAR(errors_str);
if (stream == NULL)
goto error;
@ -1874,7 +1889,7 @@ init_sys_streams(PyInterpreterState *interp)
fd = fileno(stderr);
std = create_stdio(config, iomod, fd, 1, "<stderr>",
config->stdio_encoding,
"backslashreplace");
L"backslashreplace");
if (std == NULL)
goto error;