Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_capi/test_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from test import support
from test.support import import_helper, Py_GIL_DISABLED, refleak_helper
import unittest

Expand Down Expand Up @@ -210,6 +211,20 @@ class H2(int): pass
with self.assertRaises(TypeError):
_testcapi.pytype_getmodulebydef(H2)


def test_get_module_by_def_in_dealloc(self):
# gh-160840: PyType_GetModuleByDef() called from tp_dealloc
# of a subclass instance that's in a reference cycle with its class
# (and with itself, so it outlives the class's tp_clear)
class C(_testlimitedcapi.GetModuleByDefInDealloc):
pass
C.inst = C()
C.inst.self = C.inst
with support.catch_unraisable_exception() as cm:
del C
support.gc_collect()
self.assertIsNone(cm.unraisable)

def test_freeze(self):
# test PyType_Freeze()
type_freeze = _testlimitedcapi.type_freeze
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
When a type's internal :term:`method resolution order` data is cleared as
the type is being torn down during garbage collection,
:c:func:`PyType_GetModuleByDef` can
now still retrieve its module.
4 changes: 2 additions & 2 deletions Modules/_testlimitedcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static PyMethodDef TestMethods[] = {
{NULL, NULL} /* sentinel */
};

static struct PyModuleDef _testlimitedcapimodule = {
PyModuleDef _Py_TestLimitedCAPI_ModuleDef = {
PyModuleDef_HEAD_INIT,
.m_name = "_testlimitedcapi",
.m_size = 0,
Expand All @@ -21,7 +21,7 @@ static struct PyModuleDef _testlimitedcapimodule = {
PyMODINIT_FUNC
PyInit__testlimitedcapi(void)
{
PyObject *mod = PyModule_Create(&_testlimitedcapimodule);
PyObject *mod = PyModule_Create(&_Py_TestLimitedCAPI_ModuleDef);
if (mod == NULL) {
return NULL;
}
Expand Down
3 changes: 3 additions & 0 deletions Modules/_testlimitedcapi/parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
# error "Py_BUILD_CORE macro must not be defined"
#endif

// Definition of the _testlimitedcapi module
extern PyModuleDef _Py_TestLimitedCAPI_ModuleDef;

int _PyTestLimitedCAPI_Init_Abstract(PyObject *module);
int _PyTestLimitedCAPI_Init_ByteArray(PyObject *module);
int _PyTestLimitedCAPI_Init_Bytes(PyObject *module);
Expand Down
40 changes: 40 additions & 0 deletions Modules/_testlimitedcapi/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ type_issubtype(PyObject *module, PyObject *args)
}


// Type whose tp_dealloc calls PyType_GetModuleByDef() (gh-160840)
static int
getmodulebydef_dealloc_traverse(PyObject *op, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(op));
return 0;
}

static void
getmodulebydef_dealloc_dealloc(PyObject *op)
{
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);

PyObject *mod = PyType_GetModuleByDef(tp, &_Py_TestLimitedCAPI_ModuleDef);
if (mod == NULL) {
PyErr_WriteUnraisable(NULL);
}

freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free);
tp_free(op);
Py_DECREF(tp);
}

static PyType_Spec getmodulebydef_dealloc_spec = {
.name = "_testlimitedcapi.GetModuleByDefInDealloc",
.flags = Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.slots = (PyType_Slot[]) {
{Py_tp_traverse, getmodulebydef_dealloc_traverse},
{Py_tp_dealloc, getmodulebydef_dealloc_dealloc},
{0},
},
};

static PyMethodDef test_methods[] = {
{"get_heaptype_for_name", get_heaptype_for_name, METH_NOARGS},
{"get_type_name", get_type_name, METH_O},
Expand All @@ -182,6 +216,12 @@ _PyTestLimitedCAPI_Init_Type(PyObject *m)
return -1;
}

PyObject *type = PyType_FromModuleAndSpec(
m, &getmodulebydef_dealloc_spec, NULL);
if (PyModule_Add(m, "GetModuleByDefInDealloc", type) < 0) {
return -1;
}

#define ADD_INT(macro) \
do { \
if (PyModule_AddIntConstant(m, #macro, macro) < 0) { \
Expand Down
71 changes: 49 additions & 22 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5768,9 +5768,31 @@ PyType_GetModuleState(PyTypeObject *type)
return _PyModule_GetState(m);
}

static PyObject *
get_module_by_def_recursive(PyObject *bases, PyModuleDef *def)
{
assert(bases != NULL);
Py_ssize_t n = PyTuple_GET_SIZE(bases);
for (Py_ssize_t i = 0; i < n; i++) {
PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i));
if (!_PyType_HasFeature(base, Py_TPFLAGS_HEAPTYPE)) {
continue;
}
PyObject *module = ((PyHeapTypeObject*)base)->ht_module;
if (module && _PyModule_GetDef(module) == def) {
return module;
break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the most useful break ever. But it isn't doing any harm and it's in the original

}
module = get_module_by_def_recursive(lookup_tp_bases(base), def);
if (module != NULL) {
return module;
}
}
return NULL;
}

/* Get the module of the first superclass where the module has the
* given PyModuleDef.
/* Return borrowed ref to the module of the first superclass where the module
* has the given PyModuleDef.
*/
PyObject *
PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
Expand All @@ -5794,28 +5816,33 @@ PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
BEGIN_TYPE_LOCK();

PyObject *mro = lookup_tp_mro(type);
// The type must be ready
assert(mro != NULL);
assert(PyTuple_Check(mro));
// mro_invoke() ensures that the type MRO cannot be empty.
assert(PyTuple_GET_SIZE(mro) >= 1);
// Also, the first item in the MRO is the type itself, which
// we already checked above. We skip it in the loop.
assert(PyTuple_GET_ITEM(mro, 0) == (PyObject *)type);
if (mro == NULL) {
// If the MRO is already cleared during garbage collection, fall
// back to walking tp_bases.
res = get_module_by_def_recursive(lookup_tp_bases(type), def);
}
else {
assert(PyTuple_Check(mro));
// mro_invoke() ensures that the type MRO cannot be empty.
assert(PyTuple_GET_SIZE(mro) >= 1);
// Also, the first item in the MRO is the type itself, which
// we already checked above. We skip it in the loop.
assert(PyTuple_GET_ITEM(mro, 0) == (PyObject *)type);

Py_ssize_t n = PyTuple_GET_SIZE(mro);
for (Py_ssize_t i = 1; i < n; i++) {
PyObject *super = PyTuple_GET_ITEM(mro, i);
if (!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
// Static types in the MRO need to be skipped
continue;
}
Py_ssize_t n = PyTuple_GET_SIZE(mro);
for (Py_ssize_t i = 1; i < n; i++) {
PyObject *super = PyTuple_GET_ITEM(mro, i);
if (!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
// Static types in the MRO need to be skipped
continue;
}

PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
PyObject *module = ht->ht_module;
if (module && _PyModule_GetDef(module) == def) {
res = module;
break;
PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
PyObject *module = ht->ht_module;
if (module && _PyModule_GetDef(module) == def) {
res = module;
break;
}
}
}
END_TYPE_LOCK();
Expand Down
Loading