Skip to content

Commit d4fa529

Browse files
gh-105375: Improve error handling in the builtins extension module (#105585)
1 parent c932f72 commit d4fa529

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

21632163
/* stdin is a text stream, so it must have an encoding. */
21642164
stdin_encoding = PyObject_GetAttr(fin, &_Py_ID(encoding));
2165+
if (stdin_encoding == NULL) {
2166+
tty = 0;
2167+
goto _readline_errors;
2168+
}
21652169
stdin_errors = PyObject_GetAttr(fin, &_Py_ID(errors));
2166-
if (!stdin_encoding || !stdin_errors ||
2167-
!PyUnicode_Check(stdin_encoding) ||
2168-
!PyUnicode_Check(stdin_errors)) {
2170+
if (stdin_errors == NULL) {
2171+
tty = 0;
2172+
goto _readline_errors;
2173+
}
2174+
if (!PyUnicode_Check(stdin_encoding) ||
2175+
!PyUnicode_Check(stdin_errors))
2176+
{
21692177
tty = 0;
21702178
goto _readline_errors;
21712179
}
21722180
stdin_encoding_str = PyUnicode_AsUTF8(stdin_encoding);
2181+
if (stdin_encoding_str == NULL) {
2182+
goto _readline_errors;
2183+
}
21732184
stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
2174-
if (!stdin_encoding_str || !stdin_errors_str)
2185+
if (stdin_errors_str == NULL) {
21752186
goto _readline_errors;
2187+
}
21762188
tmp = PyObject_CallMethodNoArgs(fout, &_Py_ID(flush));
21772189
if (tmp == NULL)
21782190
PyErr_Clear();
@@ -2183,17 +2195,29 @@ builtin_input_impl(PyObject *module, PyObject *prompt)
21832195
const char *stdout_encoding_str, *stdout_errors_str;
21842196
PyObject *stringpo;
21852197
stdout_encoding = PyObject_GetAttr(fout, &_Py_ID(encoding));
2198+
if (stdout_encoding == NULL) {
2199+
tty = 0;
2200+
goto _readline_errors;
2201+
}
21862202
stdout_errors = PyObject_GetAttr(fout, &_Py_ID(errors));
2187-
if (!stdout_encoding || !stdout_errors ||
2188-
!PyUnicode_Check(stdout_encoding) ||
2189-
!PyUnicode_Check(stdout_errors)) {
2203+
if (stdout_errors == NULL) {
2204+
tty = 0;
2205+
goto _readline_errors;
2206+
}
2207+
if (!PyUnicode_Check(stdout_encoding) ||
2208+
!PyUnicode_Check(stdout_errors))
2209+
{
21902210
tty = 0;
21912211
goto _readline_errors;
21922212
}
21932213
stdout_encoding_str = PyUnicode_AsUTF8(stdout_encoding);
2214+
if (stdout_encoding_str == NULL) {
2215+
goto _readline_errors;
2216+
}
21942217
stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
2195-
if (!stdout_encoding_str || !stdout_errors_str)
2218+
if (stdout_errors_str == NULL) {
21962219
goto _readline_errors;
2220+
}
21972221
stringpo = PyObject_Str(prompt);
21982222
if (stringpo == NULL)
21992223
goto _readline_errors;

0 commit comments

Comments
 (0)