Module Objects¶
-
PyTypeObject PyModule_Type¶
- Part of the Stable ABI.
This instance of
PyTypeObjectrepresents the Python module type. This is exposed to Python programs astypes.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 toNone). The caller is responsible for setting a__file__attribute.Return
NULLwith an exception set on error.Added in version 3.3.
Changed in version 3.4:
__package__and__loader__are now set toNone.
-
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),SystemErroris raised andNULLis returned.It is recommended extensions use other
PyModule_*andPyObject_*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,SystemErroris raised andNULLis 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
PyModuleDefstruct from which the module was created, orNULLif the module wasn’t created from a definition.On error, return
NULLwith an exception set. UsePyErr_Occurred()to tell this case apart from a missingPyModuleDef.
-
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, raiseSystemErrorand returnNULL; 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.Deprecated since version 3.2:
PyModule_GetFilename()raisesUnicodeEncodeErroron unencodable filenames, usePyModule_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.
Changed in version 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 IDfor the name of the new module, as a NUL-terminated UTF8-encodedconst