Commit 008fc77e authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #25182: The stdprinter (used as sys.stderr before the io module is

imported at startup) now uses the backslashreplace error handler.
parents f1c78087 a59018c7
...@@ -11,6 +11,9 @@ Release date: TBA ...@@ -11,6 +11,9 @@ Release date: TBA
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #25182: The stdprinter (used as sys.stderr before the io module is
imported at startup) now uses the backslashreplace error handler.
- Issue #25131: Make the line number and column offset of set/dict literals and - Issue #25131: Make the line number and column offset of set/dict literals and
comprehensions correspond to the opening brace. comprehensions correspond to the opening brace.
......
...@@ -372,8 +372,11 @@ PyFile_NewStdPrinter(int fd) ...@@ -372,8 +372,11 @@ PyFile_NewStdPrinter(int fd)
static PyObject * static PyObject *
stdprinter_write(PyStdPrinter_Object *self, PyObject *args) stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
{ {
PyObject *unicode;
PyObject *bytes = NULL;
char *str; char *str;
Py_ssize_t n; Py_ssize_t n;
int _errno;
if (self->fd < 0) { if (self->fd < 0) {
/* fd might be invalid on Windows /* fd might be invalid on Windows
...@@ -383,13 +386,27 @@ stdprinter_write(PyStdPrinter_Object *self, PyObject *args) ...@@ -383,13 +386,27 @@ stdprinter_write(PyStdPrinter_Object *self, PyObject *args)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
/* encode Unicode to UTF-8 */ if (!PyArg_ParseTuple(args, "U", &unicode))
if (!PyArg_ParseTuple(args, "s", &str))
return NULL; return NULL;
n = _Py_write(self->fd, str, strlen(str)); /* encode Unicode to UTF-8 */
str = PyUnicode_AsUTF8AndSize(unicode, &n);
if (str == NULL) {
PyErr_Clear();
bytes = _PyUnicode_AsUTF8String(unicode, "backslashreplace");
if (bytes == NULL)
return NULL;
if (PyBytes_AsStringAndSize(bytes, &str, &n) < 0) {
Py_DECREF(bytes);
return NULL;
}
}
n = _Py_write(self->fd, str, n);
_errno = errno;
Py_XDECREF(bytes);
if (n == -1) { if (n == -1) {
if (errno == EAGAIN) { if (_errno == EAGAIN) {
PyErr_Clear(); PyErr_Clear();
Py_RETURN_NONE; Py_RETURN_NONE;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment