[Python-checkins] [3.11] gh-105375: Improve errnomodule error handling (#105590) (#105595)

erlend-aasland webhook-mailer at python.org
Fri Jun 9 16:35:37 EDT 2023


https://github.com/python/cpython/commit/76682ba7aff2d18eec34b8433c68da028260bc51
commit: 76682ba7aff2d18eec34b8433c68da028260bc51
branch: 3.11
author: Erlend E. Aasland <erlend.aasland at protonmail.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-06-09T20:35:30Z
summary:

[3.11] gh-105375: Improve errnomodule error handling (#105590) (#105595)

(cherry picked from commit eede1d2f48b4fe7f7918952d9ebeb744b58668c1)

Bail immediately if an exception is set, to prevent exceptions from
being overwritten.

files:
A Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst
M Modules/errnomodule.c

diff --git a/Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst b/Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst
new file mode 100644
index 000000000000..3030477c8245
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-06-09-21-04-39.gh-issue-105375.bTcqS9.rst
@@ -0,0 +1 @@
+Fix bugs in :mod:`pickle` where exceptions could be overwritten.
diff --git a/Modules/errnomodule.c b/Modules/errnomodule.c
index 4de4144520aa..a63ac721315a 100644
--- a/Modules/errnomodule.c
+++ b/Modules/errnomodule.c
@@ -79,9 +79,12 @@ _add_errcode(PyObject *module_dict, PyObject *error_dict, const char *name_str,
 static int
 errno_exec(PyObject *module)
 {
-    PyObject *module_dict = PyModule_GetDict(module);
+    PyObject *module_dict = PyModule_GetDict(module);  // Borrowed ref.
+    if (module_dict == NULL) {
+        return -1;
+    }
     PyObject *error_dict = PyDict_New();
-    if (!module_dict || !error_dict) {
+    if (error_dict == NULL) {
         return -1;
     }
     if (PyDict_SetItemString(module_dict, "errorcode", error_dict) < 0) {



More information about the Python-checkins mailing list