[Python-checkins] cpython (merge 3.4 -> 3.5): Issue #25182: The stdprinter (used as sys.stderr before the io module is

serhiy.storchaka python-checkins at python.org
Wed Sep 30 15:03:41 CEST 2015


https://hg.python.org/cpython/rev/e8b6c6c433a4
changeset: 98436:e8b6c6c433a4
branch: 3.5
parent: 98413:73b6b88ac28a
parent: 98435:6347b154dd67
user: Serhiy Storchaka <storchaka at gmail.com>
date: Wed Sep 30 15:50:32 2015 +0300
summary:
 Issue #25182: The stdprinter (used as sys.stderr before the io module is
imported at startup) now uses the backslashreplace error handler.
files:
 Misc/NEWS | 3 +++
 Objects/fileobject.c | 25 +++++++++++++++++++++----
 2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -11,6 +11,9 @@
 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
--- a/Objects/fileobject.c
+++ b/Objects/fileobject.c
@@ -372,8 +372,11 @@
 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 @@
 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;
 }
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /