]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044)
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 29 Jun 2023 09:31:08 +0000 (12:31 +0300)
committerGitHub <noreply@github.com>
Thu, 29 Jun 2023 09:31:08 +0000 (12:31 +0300)
Objects/funcobject.c

index 753038600aa858d55294dcb986130809c60323d8..f43e3a2787b846c7dfe2007d0622035bd71bc327 100644 (file)
@@ -106,9 +106,14 @@ PyFunction_ClearWatcher(int watcher_id)
 PyFunctionObject *
 _PyFunction_FromConstructor(PyFrameConstructor *constr)
 {
+    PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
+    if (!module && PyErr_Occurred()) {
+        return NULL;
+    }
 
     PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
     if (op == NULL) {
+        Py_XDECREF(module);
         return NULL;
     }
     op->func_globals = Py_NewRef(constr->fc_globals);
@@ -122,10 +127,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
     op->func_doc = Py_NewRef(Py_None);
     op->func_dict = NULL;
     op->func_weakreflist = NULL;
-    op->func_module = Py_XNewRef(PyDict_GetItem(op->func_globals, &_Py_ID(__name__)));
-    if (!op->func_module) {
-        PyErr_Clear();
-    }
+    op->func_module = module;
     op->func_annotations = NULL;
     op->func_typeparams = NULL;
     op->vectorcall = _PyFunction_Vectorcall;