Skip to content

PyType_GetModuleByDef can return NULL without exception set #133166

Closed

Description

@iritkatriel

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

Activity

  1. added a commit that references this issue on May 1, 2025
  2. encukou commented on May 1, 2025

    @encukou
    Member

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

  3. neonene commented on May 1, 2025

    @neonene
    Contributor

    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.

  4. encukou commented on May 1, 2025

    @encukou
    Member

    Ah! My mistake

  5. neonene commented on May 1, 2025

    @neonene
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions