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

erlend-aasland webhook-mailer at python.org
Sun Jun 11 07:13:53 EDT 2023


https://github.com/python/cpython/commit/b3d95d4e612ffa841d83332c0bc3e82e9f456c25
commit: b3d95d4e612ffa841d83332c0bc3e82e9f456c25
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-06-11T11:13:45Z
summary:

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

(cherry picked from commit d4fa52934a282df51cff800eee5caeb94a229547)

Co-authored-by: Erlend E. Aasland <erlend.aasland at protonmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2023-06-08-10-10-07.gh-issue-105375.35VGDd.rst
M Python/bltinmodule.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-06-08-10-10-07.gh-issue-105375.35VGDd.rst b/Misc/NEWS.d/next/Core and Builtins/2023-06-08-10-10-07.gh-issue-105375.35VGDd.rst
new file mode 100644
index 0000000000000..3ab85538f3fc4
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-06-08-10-10-07.gh-issue-105375.35VGDd.rst	
@@ -0,0 +1,2 @@
+Fix bugs in the :mod:`builtins` module where exceptions could end up being
+overwritten.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index e20bd5396bf56..f30040507bcb3 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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);
+        if (stdin_encoding_str == NULL) {
+            goto _readline_errors;
+        }
         stdin_errors_str = PyUnicode_AsUTF8(stdin_errors);
-        if (!stdin_encoding_str || !stdin_errors_str)
+        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);
+            if (stdout_encoding_str == NULL) {
+                goto _readline_errors;
+            }
             stdout_errors_str = PyUnicode_AsUTF8(stdout_errors);
-            if (!stdout_encoding_str || !stdout_errors_str)
+            if (stdout_errors_str == NULL) {
                 goto _readline_errors;
+            }
             stringpo = PyObject_Str(prompt);
             if (stringpo == NULL)
                 goto _readline_errors;



More information about the Python-checkins mailing list