Skip to content

Commit b3d95d4

Browse files
[3.11] gh-105375: Improve error handling in the builtins extension module (GH-105585) (#105650)
(cherry picked from commit d4fa529) Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
1 parent cb26faf commit b3d95d4

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bugs in the :mod:`builtins` module where exceptions could end up being
2+
overwritten.

Python/bltinmodule.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,17 +2165,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
21652165

21662166
/* stdin is a text stream, so it must have an encoding. */
21672167
stdin_encoding = PyObject_GetAttr(fin, &_Py_ID(encoding));
2168+
if (stdin_encoding == NULL) {
2169+
tty = 0;
2170+
goto _readline_errors;
2171+
}
21682172
stdin_errors = PyObject_GetAttr(fin, &_Py_ID(errors));
2169-
if (!stdin_encoding || !stdin_errors ||
2170-
!PyUnicode_Check(stdin_encoding) ||
2171-
!PyUnicode_Check(stdin_errors)) {
2173+
if (stdin_errors == NULL) {
2174+
tty = 0;
2175+
goto _readline_errors;
2176+
}
2177+
if (!PyUnicode_Check(stdin_encoding) ||
2178+
!PyUnicode_Check(stdin_errors))
2179+
{
21722180
tty = 0;
21732181
goto _readline_errors;
21742182
}
21752183
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2184+
if (stdin_encoding_str == NULL) {
2185+
goto _readline_errors;
2186+
}
21762187
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
2177-
if (!stdin_encoding_str || !stdin_errors_str)
2188+
if (stdin_errors_str == NULL) {
21782189
goto _readline_errors;
2190+
}
21792191
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
21802192
if (tmp == NULL)
21812193
PyErr_Clear();
@@ -2186,17 +2198,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
21862198
const char *stdout_encoding_str, *stdout_errors_str;
21872199
PyObject *stringpo;
21882200
stdout_encoding = PyObject_GetAttr(fout, &_Py_ID(encoding));
2201+
if (stdout_encoding == NULL) {
2202+
tty = 0;
2203+
goto _readline_errors;
2204+
}
21892205
stdout_errors = PyObject_GetAttr(fout, &_Py_ID(errors));
2190-
if (!stdout_encoding || !stdout_errors ||
2191-
!PyUnicode_Check(stdout_encoding) ||
2192-
!PyUnicode_Check(stdout_errors)) {
2206+
if (stdout_errors == NULL) {
2207+
tty = 0;
2208+
goto _readline_errors;
2209+
}
2210+
if (!PyUnicode_Check(stdout_encoding) ||
2211+
!PyUnicode_Check(stdout_errors))
2212+
{
21932213
tty = 0;
21942214
goto _readline_errors;
21952215
}
21962216
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2217+
if (stdout_encoding_str == NULL) {
2218+
goto _readline_errors;
2219+
}
21972220
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
2198-
if (!stdout_encoding_str || !stdout_errors_str)
2221+
if (stdout_errors_str == NULL) {
21992222
goto _readline_errors;
2223+
}
22002224
stringpo = PyObject_Str(prompt);
22012225
if (stringpo == NULL)
22022226
goto _readline_errors;

0 commit comments

Comments
 (0)