[3.11] gh-105375: Improve error handling in the builtins extension module (GH-105585) (#105650)

(cherry picked from commit d4fa52934a)

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
This commit is contained in:
Miss Islington (bot) 2023-06-11 04:13:45 -07:00 committed by GitHub
parent cb26fafdcc
commit b3d95d4e61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 10 deletions

View File

@ -0,0 +1,2 @@
Fix bugs in the :mod:`builtins` module where exceptions could end up being
overwritten.

View File

@ -2165,17 +2165,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
/* stdin is a text stream, so it must have an encoding. */
stdin_encoding = PyObject_GetAttr(fin, &_Py_ID(encoding));
if (stdin_encoding == NULL) {
tty = 0;
goto _readline_errors;
}
stdin_errors = PyObject_GetAttr(fin, &_Py_ID(errors));
if (!stdin_encoding || !stdin_errors ||
!PyUnicode_Check(stdin_encoding) ||
!PyUnicode_Check(stdin_errors)) {
if (stdin_errors == NULL) {
tty = 0;
goto _readline_errors;
}
if (!PyUnicode_Check(stdin_encoding) ||
!PyUnicode_Check(stdin_errors))
{
tty = 0;
goto _readline_errors;
}
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
if (!stdin_encoding_str || !stdin_errors_str)
if (stdin_encoding_str == NULL) {
goto _readline_errors;
}
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
if (stdin_errors_str == NULL) {
goto _readline_errors;
}
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
if (tmp == NULL)
PyErr_Clear();
@ -2186,17 +2198,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
const char *stdout_encoding_str, *stdout_errors_str;
PyObject *stringpo;
stdout_encoding = PyObject_GetAttr(fout, &_Py_ID(encoding));
if (stdout_encoding == NULL) {
tty = 0;
goto _readline_errors;
}
stdout_errors = PyObject_GetAttr(fout, &_Py_ID(errors));
if (!stdout_encoding || !stdout_errors ||
!PyUnicode_Check(stdout_encoding) ||
!PyUnicode_Check(stdout_errors)) {
if (stdout_errors == NULL) {
tty = 0;
goto _readline_errors;
}
if (!PyUnicode_Check(stdout_encoding) ||
!PyUnicode_Check(stdout_errors))
{
tty = 0;
goto _readline_errors;
}
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
if (!stdout_encoding_str || !stdout_errors_str)
if (stdout_encoding_str == NULL) {
goto _readline_errors;
}
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
if (stdout_errors_str == NULL) {
goto _readline_errors;
}
stringpo = PyObject_Str(prompt);
if (stringpo == NULL)
goto _readline_errors;