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
14 changes: 14 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 @@ -228,6 +229,19 @@ class H2(int): pass
with self.assertRaises(TypeError):
_testcapi.pytype_getmodulebytoken(H2, token)

def test_get_module_by_token_in_dealloc(self):
# gh-160840: PyType_GetModuleByToken() 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.GetModuleByTokenInDealloc):
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_GetModuleByToken` and :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 @@ -103,7 +103,7 @@ module_exec(PyObject *mod)
return 0;
}

static struct PyModuleDef _testlimitedcapimodule_def = {
PyModuleDef _Py_TestLimitedCAPI_ModuleDef = {
PyModuleDef_HEAD_INIT,
.m_name = "_testlimitedcapi",
.m_size = 0,
Expand All @@ -119,5 +119,5 @@ static struct PyModuleDef _testlimitedcapimodule_def = {
PyMODINIT_FUNC
PyInit__testlimitedcapi(void)
{
return PyModuleDef_Init(&_testlimitedcapimodule_def);
return PyModuleDef_Init(&_Py_TestLimitedCAPI_ModuleDef);
}
3 changes: 3 additions & 0 deletions Modules/_testlimitedcapi/parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# error "Py_BUILD_CORE macro must not be defined"
#endif

// Definition (and token) 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
42 changes: 41 additions & 1 deletion Modules/_testlimitedcapi/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Do no check PyType_Check() so Python tests can pass arbitrary objects,
// even if it's likely to crash.

// Need limited C API version 3.14 for PyType_Freeze()
// Need limited C API version 3.15 for PyType_Freeze()
#include "pyconfig.h" // Py_GIL_DISABLED
#if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API)
# define Py_LIMITED_API 0x030e0000
Expand Down Expand Up @@ -160,6 +160,40 @@ type_issubtype(PyObject *module, PyObject *args)
}


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

static void
getmodulebytoken_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 getmodulebytoken_dealloc_spec = {
.name = "_testlimitedcapi.GetModuleByTokenInDealloc",
.flags = Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.slots = (PyType_Slot[]) {
{Py_tp_traverse, getmodulebytoken_dealloc_traverse},
{Py_tp_dealloc, getmodulebytoken_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, &getmodulebytoken_dealloc_spec, NULL);
if (PyModule_Add(m, "GetModuleByTokenInDealloc", type) < 0) {
return -1;
}

#define ADD_INT(macro) \
do { \
if (PyModule_AddIntConstant(m, #macro, macro) < 0) { \
Expand Down
68 changes: 48 additions & 20 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5906,6 +5906,29 @@ PyType_GetModuleState(PyTypeObject *type)
return _PyModule_GetState(m);
}

static PyObject *
get_module_by_token_recursive(PyObject *bases, const void *token)
{
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_GetToken(module) == token) {
return module;
break;
}
module = get_module_by_token_recursive(lookup_tp_bases(base), token);
if (module != NULL) {
return module;
}
}
return NULL;
}

/* Return borrowed ref to the module of the first superclass where the module
* has the given token.
*/
Expand All @@ -5931,28 +5954,33 @@ PyType_GetModuleByToken_DuringGC(PyTypeObject *type, const void *token)
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_token_recursive(lookup_tp_bases(type), token);
}
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_GetToken(module) == token) {
res = module;
break;
PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
PyObject *module = ht->ht_module;
if (module && _PyModule_GetToken(module) == token) {
res = module;
break;
}
}
}
END_TYPE_LOCK();
Expand Down
Loading