@@ -1926,12 +1926,15 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
1926
1926
PyObject * result ;
1927
1927
size_t len ;
1928
1928
1929
+ /* stdin is a text stream, so it must have an encoding. */
1929
1930
stdin_encoding = _PyObject_GetAttrId (fin , & PyId_encoding );
1930
1931
stdin_errors = _PyObject_GetAttrId (fin , & PyId_errors );
1931
- if (!stdin_encoding || !stdin_errors )
1932
- /* stdin is a text stream, so it must have an
1933
- encoding. */
1932
+ if (!stdin_encoding || !stdin_errors ||
1933
+ !PyUnicode_Check (stdin_encoding ) ||
1934
+ !PyUnicode_Check (stdin_errors )) {
1935
+ tty = 0 ;
1934
1936
goto _readline_errors ;
1937
+ }
1935
1938
stdin_encoding_str = PyUnicode_AsUTF8 (stdin_encoding );
1936
1939
stdin_errors_str = PyUnicode_AsUTF8 (stdin_errors );
1937
1940
if (!stdin_encoding_str || !stdin_errors_str )
@@ -1947,8 +1950,12 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
1947
1950
PyObject * stringpo ;
1948
1951
stdout_encoding = _PyObject_GetAttrId (fout , & PyId_encoding );
1949
1952
stdout_errors = _PyObject_GetAttrId (fout , & PyId_errors );
1950
- if (!stdout_encoding || !stdout_errors )
1953
+ if (!stdout_encoding || !stdout_errors ||
1954
+ !PyUnicode_Check (stdout_encoding ) ||
1955
+ !PyUnicode_Check (stdout_errors )) {
1956
+ tty = 0 ;
1951
1957
goto _readline_errors ;
1958
+ }
1952
1959
stdout_encoding_str = PyUnicode_AsUTF8 (stdout_encoding );
1953
1960
stdout_errors_str = PyUnicode_AsUTF8 (stdout_errors );
1954
1961
if (!stdout_encoding_str || !stdout_errors_str )
@@ -2002,13 +2009,17 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
2002
2009
Py_XDECREF (po );
2003
2010
PyMem_FREE (s );
2004
2011
return result ;
2012
+
2005
2013
_readline_errors :
2006
2014
Py_XDECREF (stdin_encoding );
2007
2015
Py_XDECREF (stdout_encoding );
2008
2016
Py_XDECREF (stdin_errors );
2009
2017
Py_XDECREF (stdout_errors );
2010
2018
Py_XDECREF (po );
2011
- return NULL ;
2019
+ if (tty )
2020
+ return NULL ;
2021
+
2022
+ PyErr_Clear ();
2012
2023
}
2013
2024
2014
2025
/* Fallback if we're not interactive */
0 commit comments