diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-09-30 12:50:32 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-09-30 12:50:32 (GMT) |
commit | 008fc77e1e443e799be19aebb697b7daf335a0c7 (patch) | |
tree | d96e6e03967f36f2fb8222d2531271dc8c44c572 | |
parent | f1c780870a455c36b03a9076d2b7457f8667088c (diff) | |
parent | a59018c7ab905584ed94d35b2cf93067e116f8f2 (diff) | |
download | cpython-008fc77e1e443e799be19aebb697b7daf335a0c7.zip cpython-008fc77e1e443e799be19aebb697b7daf335a0c7.tar.gz cpython-008fc77e1e443e799be19aebb697b7daf335a0c7.tar.bz2 |
Issue #25182: The stdprinter (used as sys.stderr before the io module is
imported at startup) now uses the backslashreplace error handler.
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Objects/fileobject.c | 25 |
2 files changed, 24 insertions, 4 deletions
@@ -11,6 +11,9 @@ Release date: TBA 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 comprehensions correspond to the opening brace. diff --git a/Objects/fileobject.c b/Objects/fileobject.c index 1b18410..a836cb3 100644 --- a/Objects/fileobject.c +++ b/Objects/fileobject.c @@ -372,8 +372,11 @@ PyFile_NewStdPrinter(int fd) static PyObject * stdprinter_write(PyStdPrinter_Object *self, PyObject *args) { + PyObject *unicode; + PyObject *bytes = NULL; char *str; Py_ssize_t n; + int _errno; if (self->fd < 0) { /* fd might be invalid on Windows @@ -383,13 +386,27 @@ stdprinter_write(PyStdPrinter_Object *self, PyObject *args) Py_RETURN_NONE; } - /* encode Unicode to UTF-8 */ - if (!PyArg_ParseTuple(args, "s", &str)) + if (!PyArg_ParseTuple(args, "U", &unicode)) 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 (errno == EAGAIN) { + if (_errno == EAGAIN) { PyErr_Clear(); Py_RETURN_NONE; } |