STINNER Victor <victor.stin...@haypocalc.com> added the comment:

Here is a patch catching the _PyUnicode_AsString() error.

input() uses sys.stdout.encoding to encode the prompt to a byte string, but 
PyOS_StdioReadline() writes the prompt to stderr (it should use sys_stdout).

I don't know which encoding should be used if sys.stdout.encoding is None (eg. 
if sys.stdout is a StringIO() object).

StringIO() of _io module has no encoding because it stores unicode characters, 
not bytes. StringIO() of _pyio module is based on BytesIO() and use utf8 
encoding, but the reference implementation is now _io.

----------
Added file: http://bugs.python.org/file17324/input_stdout_encoding.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue8256>
_______________________________________
Index: Python/bltinmodule.c
===================================================================
--- Python/bltinmodule.c        (révision 81157)
+++ Python/bltinmodule.c        (copie de travail)
@@ -1618,6 +1618,7 @@
         if (promptarg != NULL) {
             PyObject *stringpo;
             PyObject *stdout_encoding;
+            char *stdout_encoding_str;
             stdout_encoding = PyObject_GetAttrString(fout,
                                                      "encoding");
             if (stdout_encoding == NULL) {
@@ -1630,8 +1631,17 @@
                 Py_DECREF(stdout_encoding);
                 return NULL;
             }
+            if (stdout_encoding != Py_None)
+                stdout_encoding_str = _PyUnicode_AsString(stdout_encoding);
+            else
+                stdout_encoding_str = "utf-8";
+            if (stdout_encoding_str == NULL) {
+                Py_DECREF(stdin_encoding);
+                Py_DECREF(stdout_encoding);
+                return NULL;
+            }
             po = PyUnicode_AsEncodedString(stringpo,
-                _PyUnicode_AsString(stdout_encoding), NULL);
+                                           stdout_encoding_str, NULL);
             Py_DECREF(stdout_encoding);
             Py_DECREF(stringpo);
             if (po == NULL) {
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to