Skip to content

Commit 122a1a2

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

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
@@ -2164,17 +2164,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
21642164

21652165
/* stdin is a text stream, so it must have an encoding. */
21662166
stdin_encoding = PyObject_GetAttr(fin, &_Py_ID(encoding));
2167+
if (stdin_encoding == NULL) {
2168+
tty = 0;
2169+
goto _readline_errors;
2170+
}
21672171
stdin_errors = PyObject_GetAttr(fin, &_Py_ID(errors));
2168-
if (!stdin_encoding || !stdin_errors ||
2169-
!PyUnicode_Check(stdin_encoding) ||
2170-
!PyUnicode_Check(stdin_errors)) {
2172+
if (stdin_errors == NULL) {
2173+
tty = 0;
2174+
goto _readline_errors;
2175+
}
2176+
if (!PyUnicode_Check(stdin_encoding) ||
2177+
!PyUnicode_Check(stdin_errors))
2178+
{
21712179
tty = 0;
21722180
goto _readline_errors;
21732181
}
21742182
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2183+
if (stdin_encoding_str == NULL) {
2184+
goto _readline_errors;
2185+
}
21752186
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
2176-
if (!stdin_encoding_str || !stdin_errors_str)
2187+
if (stdin_errors_str == NULL) {
21772188
goto _readline_errors;
2189+
}
21782190
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
21792191
if (tmp == NULL)
21802192
PyErr_Clear();
@@ -2185,17 +2197,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
21852197
const char *stdout_encoding_str, *stdout_errors_str;
21862198
PyObject *stringpo;
21872199
stdout_encoding = PyObject_GetAttr(fout, &_Py_ID(encoding));
2200+
if (stdout_encoding == NULL) {
2201+
tty = 0;
2202+
goto _readline_errors;
2203+
}
21882204
stdout_errors = PyObject_GetAttr(fout, &_Py_ID(errors));
2189-
if (!stdout_encoding || !stdout_errors ||
2190-
!PyUnicode_Check(stdout_encoding) ||
2191-
!PyUnicode_Check(stdout_errors)) {
2205+
if (stdout_errors == NULL) {
2206+
tty = 0;
2207+
goto _readline_errors;
2208+
}
2209+
if (!PyUnicode_Check(stdout_encoding) ||
2210+
!PyUnicode_Check(stdout_errors))
2211+
{
21922212
tty = 0;
21932213
goto _readline_errors;
21942214
}
21952215
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2216+
if (stdout_encoding_str == NULL) {
2217+
goto _readline_errors;
2218+
}
21962219
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
2197-
if (!stdout_encoding_str || !stdout_errors_str)
2220+
if (stdout_errors_str == NULL) {
21982221
goto _readline_errors;
2222+
}
21992223
stringpo = PyObject_Str(prompt);
22002224
if (stringpo == NULL)
22012225
goto _readline_errors;

0 commit comments

Comments
 (0)