From 857ec727a6ad417452029f658daf0bbd34d5b783 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 23 Jun 2023 20:38:48 +0300 Subject: [PATCH 1/2] gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor --- Objects/funcobject.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 753038600aa858..9496d55fdfa447 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -106,6 +106,10 @@ 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) { @@ -122,10 +126,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; From ffc68fc01ccd2a6110e4e03f2a29306a6ee3ee05 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 29 Jun 2023 11:04:33 +0300 Subject: [PATCH 2/2] Address review comment. --- Objects/funcobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/funcobject.c b/Objects/funcobject.c index 9496d55fdfa447..f43e3a2787b846 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -113,6 +113,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr) PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type); if (op == NULL) { + Py_XDECREF(module); return NULL; } op->func_globals = Py_NewRef(constr->fc_globals);