Skip to content

Commit e0fa531

Browse files
[3.12] gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044) (GH-106228)
gh-106033: Get rid of PyDict_GetItem in _PyFunction_FromConstructor (GH-106044) (cherry picked from commit 08c08d2) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent e12045d commit e0fa531

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

Objects/funcobject.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,14 @@ PyFunction_ClearWatcher(int watcher_id)
106106
PyFunctionObject *
107107
_PyFunction_FromConstructor(PyFrameConstructor *constr)
108108
{
109+
PyObject *module = Py_XNewRef(PyDict_GetItemWithError(constr->fc_globals, &_Py_ID(__name__)));
110+
if (!module && PyErr_Occurred()) {
111+
return NULL;
112+
}
109113

110114
PyFunctionObject *op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
111115
if (op == NULL) {
116+
Py_XDECREF(module);
112117
return NULL;
113118
}
114119
op->func_globals = Py_NewRef(constr->fc_globals);
@@ -122,10 +127,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
122127
op->func_doc = Py_NewRef(Py_None);
123128
op->func_dict = NULL;
124129
op->func_weakreflist = NULL;
125-
op->func_module = Py_XNewRef(PyDict_GetItem(op->func_globals, &_Py_ID(__name__)));
126-
if (!op->func_module) {
127-
PyErr_Clear();
128-
}
130+
op->func_module = module;
129131
op->func_annotations = NULL;
130132
op->func_typeparams = NULL;
131133
op->vectorcall = _PyFunction_Vectorcall;

0 commit comments

Comments
 (0)