@@ -1892,12 +1892,15 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
18921892 PyObject * result ;
18931893 size_t len ;
18941894
1895+ /* stdin is a text stream, so it must have an encoding. */
18951896 stdin_encoding = _PyObject_GetAttrId (fin , & PyId_encoding );
18961897 stdin_errors = _PyObject_GetAttrId (fin , & PyId_errors );
1897- if (!stdin_encoding || !stdin_errors )
1898- /* stdin is a text stream, so it must have an
1899- encoding. */
1898+ if (!stdin_encoding || !stdin_errors ||
1899+ !PyUnicode_Check (stdin_encoding ) ||
1900+ !PyUnicode_Check (stdin_errors )) {
1901+ tty = 0 ;
19001902 goto _readline_errors ;
1903+ }
19011904 stdin_encoding_str = _PyUnicode_AsString (stdin_encoding );
19021905 stdin_errors_str = _PyUnicode_AsString (stdin_errors );
19031906 if (!stdin_encoding_str || !stdin_errors_str )
@@ -1913,8 +1916,12 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
19131916 PyObject * stringpo ;
19141917 stdout_encoding = _PyObject_GetAttrId (fout , & PyId_encoding );
19151918 stdout_errors = _PyObject_GetAttrId (fout , & PyId_errors );
1916- if (!stdout_encoding || !stdout_errors )
1919+ if (!stdout_encoding || !stdout_errors ||
1920+ !PyUnicode_Check (stdout_encoding ) ||
1921+ !PyUnicode_Check (stdout_errors )) {
1922+ tty = 0 ;
19171923 goto _readline_errors ;
1924+ }
19181925 stdout_encoding_str = _PyUnicode_AsString (stdout_encoding );
19191926 stdout_errors_str = _PyUnicode_AsString (stdout_errors );
19201927 if (!stdout_encoding_str || !stdout_errors_str )
@@ -1969,13 +1976,17 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
19691976 Py_XDECREF (po );
19701977 PyMem_FREE (s );
19711978 return result ;
1979+
19721980 _readline_errors :
19731981 Py_XDECREF (stdin_encoding );
19741982 Py_XDECREF (stdout_encoding );
19751983 Py_XDECREF (stdin_errors );
19761984 Py_XDECREF (stdout_errors );
19771985 Py_XDECREF (po );
1978- return NULL ;
1986+ if (tty )
1987+ return NULL ;
1988+
1989+ PyErr_Clear ();
19791990 }
19801991
19811992 /* Fallback if we're not interactive */
0 commit comments