Skip to content

Commit 34f96b8

Browse files
committed
Issue #18520: Fix PyFunction_NewWithQualName() error handling
1 parent 4d1f5d6 commit 34f96b8

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

Objects/funcobject.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
1212
PyObject *doc, *consts, *module;
1313
static PyObject *__name__ = NULL;
1414

15+
if (__name__ == NULL) {
16+
__name__ = PyUnicode_InternFromString("__name__");
17+
if (__name__ == NULL)
18+
return NULL;
19+
}
20+
1521
op = PyObject_GC_New(PyFunctionObject, &PyFunction_Type);
1622
if (op == NULL)
1723
return NULL;
@@ -26,6 +32,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
2632
op->func_defaults = NULL; /* No default arguments */
2733
op->func_kwdefaults = NULL; /* No keyword only defaults */
2834
op->func_closure = NULL;
35+
2936
consts = ((PyCodeObject *)code)->co_consts;
3037
if (PyTuple_Size(consts) >= 1) {
3138
doc = PyTuple_GetItem(consts, 0);
@@ -36,21 +43,13 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
3643
doc = Py_None;
3744
Py_INCREF(doc);
3845
op->func_doc = doc;
46+
3947
op->func_dict = NULL;
4048
op->func_module = NULL;
4149
op->func_annotations = NULL;
4250

4351
/* __module__: If module name is in globals, use it.
44-
Otherwise, use None.
45-
*/
46-
if (!__name__) {
47-
__name__ = PyUnicode_InternFromString("__name__");
48-
if (!__name__) {
49-
Py_DECREF(op);
50-
return NULL;
51-
}
52-
}
53-
52+
Otherwise, use None. */
5453
module = PyDict_GetItem(globals, __name__);
5554
if (module) {
5655
Py_INCREF(module);

0 commit comments

Comments
 (0)