Module Objects — Python 3.14.7 documentation

docs.python.org

Tin mới

Python Module Index

3.14.7 Documentation

Python/C API reference manual

Concrete Objects Layer

module.__package__

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

UnicodeEncodeError

Deprecated since version 3.2: PyModule_GetFilename() raises UnicodeEncodeError on unencodable filenames, use PyModule_GetFilenameObject() instead.

Defining extension modules

The functions in the previous section work on any module object, including modules imported from Python code.

PyTypeObject.tp_clear

A clear function to call during GC clearing of the module object, or NULL if not needed.

importlib.machinery.ModuleSpec

Specifies a function that is called to create the module object itself. The value pointer of this slot must point to a function of the signature:

Isolating Extension Modules

The module supports being imported in subinterpreters, but only when they share the main interpreter’s GIL. (See Isolating Extension Modules.)

Free-threaded CPython

The module depends on the presence of the global interpreter lock (GIL), and may access global state without synchronization.

PyUnicode_FromString()

Add an object to module as name. This is a convenience function which can be used from the module’s initialization function.

PyObject_SetAttr()

Add an object to module as name. This is a convenience function which can be used from the module’s initialization function.

attached thread state

Attaches the module object passed to the function to the interpreter state. This allows the module object to be accessible via PyState_FindModule().