Skip to content

PyType_GetModuleByDef can return NULL without exception set #133166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iritkatriel opened this issue Apr 29, 2025 · 4 comments
Closed

PyType_GetModuleByDef can return NULL without exception set #133166

iritkatriel opened this issue Apr 29, 2025 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@iritkatriel
Copy link
Member

iritkatriel commented Apr 29, 2025

The documentation does not mention that this is possible, but if the object passed in is not a heap type it returns NULL without setting an exception:

PyObject *
PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
{
    assert(PyType_Check(type));   

    if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
        // type_ready_mro() ensures that no heap type is
        // contained in a static type MRO.
        return NULL;
    }
   ...
}

CC @ericsnowcurrently @encukou .

Linked PRs

@ZeroIntensity ZeroIntensity added type-bug An unexpected behavior, bug, or error topic-C-API labels Apr 29, 2025
@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Apr 30, 2025
@encukou
Copy link
Member

encukou commented May 1, 2025

Thanks for the catch, and thanks @neonene for the quick fix!

@neonene
Copy link
Contributor

neonene commented May 1, 2025

cpython/Objects/typeobject.c

Lines 5015 to 5020 in 90c786e

PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
{
PyObject *module = get_module_by_def(type, def);
if (module == NULL) {
PyErr_Format(
PyExc_TypeError,

3.13 has no issue. The test for 3.14 cannot be backported.

@encukou
Copy link
Member

encukou commented May 1, 2025

Ah! My mistake

@encukou encukou closed this as completed May 1, 2025
@neonene
Copy link
Contributor

neonene commented May 1, 2025

Thank you for the report and the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants