Skip to content

Commit a48db2b

Browse files
committed
Issue python#24268: Address some PEP 489 refleaks
- missing DECREF in PyModule_FromDefAndSpec2 - missing DECREF in PyType_FromSpecAndBases2 - missing DECREF in _testmultiphase module Patch by Petr Viktorin
1 parent d5cacbb commit a48db2b

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

Modules/_testmultiphase.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ createfunc_nonmodule(PyObject *spec, PyModuleDef *def)
262262
return NULL;
263263
}
264264
PyDict_SetItemString(dct, "three", three);
265+
Py_DECREF(three);
265266

266267
ns = _PyNamespace_New(dct);
267268
Py_DECREF(dct);

Objects/moduleobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api
311311
}
312312
}
313313

314+
Py_DECREF(nameobj);
314315
return m;
315316

316317
error:

Objects/typeobject.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,6 +2694,7 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
26942694
{
26952695
PyHeapTypeObject *res = (PyHeapTypeObject*)PyType_GenericAlloc(&PyType_Type, 0);
26962696
PyTypeObject *type, *base;
2697+
PyObject *modname;
26972698
char *s;
26982699
char *res_start = (char*)res;
26992700
PyType_Slot *slot;
@@ -2807,11 +2808,15 @@ PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
28072808

28082809
/* Set type.__module__ */
28092810
s = strrchr(spec->name, '.');
2810-
if (s != NULL)
2811-
_PyDict_SetItemId(type->tp_dict, &PyId___module__,
2812-
PyUnicode_FromStringAndSize(
2813-
spec->name, (Py_ssize_t)(s - spec->name)));
2814-
else {
2811+
if (s != NULL) {
2812+
modname = PyUnicode_FromStringAndSize(
2813+
spec->name, (Py_ssize_t)(s - spec->name));
2814+
if (modname == NULL) {
2815+
goto fail;
2816+
}
2817+
_PyDict_SetItemId(type->tp_dict, &PyId___module__, modname);
2818+
Py_DECREF(modname);
2819+
} else {
28152820
if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
28162821
"builtin type %.200s has no __module__ attribute",
28172822
spec->name))

0 commit comments

Comments
 (0)