| 1 |
.. highlight:: c |
| 2 |
|
| 3 |
.. _moduleobjects: |
| 4 |
|
| 5 |
Module Objects |
| 6 |
-------------- |
| 7 |
|
| 8 |
.. index:: pair: object; module |
| 9 |
|
| 10 |
|
| 11 |
.. c:var:: PyTypeObject PyModule_Type |
| 12 |
|
| 13 |
.. index:: single: ModuleType (in module types) |
| 14 |
|
| 15 |
This instance of :c:type:`PyTypeObject` represents the Python module type. This |
| 16 |
is exposed to Python programs as :py:class:`types.ModuleType`. |
| 17 |
|
| 18 |
|
| 19 |
.. c:function:: int PyModule_Check(PyObject *p) |
| 20 |
|
| 21 |
Return true if *p* is a module object, or a subtype of a module object. |
| 22 |
This function always succeeds. |
| 23 |
|
| 24 |
|
| 25 |
.. c:function:: int PyModule_CheckExact(PyObject *p) |
| 26 |
|
| 27 |
Return true if *p* is a module object, but not a subtype of |
| 28 |
:c:data:`PyModule_Type`. This function always succeeds. |
| 29 |
|
| 30 |
|
| 31 |
.. c:function:: PyObject* PyModule_NewObject(PyObject *name) |
| 32 |
|
| 33 |
.. index:: |
| 34 |
single: __name__ (module attribute) |
| 35 |
single: __doc__ (module attribute) |
| 36 |
single: __file__ (module attribute) |
| 37 |
single: __package__ (module attribute) |
| 38 |
single: __loader__ (module attribute) |
| 39 |
|
| 40 |
Return a new module object with :attr:`module.__name__` set to *name*. |
| 41 |
The module's :attr:`!__name__`, :attr:`~module.__doc__`, |
| 42 |
:attr:`~module.__package__` and :attr:`~module.__loader__` attributes are |
| 43 |
filled in (all but :attr:`!__name__` are set to ``None``). The caller is |
| 44 |
responsible for setting a :attr:`~module.__file__` attribute. |
| 45 |
|
| 46 |
Return ``NULL`` with an exception set on error. |
| 47 |
|
| 48 |
.. versionadded:: 3.3 |
| 49 |
|
| 50 |
.. versionchanged:: 3.4 |
| 51 |
:attr:`~module.__package__` and :attr:`~module.__loader__` are now set to |
| 52 |
``None``. |
| 53 |
|
| 54 |
|
| 55 |
.. c:function:: PyObject* PyModule_New(const char *name) |
| 56 |
|
| 57 |
Similar to :c:func:`PyModule_NewObject`, but the name is a UTF-8 encoded |
| 58 |
string instead of a Unicode object. |
| 59 |
|
| 60 |
|
| 61 |
.. c:function:: PyObject* PyModule_GetDict(PyObject *module) |
| 62 |
|
| 63 |
.. index:: single: __dict__ (module attribute) |
| 64 |
|
| 65 |
Return the dictionary object that implements *module*'s namespace; this object |
| 66 |
is the same as the :attr:`~object.__dict__` attribute of the module object. |
| 67 |
If *module* is not a module object (or a subtype of a module object), |
| 68 |
:exc:`SystemError` is raised and ``NULL`` is returned. |
| 69 |
|
| 70 |
It is recommended extensions use other ``PyModule_*`` and |
| 71 |
``PyObject_*`` functions rather than directly manipulate a module's |
| 72 |
:attr:`~object.__dict__`. |
| 73 |
|
| 74 |
The returned reference is borrowed from the module; it is valid until |
| 75 |
the module is destroyed. |
| 76 |
|
| 77 |
|
| 78 |
.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module) |
| 79 |
|
| 80 |
.. index:: |
| 81 |
single: __name__ (module attribute) |
| 82 |
single: SystemError (built-in exception) |
| 83 |
|
| 84 |
Return *module*'s :attr:`~module.__name__` value. If the module does not |
| 85 |
provide one, or if it is not a string, :exc:`SystemError` is raised and |
| 86 |
``NULL`` is returned. |
| 87 |
|
| 88 |
.. versionadded:: 3.3 |
| 89 |
|
| 90 |
|
| 91 |
.. c:function:: const char* PyModule_GetName(PyObject *module) |
| 92 |
|
| 93 |
Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to |
| 94 |
``'utf-8'``. |
| 95 |
|
| 96 |
The returned buffer is only valid until the module is renamed or destroyed. |
| 97 |
Note that Python code may rename a module by setting its :py:attr:`~module.__name__` |
| 98 |
attribute. |
| 99 |
|
| 100 |
.. c:function:: void* PyModule_GetState(PyObject *module) |
| 101 |
|
| 102 |
Return the "state" of the module, that is, a pointer to the block of memory |
| 103 |
allocated at module creation time, or ``NULL``. See |
| 104 |
:c:member:`PyModuleDef.m_size`. |
| 105 |
|
| 106 |
|
| 107 |
.. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module) |
| 108 |
|
| 109 |
Return a pointer to the :c:type:`PyModuleDef` struct from which the module was |
| 110 |
created, or ``NULL`` if the module wasn't created from a definition. |
| 111 |
|
| 112 |
On error, return ``NULL`` with an exception set. |
| 113 |
Use :c:func:`PyErr_Occurred` to tell this case apart from a missing |
| 114 |
:c:type:`!PyModuleDef`. |
| 115 |
|
| 116 |
|
| 117 |
.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module) |
| 118 |
|
| 119 |
.. index:: |
| 120 |
single: __file__ (module attribute) |
| 121 |
single: SystemError (built-in exception) |
| 122 |
|
| 123 |
Return the name of the file from which *module* was loaded using *module*'s |
| 124 |
:attr:`~module.__file__` attribute. If this is not defined, or if it is not a |
| 125 |
string, raise :exc:`SystemError` and return ``NULL``; otherwise return |
| 126 |
a reference to a Unicode object. |
| 127 |
|
| 128 |
.. versionadded:: 3.2 |
| 129 |
|
| 130 |
|
| 131 |
.. c:function:: const char* PyModule_GetFilename(PyObject *module) |
| 132 |
|
| 133 |
Similar to :c:func:`PyModule_GetFilenameObject` but return the filename |
| 134 |
encoded to 'utf-8'. |
| 135 |
|
| 136 |
The returned buffer is only valid until the module's :py:attr:`~module.__file__` attribute |
| 137 |
is reassigned or the module is destroyed. |
| 138 |
|
| 139 |
.. deprecated:: 3.2 |
| 140 |
:c:func:`PyModule_GetFilename` raises :exc:`UnicodeEncodeError` on |
| 141 |
unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead. |
| 142 |
|
| 143 |
|
| 144 |
.. _initializing-modules: |
| 145 |
|
| 146 |
Initializing C modules |
| 147 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 148 |
|
| 149 |
Modules objects are usually created from extension modules (shared libraries |
| 150 |
which export an initialization function), or compiled-in modules |
| 151 |
(where the initialization function is added using :c:func:`PyImport_AppendInittab`). |
| 152 |
See :ref:`building` or :ref:`extending-with-embedding` for details. |
| 153 |
|
| 154 |
The initialization function can either pass a module definition instance |
| 155 |
to :c:func:`PyModule_Create`, and return the resulting module object, |
| 156 |
or request "multi-phase initialization" by returning the definition struct itself. |
| 157 |
|
| 158 |
.. c:type:: PyModuleDef |
| 159 |
|
| 160 |
The module definition struct, which holds all information needed to create |
| 161 |
a module object. There is usually only one statically initialized variable |
| 162 |
of this type for each module. |
| 163 |
|
| 164 |
.. c:member:: PyModuleDef_Base m_base |
| 165 |
|
| 166 |
Always initialize this member to :c:macro:`PyModuleDef_HEAD_INIT`. |
| 167 |
|
| 168 |
.. c:member:: const char *m_name |
| 169 |
|
| 170 |
Name for the new module. |
| 171 |
|
| 172 |
.. c:member:: const char *m_doc |
| 173 |
|
| 174 |
Docstring for the module; usually a docstring variable created with |
| 175 |
:c:macro:`PyDoc_STRVAR` is used. |
| 176 |
|
| 177 |
.. c:member:: Py_ssize_t m_size |
| 178 |
|
| 179 |
Module state may be kept in a per-module memory area that can be |
| 180 |
retrieved with :c:func:`PyModule_GetState`, rather than in static globals. |
| 181 |
This makes modules safe for use in multiple sub-interpreters. |
| 182 |
|
| 183 |
This memory area is allocated based on *m_size* on module creation, |
| 184 |
and freed when the module object is deallocated, after the |
| 185 |
:c:member:`~PyModuleDef.m_free` function has been called, if present. |
| 186 |
|
| 187 |
Setting ``m_size`` to ``-1`` means that the module does not support |
| 188 |
sub-interpreters, because it has global state. |
| 189 |
|
| 190 |
Setting it to a non-negative value means that the module can be |
| 191 |
re-initialized and specifies the additional amount of memory it requires |
| 192 |
for its state. Non-negative ``m_size`` is required for multi-phase |
| 193 |
initialization. |
| 194 |
|
| 195 |
See :PEP:`3121` for more details. |
| 196 |
|
| 197 |
.. c:member:: PyMethodDef* m_methods |
| 198 |
|
| 199 |
A pointer to a table of module-level functions, described by |
| 200 |
:c:type:`PyMethodDef` values. Can be ``NULL`` if no functions are present. |
| 201 |
|
| 202 |
.. c:member:: PyModuleDef_Slot* m_slots |
| 203 |
|
| 204 |
An array of slot definitions for multi-phase initialization, terminated by |
| 205 |
a ``{0, NULL}`` entry. |
| 206 |
When using single-phase initialization, *m_slots* must be ``NULL``. |
| 207 |
|
| 208 |
.. versionchanged:: 3.5 |
| 209 |
|
| 210 |
Prior to version 3.5, this member was always set to ``NULL``, |
| 211 |
and was defined as: |
| 212 |
|
| 213 |
.. c:member:: inquiry m_reload |
| 214 |
|
| 215 |
.. c:member:: traverseproc m_traverse |
| 216 |
|
| 217 |
A traversal function to call during GC traversal of the module object, or |
| 218 |
``NULL`` if not needed. |
| 219 |
|
| 220 |
This function is not called if the module state was requested but is not |
| 221 |
allocated yet. This is the case immediately after the module is created |
| 222 |
and before the module is executed (:c:data:`Py_mod_exec` function). More |
| 223 |
precisely, this function is not called if :c:member:`~PyModuleDef.m_size` is greater |
| 224 |
than 0 and the module state (as returned by :c:func:`PyModule_GetState`) |
| 225 |
is ``NULL``. |
| 226 |
|
| 227 |
.. versionchanged:: 3.9 |
| 228 |
No longer called before the module state is allocated. |
| 229 |
|
| 230 |
.. c:member:: inquiry m_clear |
| 231 |
|
| 232 |
A clear function to call during GC clearing of the module object, or |
| 233 |
``NULL`` if not needed. |
| 234 |
|
| 235 |
This function is not called if the module state was requested but is not |
| 236 |
allocated yet. This is the case immediately after the module is created |
| 237 |
and before the module is executed (:c:data:`Py_mod_exec` function). More |
| 238 |
precisely, this function is not called if :c:member:`~PyModuleDef.m_size` is greater |
| 239 |
than 0 and the module state (as returned by :c:func:`PyModule_GetState`) |
| 240 |
is ``NULL``. |
| 241 |
|
| 242 |
Like :c:member:`PyTypeObject.tp_clear`, this function is not *always* |
| 243 |
called before a module is deallocated. For example, when reference |
| 244 |
counting is enough to determine that an object is no longer used, |
| 245 |
the cyclic garbage collector is not involved and |
| 246 |
:c:member:`~PyModuleDef.m_free` is called directly. |
| 247 |
|
| 248 |
.. versionchanged:: 3.9 |
| 249 |
No longer called before the module state is allocated. |
| 250 |
|
| 251 |
.. c:member:: freefunc m_free |
| 252 |
|
| 253 |
A function to call during deallocation of the module object, or ``NULL`` |
| 254 |
if not needed. |
| 255 |
|
| 256 |
This function is not called if the module state was requested but is not |
| 257 |
allocated yet. This is the case immediately after the module is created |
| 258 |
and before the module is executed (:c:data:`Py_mod_exec` function). More |
| 259 |
precisely, this function is not called if :c:member:`~PyModuleDef.m_size` is greater |
| 260 |
than 0 and the module state (as returned by :c:func:`PyModule_GetState`) |
| 261 |
is ``NULL``. |
| 262 |
|
| 263 |
.. versionchanged:: 3.9 |
| 264 |
No longer called before the module state is allocated. |
| 265 |
|
| 266 |
Single-phase initialization |
| 267 |
........................... |
| 268 |
|
| 269 |
The module initialization function may create and return the module object |
| 270 |
directly. This is referred to as "single-phase initialization", and uses one |
| 271 |
of the following two module creation functions: |
| 272 |
|
| 273 |
.. c:function:: PyObject* PyModule_Create(PyModuleDef *def) |
| 274 |
|
| 275 |
Create a new module object, given the definition in *def*. This behaves |
| 276 |
like :c:func:`PyModule_Create2` with *module_api_version* set to |
| 277 |
:c:macro:`PYTHON_API_VERSION`. |
| 278 |
|
| 279 |
|
| 280 |
.. c:function:: PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version) |
| 281 |
|
| 282 |
Create a new module object, given the definition in *def*, assuming the |
| 283 |
API version *module_api_version*. If that version does not match the version |
| 284 |
of the running interpreter, a :exc:`RuntimeWarning` is emitted. |
| 285 |
|
| 286 |
Return ``NULL`` with an exception set on error. |
| 287 |
|
| 288 |
.. note:: |
| 289 |
|
| 290 |
Most uses of this function should be using :c:func:`PyModule_Create` |
| 291 |
instead; only use this if you are sure you need it. |
| 292 |
|
| 293 |
Before it is returned from in the initialization function, the resulting module |
| 294 |
object is typically populated using functions like :c:func:`PyModule_AddObjectRef`. |
| 295 |
|
| 296 |
.. _multi-phase-initialization: |
| 297 |
|
| 298 |
Multi-phase initialization |
| 299 |
.......................... |
| 300 |
|
| 301 |
An alternate way to specify extensions is to request "multi-phase initialization". |
| 302 |
Extension modules created this way behave more like Python modules: the |
| 303 |
initialization is split between the *creation phase*, when the module object |
| 304 |
is created, and the *execution phase*, when it is populated. |
| 305 |
The distinction is similar to the :py:meth:`~object.__new__` and |
| 306 |
:py:meth:`~object.__init__` methods of classes. |
| 307 |
|
| 308 |
Unlike modules created using single-phase initialization, these modules are not |
| 309 |
singletons. |
| 310 |
For example, if the :py:attr:`sys.modules` entry is removed and the module |
| 311 |
is re-imported, a new module object is created, and typically populated with |
| 312 |
fresh method and type objects. |
| 313 |
The old module is subject to normal garbage collection. |
| 314 |
This mirrors the behavior of pure-Python modules. |
| 315 |
|
| 316 |
Additional module instances may be created in |
| 317 |
:ref:`sub-interpreters <sub-interpreter-support>` |
| 318 |
or after after Python runtime reinitialization |
| 319 |
(:c:func:`Py_Finalize` and :c:func:`Py_Initialize`). |
| 320 |
In these cases, sharing Python objects between module instances would likely |
| 321 |
cause crashes or undefined behavior. |
| 322 |
|
| 323 |
To avoid such issues, each instance of an extension module should |
| 324 |
be *isolated*: changes to one instance should not implicitly affect the others, |
| 325 |
and all state, including references to Python objects, should be specific to |
| 326 |
a particular module instance. |
| 327 |
See :ref:`isolating-extensions-howto` for more details and a practical guide. |
| 328 |
|
| 329 |
A simpler way to avoid these issues is |
| 330 |
:ref:`raising an error on repeated initialization <isolating-extensions-optout>`. |
| 331 |
|
| 332 |
All modules created using multi-phase initialization are expected to support |
| 333 |
:ref:`sub-interpreters <sub-interpreter-support>`, or otherwise explicitly |
| 334 |
signal a lack of support. |
| 335 |
This is usually achieved by isolation or blocking repeated initialization, |
| 336 |
as above. |
| 337 |
A module may also be limited to the main interpreter using |
| 338 |
the :c:data:`Py_mod_multiple_interpreters` slot. |
| 339 |
|
| 340 |
To request multi-phase initialization, the initialization function |
| 341 |
(PyInit_modulename) returns a :c:type:`PyModuleDef` instance with non-empty |
| 342 |
:c:member:`~PyModuleDef.m_slots`. Before it is returned, the ``PyModuleDef`` |
| 343 |
instance must be initialized with the following function: |
| 344 |
|
| 345 |
.. c:function:: PyObject* PyModuleDef_Init(PyModuleDef *def) |
| 346 |
|
| 347 |
Ensures a module definition is a properly initialized Python object that |
| 348 |
correctly reports its type and reference count. |
| 349 |
|
| 350 |
Returns *def* cast to ``PyObject*``, or ``NULL`` if an error occurred. |
| 351 |
|
| 352 |
.. versionadded:: 3.5 |
| 353 |
|
| 354 |
The *m_slots* member of the module definition must point to an array of |
| 355 |
``PyModuleDef_Slot`` structures: |
| 356 |
|
| 357 |
.. c:type:: PyModuleDef_Slot |
| 358 |
|
| 359 |
.. c:member:: int slot |
| 360 |
|
| 361 |
A slot ID, chosen from the available values explained below. |
| 362 |
|
| 363 |
.. c:member:: void* value |
| 364 |
|
| 365 |
Value of the slot, whose meaning depends on the slot ID. |
| 366 |
|
| 367 |
.. versionadded:: 3.5 |
| 368 |
|
| 369 |
The *m_slots* array must be terminated by a slot with id 0. |
| 370 |
|
| 371 |
The available slot types are: |
| 372 |
|
| 373 |
.. c:macro:: Py_mod_create |
| 374 |
|
| 375 |
Specifies a function that is called to create the module object itself. |
| 376 |
The *value* pointer of this slot must point to a function of the signature: |
| 377 |
|
| 378 |
.. c:function:: PyObject* create_module(PyObject *spec, PyModuleDef *def) |
| 379 |
:no-index-entry: |
| 380 |
:no-contents-entry: |
| 381 |
|
| 382 |
The function receives a :py:class:`~importlib.machinery.ModuleSpec` |
| 383 |
instance, as defined in :PEP:`451`, and the module definition. |
| 384 |
It should return a new module object, or set an error |
| 385 |
and return ``NULL``. |
| 386 |
|
| 387 |
This function should be kept minimal. In particular, it should not |
| 388 |
call arbitrary Python code, as trying to import the same module again may |
| 389 |
result in an infinite loop. |
| 390 |
|
| 391 |
Multiple ``Py_mod_create`` slots may not be specified in one module |
| 392 |
definition. |
| 393 |
|
| 394 |
If ``Py_mod_create`` is not specified, the import machinery will create |
| 395 |
a normal module object using :c:func:`PyModule_New`. The name is taken from |
| 396 |
*spec*, not the definition, to allow extension modules to dynamically adjust |
| 397 |
to their place in the module hierarchy and be imported under different |
| 398 |
names through symlinks, all while sharing a single module definition. |
| 399 |
|
| 400 |
There is no requirement for the returned object to be an instance of |
| 401 |
:c:type:`PyModule_Type`. Any type can be used, as long as it supports |
| 402 |
setting and getting import-related attributes. |
| 403 |
However, only ``PyModule_Type`` instances may be returned if the |
| 404 |
``PyModuleDef`` has non-``NULL`` ``m_traverse``, ``m_clear``, |
| 405 |
``m_free``; non-zero ``m_size``; or slots other than ``Py_mod_create``. |
| 406 |
|
| 407 |
.. versionadded:: 3.5 |
| 408 |
|
| 409 |
.. c:macro:: Py_mod_exec |
| 410 |
|
| 411 |
Specifies a function that is called to *execute* the module. |
| 412 |
This is equivalent to executing the code of a Python module: typically, |
| 413 |
this function adds classes and constants to the module. |
| 414 |
The signature of the function is: |
| 415 |
|
| 416 |
.. c:function:: int exec_module(PyObject* module) |
| 417 |
:no-index-entry: |
| 418 |
:no-contents-entry: |
| 419 |
|
| 420 |
If multiple ``Py_mod_exec`` slots are specified, they are processed in the |
| 421 |
order they appear in the *m_slots* array. |
| 422 |
|
| 423 |
.. versionadded:: 3.5 |
| 424 |
|
| 425 |
.. c:macro:: Py_mod_multiple_interpreters |
| 426 |
|
| 427 |
Specifies one of the following values: |
| 428 |
|
| 429 |
.. c:namespace:: NULL |
| 430 |
|
| 431 |
.. c:macro:: Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED |
| 432 |
|
| 433 |
The module does not support being imported in subinterpreters. |
| 434 |
|
| 435 |
.. c:macro:: Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED |
| 436 |
|
| 437 |
The module supports being imported in subinterpreters, |
| 438 |
but only when they share the main interpreter's GIL. |
| 439 |
(See :ref:`isolating-extensions-howto`.) |
| 440 |
|
| 441 |
.. c:macro:: Py_MOD_PER_INTERPRETER_GIL_SUPPORTED |
| 442 |
|
| 443 |
The module supports being imported in subinterpreters, |
| 444 |
even when they have their own GIL. |
| 445 |
(See :ref:`isolating-extensions-howto`.) |
| 446 |
|
| 447 |
This slot determines whether or not importing this module |
| 448 |
in a subinterpreter will fail. |
| 449 |
|
| 450 |
Multiple ``Py_mod_multiple_interpreters`` slots may not be specified |
| 451 |
in one module definition. |
| 452 |
|
| 453 |
If ``Py_mod_multiple_interpreters`` is not specified, the import |
| 454 |
machinery defaults to ``Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED``. |
| 455 |
|
| 456 |
.. versionadded:: 3.12 |
| 457 |
|
| 458 |
.. c:macro:: Py_mod_gil |
| 459 |
|
| 460 |
Specifies one of the following values: |
| 461 |
|
| 462 |
.. c:namespace:: NULL |
| 463 |
|
| 464 |
.. c:macro:: Py_MOD_GIL_USED |
| 465 |
|
| 466 |
The module depends on the presence of the global interpreter lock (GIL), |
| 467 |
and may access global state without synchronization. |
| 468 |
|
| 469 |
.. c:macro:: Py_MOD_GIL_NOT_USED |
| 470 |
|
| 471 |
The module is safe to run without an active GIL. |
| 472 |
|
| 473 |
This slot is ignored by Python builds not configured with |
| 474 |
:option:`--disable-gil`. Otherwise, it determines whether or not importing |
| 475 |
this module will cause the GIL to be automatically enabled. See |
| 476 |
:ref:`whatsnew313-free-threaded-cpython` for more detail. |
| 477 |
|
| 478 |
Multiple ``Py_mod_gil`` slots may not be specified in one module definition. |
| 479 |
|
| 480 |
If ``Py_mod_gil`` is not specified, the import machinery defaults to |
| 481 |
``Py_MOD_GIL_USED``. |
| 482 |
|
| 483 |
.. versionadded:: 3.13 |
| 484 |
|
| 485 |
See :PEP:`489` for more details on multi-phase initialization. |
| 486 |
|
| 487 |
Low-level module creation functions |
| 488 |
................................... |
| 489 |
|
| 490 |
The following functions are called under the hood when using multi-phase |
| 491 |
initialization. They can be used directly, for example when creating module |
| 492 |
objects dynamically. Note that both ``PyModule_FromDefAndSpec`` and |
| 493 |
``PyModule_ExecDef`` must be called to fully initialize a module. |
| 494 |
|
| 495 |
.. c:function:: PyObject * PyModule_FromDefAndSpec(PyModuleDef *def, PyObject *spec) |
| 496 |
|
| 497 |
Create a new module object, given the definition in *def* and the |
| 498 |
ModuleSpec *spec*. This behaves like :c:func:`PyModule_FromDefAndSpec2` |
| 499 |
with *module_api_version* set to :c:macro:`PYTHON_API_VERSION`. |
| 500 |
|
| 501 |
.. versionadded:: 3.5 |
| 502 |
|
| 503 |
.. c:function:: PyObject * PyModule_FromDefAndSpec2(PyModuleDef *def, PyObject *spec, int module_api_version) |
| 504 |
|
| 505 |
Create a new module object, given the definition in *def* and the |
| 506 |
ModuleSpec *spec*, assuming the API version *module_api_version*. |
| 507 |
If that version does not match the version of the running interpreter, |
| 508 |
a :exc:`RuntimeWarning` is emitted. |
| 509 |
|
| 510 |
Return ``NULL`` with an exception set on error. |
| 511 |
|
| 512 |
.. note:: |
| 513 |
|
| 514 |
Most uses of this function should be using :c:func:`PyModule_FromDefAndSpec` |
| 515 |
instead; only use this if you are sure you need it. |
| 516 |
|
| 517 |
.. versionadded:: 3.5 |
| 518 |
|
| 519 |
.. c:function:: int PyModule_ExecDef(PyObject *module, PyModuleDef *def) |
| 520 |
|
| 521 |
Process any execution slots (:c:data:`Py_mod_exec`) given in *def*. |
| 522 |
|
| 523 |
.. versionadded:: 3.5 |
| 524 |
|
| 525 |
.. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring) |
| 526 |
|
| 527 |
Set the docstring for *module* to *docstring*. |
| 528 |
This function is called automatically when creating a module from |
| 529 |
``PyModuleDef``, using either ``PyModule_Create`` or |
| 530 |
``PyModule_FromDefAndSpec``. |
| 531 |
|
| 532 |
Return ``0`` on success. |
| 533 |
Return ``-1`` with an exception set on error. |
| 534 |
|
| 535 |
.. versionadded:: 3.5 |
| 536 |
|
| 537 |
.. c:function:: int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions) |
| 538 |
|
| 539 |
Add the functions from the ``NULL`` terminated *functions* array to *module*. |
| 540 |
Refer to the :c:type:`PyMethodDef` documentation for details on individual |
| 541 |
entries (due to the lack of a shared module namespace, module level |
| 542 |
"functions" implemented in C typically receive the module as their first |
| 543 |
parameter, making them similar to instance methods on Python classes). |
| 544 |
This function is called automatically when creating a module from |
| 545 |
``PyModuleDef``, using either ``PyModule_Create`` or |
| 546 |
``PyModule_FromDefAndSpec``. |
| 547 |
|
| 548 |
The *functions* array must be statically allocated (or otherwise guaranteed |
| 549 |
to outlive the module object). |
| 550 |
|
| 551 |
.. versionadded:: 3.5 |
| 552 |
|
| 553 |
Support functions |
| 554 |
................. |
| 555 |
|
| 556 |
The module initialization function (if using single phase initialization) or |
| 557 |
a function called from a module execution slot (if using multi-phase |
| 558 |
initialization), can use the following functions to help initialize the module |
| 559 |
state: |
| 560 |
|
| 561 |
.. c:function:: int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value) |
| 562 |
|
| 563 |
Add an object to *module* as *name*. This is a convenience function which |
| 564 |
can be used from the module's initialization function. |
| 565 |
|
| 566 |
On success, return ``0``. On error, raise an exception and return ``-1``. |
| 567 |
|
| 568 |
Example usage:: |
| 569 |
|
| 570 |
static int |
| 571 |
add_spam(PyObject *module, int value) |
| 572 |
{ |
| 573 |
PyObject *obj = PyLong_FromLong(value); |
| 574 |
if (obj == NULL) { |
| 575 |
return -1; |
| 576 |
} |
| 577 |
int res = PyModule_AddObjectRef(module, "spam", obj); |
| 578 |
Py_DECREF(obj); |
| 579 |
return res; |
| 580 |
} |
| 581 |
|
| 582 |
To be convenient, the function accepts ``NULL`` *value* with an exception |
| 583 |
set. In this case, return ``-1`` and just leave the raised exception |
| 584 |
unchanged. |
| 585 |
|
| 586 |
The example can also be written without checking explicitly if *obj* is |
| 587 |
``NULL``:: |
| 588 |
|
| 589 |
static int |
| 590 |
add_spam(PyObject *module, int value) |
| 591 |
{ |
| 592 |
PyObject *obj = PyLong_FromLong(value); |
| 593 |
int res = PyModule_AddObjectRef(module, "spam", obj); |
| 594 |
Py_XDECREF(obj); |
| 595 |
return res; |
| 596 |
} |
| 597 |
|
| 598 |
Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in |
| 599 |
this case, since *obj* can be ``NULL``. |
| 600 |
|
| 601 |
The number of different *name* strings passed to this function |
| 602 |
should be kept small, usually by only using statically allocated strings |
| 603 |
as *name*. |
| 604 |
For names that aren't known at compile time, prefer calling |
| 605 |
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly. |
| 606 |
For more details, see :c:func:`PyUnicode_InternFromString`, which may be |
| 607 |
used internally to create a key object. |
| 608 |
|
| 609 |
.. versionadded:: 3.10 |
| 610 |
|
| 611 |
|
| 612 |
.. c:function:: int PyModule_Add(PyObject *module, const char *name, PyObject *value) |
| 613 |
|
| 614 |
Similar to :c:func:`PyModule_AddObjectRef`, but "steals" a reference |
| 615 |
to *value*. |
| 616 |
It can be called with a result of function that returns a new reference |
| 617 |
without bothering to check its result or even saving it to a variable. |
| 618 |
|
| 619 |
Example usage:: |
| 620 |
|
| 621 |
if (PyModule_Add(module, "spam", PyBytes_FromString(value)) < 0) { |
| 622 |
goto error; |
| 623 |
} |
| 624 |
|
| 625 |
.. versionadded:: 3.13 |
| 626 |
|
| 627 |
|
| 628 |
.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value) |
| 629 |
|
| 630 |
Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to |
| 631 |
*value* on success (if it returns ``0``). |
| 632 |
|
| 633 |
The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef` |
| 634 |
functions are recommended, since it is |
| 635 |
easy to introduce reference leaks by misusing the |
| 636 |
:c:func:`PyModule_AddObject` function. |
| 637 |
|
| 638 |
.. note:: |
| 639 |
|
| 640 |
Unlike other functions that steal references, ``PyModule_AddObject()`` |
| 641 |
only releases the reference to *value* **on success**. |
| 642 |
|
| 643 |
This means that its return value must be checked, and calling code must |
| 644 |
:c:func:`Py_XDECREF` *value* manually on error. |
| 645 |
|
| 646 |
Example usage:: |
| 647 |
|
| 648 |
PyObject *obj = PyBytes_FromString(value); |
| 649 |
if (PyModule_AddObject(module, "spam", obj) < 0) { |
| 650 |
// If 'obj' is not NULL and PyModule_AddObject() failed, |
| 651 |
// 'obj' strong reference must be deleted with Py_XDECREF(). |
| 652 |
// If 'obj' is NULL, Py_XDECREF() does nothing. |
| 653 |
Py_XDECREF(obj); |
| 654 |
goto error; |
| 655 |
} |
| 656 |
// PyModule_AddObject() stole a reference to obj: |
| 657 |
// Py_XDECREF(obj) is not needed here. |
| 658 |
|
| 659 |
.. deprecated:: 3.13 |
| 660 |
|
| 661 |
:c:func:`PyModule_AddObject` is :term:`soft deprecated`. |
| 662 |
|
| 663 |
|
| 664 |
.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value) |
| 665 |
|
| 666 |
Add an integer constant to *module* as *name*. This convenience function can be |
| 667 |
used from the module's initialization function. |
| 668 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 669 |
|
| 670 |
This is a convenience function that calls :c:func:`PyLong_FromLong` and |
| 671 |
:c:func:`PyModule_AddObjectRef`; see their documentation for details. |
| 672 |
|
| 673 |
|
| 674 |
.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value) |
| 675 |
|
| 676 |
Add a string constant to *module* as *name*. This convenience function can be |
| 677 |
used from the module's initialization function. The string *value* must be |
| 678 |
``NULL``-terminated. |
| 679 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 680 |
|
| 681 |
This is a convenience function that calls |
| 682 |
:c:func:`PyUnicode_InternFromString` and :c:func:`PyModule_AddObjectRef`; |
| 683 |
see their documentation for details. |
| 684 |
|
| 685 |
|
| 686 |
.. c:macro:: PyModule_AddIntMacro(module, macro) |
| 687 |
|
| 688 |
Add an int constant to *module*. The name and the value are taken from |
| 689 |
*macro*. For example ``PyModule_AddIntMacro(module, AF_INET)`` adds the int |
| 690 |
constant *AF_INET* with the value of *AF_INET* to *module*. |
| 691 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 692 |
|
| 693 |
|
| 694 |
.. c:macro:: PyModule_AddStringMacro(module, macro) |
| 695 |
|
| 696 |
Add a string constant to *module*. |
| 697 |
|
| 698 |
.. c:function:: int PyModule_AddType(PyObject *module, PyTypeObject *type) |
| 699 |
|
| 700 |
Add a type object to *module*. |
| 701 |
The type object is finalized by calling internally :c:func:`PyType_Ready`. |
| 702 |
The name of the type object is taken from the last component of |
| 703 |
:c:member:`~PyTypeObject.tp_name` after dot. |
| 704 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 705 |
|
| 706 |
.. versionadded:: 3.9 |
| 707 |
|
| 708 |
.. c:function:: int PyUnstable_Module_SetGIL(PyObject *module, void *gil) |
| 709 |
|
| 710 |
Indicate that *module* does or does not support running without the global |
| 711 |
interpreter lock (GIL), using one of the values from |
| 712 |
:c:macro:`Py_mod_gil`. It must be called during *module*'s initialization |
| 713 |
function. If this function is not called during module initialization, the |
| 714 |
import machinery assumes the module does not support running without the |
| 715 |
GIL. This function is only available in Python builds configured with |
| 716 |
:option:`--disable-gil`. |
| 717 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 718 |
|
| 719 |
.. versionadded:: 3.13 |
| 720 |
|
| 721 |
|
| 722 |
Module lookup |
| 723 |
^^^^^^^^^^^^^ |
| 724 |
|
| 725 |
Single-phase initialization creates singleton modules that can be looked up |
| 726 |
in the context of the current interpreter. This allows the module object to be |
| 727 |
retrieved later with only a reference to the module definition. |
| 728 |
|
| 729 |
These functions will not work on modules created using multi-phase initialization, |
| 730 |
since multiple such modules can be created from a single definition. |
| 731 |
|
| 732 |
.. c:function:: PyObject* PyState_FindModule(PyModuleDef *def) |
| 733 |
|
| 734 |
Returns the module object that was created from *def* for the current interpreter. |
| 735 |
This method requires that the module object has been attached to the interpreter state with |
| 736 |
:c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not |
| 737 |
found or has not been attached to the interpreter state yet, it returns ``NULL``. |
| 738 |
|
| 739 |
.. c:function:: int PyState_AddModule(PyObject *module, PyModuleDef *def) |
| 740 |
|
| 741 |
Attaches the module object passed to the function to the interpreter state. This allows |
| 742 |
the module object to be accessible via :c:func:`PyState_FindModule`. |
| 743 |
|
| 744 |
Only effective on modules created using single-phase initialization. |
| 745 |
|
| 746 |
Python calls ``PyState_AddModule`` automatically after importing a module, |
| 747 |
so it is unnecessary (but harmless) to call it from module initialization |
| 748 |
code. An explicit call is needed only if the module's own init code |
| 749 |
subsequently calls ``PyState_FindModule``. |
| 750 |
The function is mainly intended for implementing alternative import |
| 751 |
mechanisms (either by calling it directly, or by referring to its |
| 752 |
implementation for details of the required state updates). |
| 753 |
|
| 754 |
The caller must hold the GIL. |
| 755 |
|
| 756 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 757 |
|
| 758 |
.. versionadded:: 3.3 |
| 759 |
|
| 760 |
.. c:function:: int PyState_RemoveModule(PyModuleDef *def) |
| 761 |
|
| 762 |
Removes the module object created from *def* from the interpreter state. |
| 763 |
Return ``-1`` with an exception set on error, ``0`` on success. |
| 764 |
|
| 765 |
The caller must hold the GIL. |
| 766 |
|
| 767 |
.. versionadded:: 3.3 |