@@ -1640,76 +1640,65 @@ builtin_input(PyObject *self, PyObject *args)
16401640
16411641 /* If we're interactive, use (GNU) readline */
16421642 if (tty ) {
1643- PyObject * po ;
1643+ PyObject * po = NULL ;
16441644 char * prompt ;
1645- char * s ;
1646- PyObject * stdin_encoding ;
1647- char * stdin_encoding_str ;
1645+ char * s = NULL ;
1646+ PyObject * stdin_encoding = NULL , * stdin_errors = NULL ;
1647+ PyObject * stdout_encoding = NULL , * stdout_errors = NULL ;
1648+ char * stdin_encoding_str , * stdin_errors_str ;
16481649 PyObject * result ;
16491650 size_t len ;
16501651
16511652 stdin_encoding = PyObject_GetAttrString (fin , "encoding" );
1652- if (!stdin_encoding )
1653+ stdin_errors = PyObject_GetAttrString (fin , "errors" );
1654+ if (!stdin_encoding || !stdin_errors )
16531655 /* stdin is a text stream, so it must have an
16541656 encoding. */
1655- return NULL ;
1657+ goto _readline_errors ;
16561658 stdin_encoding_str = _PyUnicode_AsString (stdin_encoding );
1657- if (stdin_encoding_str == NULL ) {
1658- Py_DECREF (stdin_encoding );
1659- return NULL ;
1660- }
1659+ stdin_errors_str = _PyUnicode_AsString (stdin_errors );
1660+ if (!stdin_encoding_str || !stdin_errors_str )
1661+ goto _readline_errors ;
16611662 tmp = PyObject_CallMethod (fout , "flush" , "" );
16621663 if (tmp == NULL )
16631664 PyErr_Clear ();
16641665 else
16651666 Py_DECREF (tmp );
16661667 if (promptarg != NULL ) {
1668+ /* We have a prompt, encode it as stdout would */
1669+ char * stdout_encoding_str , * stdout_errors_str ;
16671670 PyObject * stringpo ;
1668- PyObject * stdout_encoding ;
1669- char * stdout_encoding_str ;
16701671 stdout_encoding = PyObject_GetAttrString (fout , "encoding" );
1671- if (stdout_encoding == NULL ) {
1672- Py_DECREF (stdin_encoding );
1673- return NULL ;
1674- }
1672+ stdout_errors = PyObject_GetAttrString (fout , "errors" );
1673+ if (!stdout_encoding || !stdout_errors )
1674+ goto _readline_errors ;
16751675 stdout_encoding_str = _PyUnicode_AsString (stdout_encoding );
1676- if (stdout_encoding_str == NULL ) {
1677- Py_DECREF (stdin_encoding );
1678- Py_DECREF (stdout_encoding );
1679- return NULL ;
1680- }
1676+ stdout_errors_str = _PyUnicode_AsString (stdout_errors );
1677+ if (!stdout_encoding_str || !stdout_errors_str )
1678+ goto _readline_errors ;
16811679 stringpo = PyObject_Str (promptarg );
1682- if (stringpo == NULL ) {
1683- Py_DECREF (stdin_encoding );
1684- Py_DECREF (stdout_encoding );
1685- return NULL ;
1686- }
1680+ if (stringpo == NULL )
1681+ goto _readline_errors ;
16871682 po = PyUnicode_AsEncodedString (stringpo ,
1688- stdout_encoding_str , NULL );
1689- Py_DECREF (stdout_encoding );
1690- Py_DECREF (stringpo );
1691- if (po == NULL ) {
1692- Py_DECREF (stdin_encoding );
1693- return NULL ;
1694- }
1683+ stdout_encoding_str , stdout_errors_str );
1684+ Py_CLEAR (stdout_encoding );
1685+ Py_CLEAR (stdout_errors );
1686+ Py_CLEAR (stringpo );
1687+ if (po == NULL )
1688+ goto _readline_errors ;
16951689 prompt = PyBytes_AsString (po );
1696- if (prompt == NULL ) {
1697- Py_DECREF (stdin_encoding );
1698- Py_DECREF (po );
1699- return NULL ;
1700- }
1690+ if (prompt == NULL )
1691+ goto _readline_errors ;
17011692 }
17021693 else {
17031694 po = NULL ;
17041695 prompt = "" ;
17051696 }
17061697 s = PyOS_Readline (stdin , stdout , prompt );
1707- Py_XDECREF (po );
17081698 if (s == NULL ) {
17091699 if (!PyErr_Occurred ())
17101700 PyErr_SetNone (PyExc_KeyboardInterrupt );
1711- Py_DECREF (stdin_encoding );
1712- return NULL ;
1701+ goto _readline_errors ;
17131702 }
17141703
17151704 len = strlen (s );
@@ -1727,12 +1716,22 @@ builtin_input(PyObject *self, PyObject *args)
17271716 len -- ; /* strip trailing '\n' */
17281717 if (len != 0 && s [len - 1 ] == '\r' )
17291718 len -- ; /* strip trailing '\r' */
1730- result = PyUnicode_Decode (s , len , stdin_encoding_str , NULL );
1719+ result = PyUnicode_Decode (s , len , stdin_encoding_str ,
1720+ stdin_errors_str );
17311721 }
17321722 }
17331723 Py_DECREF (stdin_encoding );
1724+ Py_DECREF (stdin_errors );
1725+ Py_XDECREF (po );
17341726 PyMem_FREE (s );
17351727 return result ;
1728+ _readline_errors :
1729+ Py_XDECREF (stdin_encoding );
1730+ Py_XDECREF (stdout_encoding );
1731+ Py_XDECREF (stdin_errors );
1732+ Py_XDECREF (stdout_errors );
1733+ Py_XDECREF (po );
1734+ return NULL ;
17361735 }
17371736
17381737 /* Fallback if we're not interactive */
0 commit comments