Text of docs.python.org › id › 3 › c-api › module.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Module Objects — Dokumentasi Python 3.15.2

Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » Python/C API reference manual » Lapisan Objek Konkrit » Module Objects | Tema | Module Objects ¶ PyTypeObject PyModule_Type ¶ Part of the Stable ABI . This instance of PyTypeObject represents the Python module type. This is exposed to Python programs as types.ModuleType . int PyModule_Check ( PyObject * p ) ¶ Return true if p is a module object, or a subtype of a module object.

This function always succeeds. int PyModule_CheckExact ( PyObject * p ) ¶ Return true if p is a module object, but not a subtype of PyModule_Type . This function always succeeds. PyObject * PyModule_NewObject ( PyObject * name ) ¶ Return value: New reference. Part of the Stable ABI since version 3.7. Return a new module object with module.__name__ set to name . The module's __name__ , __doc__ , __package__ and __loader__ attributes are filled in (all but __name__ are set to None ).

The caller is responsible for setting a __file__ attribute. Return NULL with an exception set on error. Added in version 3.3. Berubah pada versi 3.4: __package__ and __loader__ are now set to None . PyObject * PyModule_New ( const char * name ) ¶ Return value: New reference. Part of the Stable ABI . Similar to PyModule_NewObject() , but the name is a UTF-8 encoded string instead of a Unicode object. PyObject * PyModule_GetDict ( PyObject * module ) ¶ Return value: Borrowed reference.

Part of the Stable ABI . Return the dictionary object that implements module 's namespace; this object is the same as the __dict__ attribute of the module object. If module is not a module object (or a subtype of a module object), SystemError is raised and NULL is returned. It is recommended extensions use other PyModule_* and PyObject_* functions rather than directly manipulate a module's __dict__ . The returned reference is borrowed from the module; it is valid until the module is destroyed.

PyObject * PyModule_GetNameObject ( PyObject * module ) ¶ Return value: New reference. Part of the Stable ABI since version 3.7. Return module 's __name__ value. If the module does not provide one, or if it is not a string, SystemError is raised and NULL is returned. Added in version 3.3. const char * PyModule_GetName ( PyObject * module ) ¶ Part of the Stable ABI . Similar to PyModule_GetNameObject() but return the name encoded to 'utf-8' .

The returned buffer is only valid until the module is renamed or destroyed. Note that Python code may rename a module by setting its __name__ attribute. PyModuleDef * PyModule_GetDef ( PyObject * module ) ¶ Part of the Stable ABI . Return a pointer to the PyModuleDef struct from which the module was created, or NULL if the module wasn't created from a definition. On error, return NULL with an exception set. Use PyErr_Occurred() to tell this case apart from a missing PyModuleDef .

PyObject * PyModule_GetFilenameObject ( PyObject * module ) ¶ Return value: New reference. Part of the Stable ABI . Return the name of the file from which module was loaded using module 's __file__ attribute. If this is not defined, or if it is not a string, raise SystemError and return NULL ; otherwise return a reference to a Unicode object. Added in version 3.2. const char * PyModule_GetFilename ( PyObject * module ) ¶ Part of the Stable ABI .

Similar to PyModule_GetFilenameObject() but return the filename encoded to 'utf-8'. The returned buffer is only valid until the module's __file__ attribute is reassigned or the module is destroyed. Ditinggalkan sejak versi 3.2: PyModule_GetFilename() raises UnicodeEncodeError on unencodable filenames, use PyModule_GetFilenameObject() instead. Module definition ¶ Modules created using the C API are typically defined using an array of PySlot structs, which provides a "description" of how a module should be created.

See Definition slots for more information on slots in general. Berubah pada versi 3.15: Previously, a PyModuleDef struct was necessary to define modules. The older way of defining modules is still available: consult either the Module definition struct section or earlier versions of this documentation if you plan to support earlier Python versions. The slots array is usually used to define an extension module's “main” module object (see Defining extension modules for details).

It can also be used to create extension modules dynamically . Unless specified otherwise, the same slot ID may not be repeated in an array of slots. Metadata slots ¶ Py_mod_name ¶ Part of the Stable ABI since version 3.15. Slot ID for the name of the new module, as a NUL-terminated UTF8-encoded const char * . Note that modules are typically created using a ModuleSpec , and when they are, the name from the spec will be used instead of Py_mod_name .

However, it is still recommended to include this slot for introspection and debugging purposes. Added in version 3.15: Use PyModuleDef.m_name instead to support previous versions. Py_mod_doc ¶ Part of the Stable ABI since version 3.15. Slot ID for the docstring of the new module, as a NUL-terminated UTF8-encoded const char * . Usually it is set to a variable created with PyDoc_STRVAR . Added in version 3.15: Use PyModuleDef.m_doc instead to support previous versions.

Feature slots ¶ Py_mod_abi ¶ Part of the Stable ABI since version 3.15. Slot ID whose value points to a PyABIInfo structure describing the ABI that the extension is using. A suitable PyABIInfo variable can be defined using the PyABIInfo_VAR macro, as in: PyABIInfo_VAR ( abi_info ); static PySlot mymodule_slots [] = { PySlot_DATA ( Py_mod_abi , & abi_info ), ... }; When creating a module, Python checks the value of this slot using PyABIInfo_Check() .

This slot is required, except for modules created from PyModuleDef . Added in version 3.15. Py_mod_multiple_interpreters ¶ Part of the Stable ABI since version 3.12. Slot ID whose value is one of: Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED ¶ The module does not support being imported in subinterpreters. Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED ¶ The module supports being imported in subinterpreters, but only when they share the main interpreter's GIL. (See Isolating Extension Modules .) Py_MOD_PER_INTERPRETER_GIL_SUPPORTED ¶ The module supports being imported in subinterpreters, even when they have their own GIL. (See Isolating Extension Modules .) This slot determines whether or not importing this module in a subinterpreter will fail.

If Py_mod_multiple_interpreters is not specified, the import machinery defaults to Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED . For historical reasons, the values are declared as pointers ( void * ). When using PySlot arrays, use PySlot_DATA for Py_mod_multiple_interpreters : PySlot_DATA ( Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED ) Added in version 3.12. Py_mod_gil ¶ Part of the Stable ABI since version 3.13.

Slot ID whose value is one of: Py_MOD_GIL_USED ¶ The module depends on the presence of the global interpreter lock (GIL), and may access global state without synchronization. Py_MOD_GIL_NOT_USED ¶ The module is safe to run without an active GIL. This slot is ignored by Python builds not configured with --disable-gil . Otherwise, it determines whether or not importing this module will cause the GIL to be automatically enabled.

See Free-threaded CPython for more detail. If Py_mod_gil is not specified, the import machinery defaults to Py_MOD_GIL_USED . For historical reasons, the values are declared as pointers ( void * ). When using PySlot arrays, use PySlot_DATA for Py_mod_gil : PySlot_DATA ( Py_mod_gil , Py_MOD_GIL_NOT_USED ) Added in version 3.13. Creation and initialization slots ¶ Py_mod_create ¶ Part of the Stable ABI since version 3.5.

Slot ID for a function that creates the module object itself. The function must have the signature: PyObject * create_module ( PyObject * spec , PyModuleDef * def ) ¶ The function will be called with: spec : a ModuleSpec -like object, meanin

The rest of this page is on the live site.