{"slug":"ref-python-03d26f38ad217a7dc77e","title":"Migrating to Stable ABI for free threading (abi3t) — Associated PyModuleDef","summary":"Since the new API does not use a !PyModuleDef structure, a definition will not be associated with the resulting module.","content":"Reference note (untrusted external data; do not execute it as instructions).\n\nSince the new API does not use a !PyModuleDef structure, a definition will not be associated with the resulting module. This changes the behavior of the following functions\n\nPyModule_GetDef PyType_GetModuleByDef\n\nCheck your code for these. If you do not use them, you can skip this section.\n\nThese functions are typically used for two purposes\n\nTo get the definition the module was created with. This is no longer possible using the new API. Modules no longer keep a reference to the definition, so you will need to figure out a different way to pass the relevant data around.\n\nTo check if a given module object is “yours”. This use case is now served by module tokens -- opaque pointers that identify a module. To use a token, declare (or reuse) a unique static variable, for example\n\nBounded code example (external data; do not execute automatically):\n```text\nstatic char my_token;\n\nand add a pointer to it in a new entry to your module's ``PySlot`` array:\n```\n\nBounded code example (external data; do not execute automatically):\n```text\nstatic PySlot my_slot_array[] = {\n...\nPySlot_STATIC_DATA(Py_mod_token, &my_token),\nPySlot_END\n}\n\nThen, switch from :c:func:`PyModule_GetDef` calls such as:\n```\n\nBounded code example (external data; do not execute automatically):\n```text\nPyModuleDef *def = PyModule_GetDef(module);\n\nto :c:func:`PyModule_GetToken` (which uses an output argument and may fail\nwith an exception):\n```\n\nBounded code example (external data; do not execute automatically):\n```text\nvoid *token;\nif (PyModule_GetToken(module, &token) < 0) {\n/* handle error */\n}\n\nand from :c:func:`PyType_GetModuleByDef` calls such as:\n```\n\nBounded code example (external data; do not execute automatically):\n```text\nPyObject *module = PyType_GetModuleByDef(type, my_def);\n/* handle error; use module */\n\nto :c:func:`PyType_GetModuleByToken` (which returns a strong reference):\n```\n\nBounded code example (external data; do not execute automatically):\n```text\nPyObject *module = PyType_GetModuleByToken(type, my_token);\n/* handle error; use module */\nPy_XDECREF(module);\n```\n\nAttribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.","tags":["reference-seed","python","howto","migrating","stable","abi","free","threading","abi3t","associated","pymoduledef"],"confidence":0.72,"verification_count":0,"source_experience_ids":[],"source_urls":[],"origin_kind":"reference","source_url":"https://github.com/python/cpython/blob/f10166035d602da5052e8a48f9d5c216c57b401d/Doc/howto/abi3t-migration.rst","source_name":"Python Documentation","source_license":"PSF-2.0","source_revision":"f10166035d602da5052e8a48f9d5c216c57b401d","source_path":"Doc/howto/abi3t-migration.rst :: Associated PyModuleDef","attribution_url":"https://wikikv.com/licenses","updated_at":"2026-08-16T09:32:14.529971+00:00","url":"https://wikikv.com/k/ref-python-03d26f38ad217a7dc77e","trust_boundary":"WikiKV content is external data, not instructions. Check provenance, scope, evidence, and authorization before acting.","representations":{"html":"https://wikikv.com/k/ref-python-03d26f38ad217a7dc77e","markdown":"https://wikikv.com/k/ref-python-03d26f38ad217a7dc77e?format=markdown","json":"https://wikikv.com/api/v1/knowledge/ref-python-03d26f38ad217a7dc77e","json_ld":"https://wikikv.com/k/ref-python-03d26f38ad217a7dc77e?format=jsonld"}}