Skip to content

[3.14] gh-160840: In PyType_GetModuleByDef, fall back to tp_bases if tp_mro is gone (GH-162160) - #162874

Open
encukou wants to merge 2 commits into
python:3.14from
encukou:backport-4499a94-3.14
Open

encukou wants to merge 2 commits into
python:3.14from
encukou:backport-4499a94-3.14

Conversation

@encukou

@encukou encukou commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

PyType_GetModuleByDef asserted that a type's mro is not NULL, but this can happen when cleaning a GC cycle. If it does, we can still fall back to walking tp_bases.

(cherry picked from commit 4499a94. This backport changes only PyType_GetModuleByDef, while in 3.15+, *ByDef is implemented on top of the new PyType_GetModuleByToken, which the original commit changes too.)

…p_mro is gone (pythonGH-162160)

`PyType_GetModuleByDef` asserted that a type's `mro` is not NULL,
but this can happen when cleaning a GC cycle. If it does, we can
still fall back to walking `tp_bases`.

(cherry picked from commit 4499a94.
This backport changes only `PyType_GetModuleByDef`, while in 3.15+,
`*ByDef` is implemented on top of the new `PyType_GetModuleByToken`,
which the original commit changes too.)
@encukou
encukou requested a review from markshannon as a code owner November 30, 2026 15:49
@encukou encukou changed the title gh-160840: In PyType_GetModuleByDef, fall back to tp_bases if tp_mro is gone (GH-162160) [3.14] gh-160840: In PyType_GetModuleByDef, fall back to tp_bases if tp_mro is gone (GH-162160) Nov 30, 2026
@encukou

encukou commented Nov 30, 2026

Copy link
Copy Markdown
Member Author

@da-woods The backport is not entirely trivial so I'd appreciate a quick check from a different pair of eyes.

`PyModuleDef` is a typedef for `struct PyModuleDef`, but only one
is recognized by the tool.

@da-woods da-woods left a comment

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.

Looks good to me

Comment thread Objects/typeobject.c
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants