Commit 4a7b5d5b authored by Martin v. Löwis's avatar Martin v. Löwis

Decode input() with stdin.encoding. Fixes #1097.

parent 4edae68f
...@@ -1314,7 +1314,14 @@ builtin_input(PyObject *self, PyObject *args) ...@@ -1314,7 +1314,14 @@ builtin_input(PyObject *self, PyObject *args)
PyObject *po; PyObject *po;
char *prompt; char *prompt;
char *s; char *s;
PyObject *stdin_encoding;
PyObject *result; PyObject *result;
stdin_encoding = PyObject_GetAttrString(fin, "encoding");
if (!stdin_encoding)
/* stdin is a text stream, so it must have an
encoding. */
return NULL;
tmp = PyObject_CallMethod(fout, "flush", ""); tmp = PyObject_CallMethod(fout, "flush", "");
if (tmp == NULL) if (tmp == NULL)
PyErr_Clear(); PyErr_Clear();
...@@ -1322,10 +1329,13 @@ builtin_input(PyObject *self, PyObject *args) ...@@ -1322,10 +1329,13 @@ builtin_input(PyObject *self, PyObject *args)
Py_DECREF(tmp); Py_DECREF(tmp);
if (promptarg != NULL) { if (promptarg != NULL) {
po = PyObject_Str(promptarg); po = PyObject_Str(promptarg);
if (po == NULL) if (po == NULL) {
Py_DECREF(stdin_encoding);
return NULL; return NULL;
}
prompt = PyString_AsString(po); prompt = PyString_AsString(po);
if (prompt == NULL) { if (prompt == NULL) {
Py_DECREF(stdin_encoding);
Py_DECREF(po); Py_DECREF(po);
return NULL; return NULL;
} }
...@@ -1339,6 +1349,7 @@ builtin_input(PyObject *self, PyObject *args) ...@@ -1339,6 +1349,7 @@ builtin_input(PyObject *self, PyObject *args)
if (s == NULL) { if (s == NULL) {
if (!PyErr_Occurred()) if (!PyErr_Occurred())
PyErr_SetNone(PyExc_KeyboardInterrupt); PyErr_SetNone(PyExc_KeyboardInterrupt);
Py_DECREF(stdin_encoding);
return NULL; return NULL;
} }
if (*s == '\0') { if (*s == '\0') {
...@@ -1353,9 +1364,13 @@ builtin_input(PyObject *self, PyObject *args) ...@@ -1353,9 +1364,13 @@ builtin_input(PyObject *self, PyObject *args)
result = NULL; result = NULL;
} }
else { else {
result = PyUnicode_FromStringAndSize(s, len-1); result = PyUnicode_Decode
(s, len-1,
PyUnicode_AsString(stdin_encoding),
NULL);
} }
} }
Py_DECREF(stdin_encoding);
PyMem_FREE(s); PyMem_FREE(s);
return result; return result;
} }
......
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