17db96d56Sopenharmony_ci.. highlight:: c 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci*************************** 47db96d56Sopenharmony_ciIsolating Extension Modules 57db96d56Sopenharmony_ci*************************** 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. topic:: Abstract 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci Traditionally, state belonging to Python extension modules was kept in C 107db96d56Sopenharmony_ci ``static`` variables, which have process-wide scope. This document 117db96d56Sopenharmony_ci describes problems of such per-process state and shows a safer way: 127db96d56Sopenharmony_ci per-module state. 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci The document also describes how to switch to per-module state where 157db96d56Sopenharmony_ci possible. This transition involves allocating space for that state, potentially 167db96d56Sopenharmony_ci switching from static types to heap types, and—perhaps most 177db96d56Sopenharmony_ci importantly—accessing per-module state from code. 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciWho should read this 217db96d56Sopenharmony_ci==================== 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ciThis guide is written for maintainers of :ref:`C-API <c-api-index>` extensions 247db96d56Sopenharmony_ciwho would like to make that extension safer to use in applications where 257db96d56Sopenharmony_ciPython itself is used as a library. 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ciBackground 297db96d56Sopenharmony_ci========== 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ciAn *interpreter* is the context in which Python code runs. It contains 327db96d56Sopenharmony_ciconfiguration (e.g. the import path) and runtime state (e.g. the set of 337db96d56Sopenharmony_ciimported modules). 347db96d56Sopenharmony_ci 357db96d56Sopenharmony_ciPython supports running multiple interpreters in one process. There are 367db96d56Sopenharmony_citwo cases to think about—users may run interpreters: 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci- in sequence, with several :c:func:`Py_InitializeEx`/:c:func:`Py_FinalizeEx` 397db96d56Sopenharmony_ci cycles, and 407db96d56Sopenharmony_ci- in parallel, managing "sub-interpreters" using 417db96d56Sopenharmony_ci :c:func:`Py_NewInterpreter`/:c:func:`Py_EndInterpreter`. 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ciBoth cases (and combinations of them) would be most useful when 447db96d56Sopenharmony_ciembedding Python within a library. Libraries generally shouldn't make 457db96d56Sopenharmony_ciassumptions about the application that uses them, which include 467db96d56Sopenharmony_ciassuming a process-wide "main Python interpreter". 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ciHistorically, Python extension modules don't handle this use case well. 497db96d56Sopenharmony_ciMany extension modules (and even some stdlib modules) use *per-process* 507db96d56Sopenharmony_ciglobal state, because C ``static`` variables are extremely easy to use. 517db96d56Sopenharmony_ciThus, data that should be specific to an interpreter ends up being shared 527db96d56Sopenharmony_cibetween interpreters. Unless the extension developer is careful, it is very 537db96d56Sopenharmony_cieasy to introduce edge cases that lead to crashes when a module is loaded in 547db96d56Sopenharmony_cimore than one interpreter in the same process. 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ciUnfortunately, *per-interpreter* state is not easy to achieve. Extension 577db96d56Sopenharmony_ciauthors tend to not keep multiple interpreters in mind when developing, 587db96d56Sopenharmony_ciand it is currently cumbersome to test the behavior. 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ciEnter Per-Module State 617db96d56Sopenharmony_ci---------------------- 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ciInstead of focusing on per-interpreter state, Python's C API is evolving 647db96d56Sopenharmony_cito better support the more granular *per-module* state. 657db96d56Sopenharmony_ciThis means that C-level data is be attached to a *module object*. 667db96d56Sopenharmony_ciEach interpreter creates its own module object, keeping the data separate. 677db96d56Sopenharmony_ciFor testing the isolation, multiple module objects corresponding to a single 687db96d56Sopenharmony_ciextension can even be loaded in a single interpreter. 697db96d56Sopenharmony_ci 707db96d56Sopenharmony_ciPer-module state provides an easy way to think about lifetime and 717db96d56Sopenharmony_ciresource ownership: the extension module will initialize when a 727db96d56Sopenharmony_cimodule object is created, and clean up when it's freed. In this regard, 737db96d56Sopenharmony_cia module is just like any other :c:expr:`PyObject *`; there are no "on 747db96d56Sopenharmony_ciinterpreter shutdown" hooks to think—or forget—about. 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ciNote that there are use cases for different kinds of "globals": 777db96d56Sopenharmony_ciper-process, per-interpreter, per-thread or per-task state. 787db96d56Sopenharmony_ciWith per-module state as the default, these are still possible, 797db96d56Sopenharmony_cibut you should treat them as exceptional cases: 807db96d56Sopenharmony_ciif you need them, you should give them additional care and testing. 817db96d56Sopenharmony_ci(Note that this guide does not cover them.) 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ciIsolated Module Objects 857db96d56Sopenharmony_ci----------------------- 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ciThe key point to keep in mind when developing an extension module is 887db96d56Sopenharmony_cithat several module objects can be created from a single shared library. 897db96d56Sopenharmony_ciFor example: 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci.. code-block:: pycon 927db96d56Sopenharmony_ci 937db96d56Sopenharmony_ci >>> import sys 947db96d56Sopenharmony_ci >>> import binascii 957db96d56Sopenharmony_ci >>> old_binascii = binascii 967db96d56Sopenharmony_ci >>> del sys.modules['binascii'] 977db96d56Sopenharmony_ci >>> import binascii # create a new module object 987db96d56Sopenharmony_ci >>> old_binascii == binascii 997db96d56Sopenharmony_ci False 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ciAs a rule of thumb, the two modules should be completely independent. 1027db96d56Sopenharmony_ciAll objects and state specific to the module should be encapsulated 1037db96d56Sopenharmony_ciwithin the module object, not shared with other module objects, and 1047db96d56Sopenharmony_cicleaned up when the module object is deallocated. 1057db96d56Sopenharmony_ciSince this just is a rule of thumb, exceptions are possible 1067db96d56Sopenharmony_ci(see `Managing Global State`_), but they will need more 1077db96d56Sopenharmony_cithought and attention to edge cases. 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_ciWhile some modules could do with less stringent restrictions, isolated 1107db96d56Sopenharmony_cimodules make it easier to set clear expectations and guidelines that 1117db96d56Sopenharmony_ciwork across a variety of use cases. 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ciSurprising Edge Cases 1157db96d56Sopenharmony_ci--------------------- 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ciNote that isolated modules do create some surprising edge cases. Most 1187db96d56Sopenharmony_cinotably, each module object will typically not share its classes and 1197db96d56Sopenharmony_ciexceptions with other similar modules. Continuing from the 1207db96d56Sopenharmony_ci`example above <Isolated Module Objects_>`__, 1217db96d56Sopenharmony_cinote that ``old_binascii.Error`` and ``binascii.Error`` are 1227db96d56Sopenharmony_ciseparate objects. In the following code, the exception is *not* caught: 1237db96d56Sopenharmony_ci 1247db96d56Sopenharmony_ci.. code-block:: pycon 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ci >>> old_binascii.Error == binascii.Error 1277db96d56Sopenharmony_ci False 1287db96d56Sopenharmony_ci >>> try: 1297db96d56Sopenharmony_ci ... old_binascii.unhexlify(b'qwertyuiop') 1307db96d56Sopenharmony_ci ... except binascii.Error: 1317db96d56Sopenharmony_ci ... print('boo') 1327db96d56Sopenharmony_ci ... 1337db96d56Sopenharmony_ci Traceback (most recent call last): 1347db96d56Sopenharmony_ci File "<stdin>", line 2, in <module> 1357db96d56Sopenharmony_ci binascii.Error: Non-hexadecimal digit found 1367db96d56Sopenharmony_ci 1377db96d56Sopenharmony_ciThis is expected. Notice that pure-Python modules behave the same way: 1387db96d56Sopenharmony_ciit is a part of how Python works. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ciThe goal is to make extension modules safe at the C level, not to make 1417db96d56Sopenharmony_cihacks behave intuitively. Mutating ``sys.modules`` "manually" counts 1427db96d56Sopenharmony_cias a hack. 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ciMaking Modules Safe with Multiple Interpreters 1467db96d56Sopenharmony_ci============================================== 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci 1497db96d56Sopenharmony_ciManaging Global State 1507db96d56Sopenharmony_ci--------------------- 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ciSometimes, the state associated with a Python module is not specific to that module, but 1537db96d56Sopenharmony_cito the entire process (or something else "more global" than a module). 1547db96d56Sopenharmony_ciFor example: 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci- The ``readline`` module manages *the* terminal. 1577db96d56Sopenharmony_ci- A module running on a circuit board wants to control *the* on-board 1587db96d56Sopenharmony_ci LED. 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ciIn these cases, the Python module should provide *access* to the global 1617db96d56Sopenharmony_cistate, rather than *own* it. If possible, write the module so that 1627db96d56Sopenharmony_cimultiple copies of it can access the state independently (along with 1637db96d56Sopenharmony_ciother libraries, whether for Python or other languages). If that is not 1647db96d56Sopenharmony_cipossible, consider explicit locking. 1657db96d56Sopenharmony_ci 1667db96d56Sopenharmony_ciIf it is necessary to use process-global state, the simplest way to 1677db96d56Sopenharmony_ciavoid issues with multiple interpreters is to explicitly prevent a 1687db96d56Sopenharmony_cimodule from being loaded more than once per process—see 1697db96d56Sopenharmony_ci`Opt-Out: Limiting to One Module Object per Process`_. 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ciManaging Per-Module State 1737db96d56Sopenharmony_ci------------------------- 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ciTo use per-module state, use 1767db96d56Sopenharmony_ci:ref:`multi-phase extension module initialization <multi-phase-initialization>`. 1777db96d56Sopenharmony_ciThis signals that your module supports multiple interpreters correctly. 1787db96d56Sopenharmony_ci 1797db96d56Sopenharmony_ciSet ``PyModuleDef.m_size`` to a positive number to request that many 1807db96d56Sopenharmony_cibytes of storage local to the module. Usually, this will be set to the 1817db96d56Sopenharmony_cisize of some module-specific ``struct``, which can store all of the 1827db96d56Sopenharmony_cimodule's C-level state. In particular, it is where you should put 1837db96d56Sopenharmony_cipointers to classes (including exceptions, but excluding static types) 1847db96d56Sopenharmony_ciand settings (e.g. ``csv``'s :py:data:`~csv.field_size_limit`) 1857db96d56Sopenharmony_ciwhich the C code needs to function. 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci.. note:: 1887db96d56Sopenharmony_ci Another option is to store state in the module's ``__dict__``, 1897db96d56Sopenharmony_ci but you must avoid crashing when users modify ``__dict__`` from 1907db96d56Sopenharmony_ci Python code. This usually means error- and type-checking at the C level, 1917db96d56Sopenharmony_ci which is easy to get wrong and hard to test sufficiently. 1927db96d56Sopenharmony_ci 1937db96d56Sopenharmony_ci However, if module state is not needed in C code, storing it in 1947db96d56Sopenharmony_ci ``__dict__`` only is a good idea. 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_ciIf the module state includes ``PyObject`` pointers, the module object 1977db96d56Sopenharmony_cimust hold references to those objects and implement the module-level hooks 1987db96d56Sopenharmony_ci``m_traverse``, ``m_clear`` and ``m_free``. These work like 1997db96d56Sopenharmony_ci``tp_traverse``, ``tp_clear`` and ``tp_free`` of a class. Adding them will 2007db96d56Sopenharmony_cirequire some work and make the code longer; this is the price for 2017db96d56Sopenharmony_cimodules which can be unloaded cleanly. 2027db96d56Sopenharmony_ci 2037db96d56Sopenharmony_ciAn example of a module with per-module state is currently available as 2047db96d56Sopenharmony_ci`xxlimited <https://github.com/python/cpython/blob/master/Modules/xxlimited.c>`__; 2057db96d56Sopenharmony_ciexample module initialization shown at the bottom of the file. 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci 2087db96d56Sopenharmony_ciOpt-Out: Limiting to One Module Object per Process 2097db96d56Sopenharmony_ci-------------------------------------------------- 2107db96d56Sopenharmony_ci 2117db96d56Sopenharmony_ciA non-negative ``PyModuleDef.m_size`` signals that a module supports 2127db96d56Sopenharmony_cimultiple interpreters correctly. If this is not yet the case for your 2137db96d56Sopenharmony_cimodule, you can explicitly make your module loadable only once per 2147db96d56Sopenharmony_ciprocess. For example:: 2157db96d56Sopenharmony_ci 2167db96d56Sopenharmony_ci static int loaded = 0; 2177db96d56Sopenharmony_ci 2187db96d56Sopenharmony_ci static int 2197db96d56Sopenharmony_ci exec_module(PyObject* module) 2207db96d56Sopenharmony_ci { 2217db96d56Sopenharmony_ci if (loaded) { 2227db96d56Sopenharmony_ci PyErr_SetString(PyExc_ImportError, 2237db96d56Sopenharmony_ci "cannot load module more than once per process"); 2247db96d56Sopenharmony_ci return -1; 2257db96d56Sopenharmony_ci } 2267db96d56Sopenharmony_ci loaded = 1; 2277db96d56Sopenharmony_ci // ... rest of initialization 2287db96d56Sopenharmony_ci } 2297db96d56Sopenharmony_ci 2307db96d56Sopenharmony_ci 2317db96d56Sopenharmony_ciModule State Access from Functions 2327db96d56Sopenharmony_ci---------------------------------- 2337db96d56Sopenharmony_ci 2347db96d56Sopenharmony_ciAccessing the state from module-level functions is straightforward. 2357db96d56Sopenharmony_ciFunctions get the module object as their first argument; for extracting 2367db96d56Sopenharmony_cithe state, you can use ``PyModule_GetState``:: 2377db96d56Sopenharmony_ci 2387db96d56Sopenharmony_ci static PyObject * 2397db96d56Sopenharmony_ci func(PyObject *module, PyObject *args) 2407db96d56Sopenharmony_ci { 2417db96d56Sopenharmony_ci my_struct *state = (my_struct*)PyModule_GetState(module); 2427db96d56Sopenharmony_ci if (state == NULL) { 2437db96d56Sopenharmony_ci return NULL; 2447db96d56Sopenharmony_ci } 2457db96d56Sopenharmony_ci // ... rest of logic 2467db96d56Sopenharmony_ci } 2477db96d56Sopenharmony_ci 2487db96d56Sopenharmony_ci.. note:: 2497db96d56Sopenharmony_ci ``PyModule_GetState`` may return ``NULL`` without setting an 2507db96d56Sopenharmony_ci exception if there is no module state, i.e. ``PyModuleDef.m_size`` was 2517db96d56Sopenharmony_ci zero. In your own module, you're in control of ``m_size``, so this is 2527db96d56Sopenharmony_ci easy to prevent. 2537db96d56Sopenharmony_ci 2547db96d56Sopenharmony_ci 2557db96d56Sopenharmony_ciHeap Types 2567db96d56Sopenharmony_ci========== 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ciTraditionally, types defined in C code are *static*; that is, 2597db96d56Sopenharmony_ci``static PyTypeObject`` structures defined directly in code and 2607db96d56Sopenharmony_ciinitialized using ``PyType_Ready()``. 2617db96d56Sopenharmony_ci 2627db96d56Sopenharmony_ciSuch types are necessarily shared across the process. Sharing them 2637db96d56Sopenharmony_cibetween module objects requires paying attention to any state they own 2647db96d56Sopenharmony_cior access. To limit the possible issues, static types are immutable at 2657db96d56Sopenharmony_cithe Python level: for example, you can't set ``str.myattribute = 123``. 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci.. impl-detail:: 2687db96d56Sopenharmony_ci Sharing truly immutable objects between interpreters is fine, 2697db96d56Sopenharmony_ci as long as they don't provide access to mutable objects. 2707db96d56Sopenharmony_ci However, in CPython, every Python object has a mutable implementation 2717db96d56Sopenharmony_ci detail: the reference count. Changes to the refcount are guarded by the GIL. 2727db96d56Sopenharmony_ci Thus, code that shares any Python objects across interpreters implicitly 2737db96d56Sopenharmony_ci depends on CPython's current, process-wide GIL. 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ciBecause they are immutable and process-global, static types cannot access 2767db96d56Sopenharmony_ci"their" module state. 2777db96d56Sopenharmony_ciIf any method of such a type requires access to module state, 2787db96d56Sopenharmony_cithe type must be converted to a *heap-allocated type*, or *heap type* 2797db96d56Sopenharmony_cifor short. These correspond more closely to classes created by Python's 2807db96d56Sopenharmony_ci``class`` statement. 2817db96d56Sopenharmony_ci 2827db96d56Sopenharmony_ciFor new modules, using heap types by default is a good rule of thumb. 2837db96d56Sopenharmony_ci 2847db96d56Sopenharmony_ci 2857db96d56Sopenharmony_ciChanging Static Types to Heap Types 2867db96d56Sopenharmony_ci----------------------------------- 2877db96d56Sopenharmony_ci 2887db96d56Sopenharmony_ciStatic types can be converted to heap types, but note that 2897db96d56Sopenharmony_cithe heap type API was not designed for "lossless" conversion 2907db96d56Sopenharmony_cifrom static types—that is, creating a type that works exactly like a given 2917db96d56Sopenharmony_cistatic type. 2927db96d56Sopenharmony_ciSo, when rewriting the class definition in a new API, 2937db96d56Sopenharmony_ciyou are likely to unintentionally change a few details (e.g. pickleability 2947db96d56Sopenharmony_cior inherited slots). 2957db96d56Sopenharmony_ciAlways test the details that are important to you. 2967db96d56Sopenharmony_ci 2977db96d56Sopenharmony_ciWatch out for the following two points in particular (but note that this is not 2987db96d56Sopenharmony_cia comprehensive list): 2997db96d56Sopenharmony_ci 3007db96d56Sopenharmony_ci* Unlike static types, heap type objects are mutable by default. 3017db96d56Sopenharmony_ci Use the :c:data:`Py_TPFLAGS_IMMUTABLETYPE` flag to prevent mutability. 3027db96d56Sopenharmony_ci* Heap types inherit :c:member:`~PyTypeObject.tp_new` by default, 3037db96d56Sopenharmony_ci so it may become possible to instantiate them from Python code. 3047db96d56Sopenharmony_ci You can prevent this with the :c:data:`Py_TPFLAGS_DISALLOW_INSTANTIATION` flag. 3057db96d56Sopenharmony_ci 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ciDefining Heap Types 3087db96d56Sopenharmony_ci------------------- 3097db96d56Sopenharmony_ci 3107db96d56Sopenharmony_ciHeap types can be created by filling a :c:struct:`PyType_Spec` structure, a 3117db96d56Sopenharmony_cidescription or "blueprint" of a class, and calling 3127db96d56Sopenharmony_ci:c:func:`PyType_FromModuleAndSpec` to construct a new class object. 3137db96d56Sopenharmony_ci 3147db96d56Sopenharmony_ci.. note:: 3157db96d56Sopenharmony_ci Other functions, like :c:func:`PyType_FromSpec`, can also create 3167db96d56Sopenharmony_ci heap types, but :c:func:`PyType_FromModuleAndSpec` associates the module 3177db96d56Sopenharmony_ci with the class, allowing access to the module state from methods. 3187db96d56Sopenharmony_ci 3197db96d56Sopenharmony_ciThe class should generally be stored in *both* the module state (for 3207db96d56Sopenharmony_cisafe access from C) and the module's ``__dict__`` (for access from 3217db96d56Sopenharmony_ciPython code). 3227db96d56Sopenharmony_ci 3237db96d56Sopenharmony_ci 3247db96d56Sopenharmony_ciGarbage-Collection Protocol 3257db96d56Sopenharmony_ci--------------------------- 3267db96d56Sopenharmony_ci 3277db96d56Sopenharmony_ciInstances of heap types hold a reference to their type. 3287db96d56Sopenharmony_ciThis ensures that the type isn't destroyed before all its instances are, 3297db96d56Sopenharmony_cibut may result in reference cycles that need to be broken by the 3307db96d56Sopenharmony_cigarbage collector. 3317db96d56Sopenharmony_ci 3327db96d56Sopenharmony_ciTo avoid memory leaks, instances of heap types must implement the 3337db96d56Sopenharmony_cigarbage collection protocol. 3347db96d56Sopenharmony_ciThat is, heap types should: 3357db96d56Sopenharmony_ci 3367db96d56Sopenharmony_ci- Have the :c:data:`Py_TPFLAGS_HAVE_GC` flag. 3377db96d56Sopenharmony_ci- Define a traverse function using ``Py_tp_traverse``, which 3387db96d56Sopenharmony_ci visits the type (e.g. using :c:expr:`Py_VISIT(Py_TYPE(self))`). 3397db96d56Sopenharmony_ci 3407db96d56Sopenharmony_ciPlease refer to the :ref:`the documentation <type-structs>` of 3417db96d56Sopenharmony_ci:c:data:`Py_TPFLAGS_HAVE_GC` and :c:member:`~PyTypeObject.tp_traverse` 3427db96d56Sopenharmony_cifor additional considerations. 3437db96d56Sopenharmony_ci 3447db96d56Sopenharmony_ciIf your traverse function delegates to the ``tp_traverse`` of its base class 3457db96d56Sopenharmony_ci(or another type), ensure that ``Py_TYPE(self)`` is visited only once. 3467db96d56Sopenharmony_ciNote that only heap type are expected to visit the type in ``tp_traverse``. 3477db96d56Sopenharmony_ci 3487db96d56Sopenharmony_ciFor example, if your traverse function includes:: 3497db96d56Sopenharmony_ci 3507db96d56Sopenharmony_ci base->tp_traverse(self, visit, arg) 3517db96d56Sopenharmony_ci 3527db96d56Sopenharmony_ci...and ``base`` may be a static type, then it should also include:: 3537db96d56Sopenharmony_ci 3547db96d56Sopenharmony_ci if (base->tp_flags & Py_TPFLAGS_HEAPTYPE) { 3557db96d56Sopenharmony_ci // a heap type's tp_traverse already visited Py_TYPE(self) 3567db96d56Sopenharmony_ci } else { 3577db96d56Sopenharmony_ci Py_VISIT(Py_TYPE(self)); 3587db96d56Sopenharmony_ci } 3597db96d56Sopenharmony_ci 3607db96d56Sopenharmony_ciIt is not necessary to handle the type's reference count in ``tp_new`` 3617db96d56Sopenharmony_ciand ``tp_clear``. 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci 3647db96d56Sopenharmony_ciModule State Access from Classes 3657db96d56Sopenharmony_ci-------------------------------- 3667db96d56Sopenharmony_ci 3677db96d56Sopenharmony_ciIf you have a type object defined with :c:func:`PyType_FromModuleAndSpec`, 3687db96d56Sopenharmony_ciyou can call :c:func:`PyType_GetModule` to get the associated module, and then 3697db96d56Sopenharmony_ci:c:func:`PyModule_GetState` to get the module's state. 3707db96d56Sopenharmony_ci 3717db96d56Sopenharmony_ciTo save a some tedious error-handling boilerplate code, you can combine 3727db96d56Sopenharmony_cithese two steps with :c:func:`PyType_GetModuleState`, resulting in:: 3737db96d56Sopenharmony_ci 3747db96d56Sopenharmony_ci my_struct *state = (my_struct*)PyType_GetModuleState(type); 3757db96d56Sopenharmony_ci if (state === NULL) { 3767db96d56Sopenharmony_ci return NULL; 3777db96d56Sopenharmony_ci } 3787db96d56Sopenharmony_ci 3797db96d56Sopenharmony_ci 3807db96d56Sopenharmony_ciModule State Access from Regular Methods 3817db96d56Sopenharmony_ci---------------------------------------- 3827db96d56Sopenharmony_ci 3837db96d56Sopenharmony_ciAccessing the module-level state from methods of a class is somewhat more 3847db96d56Sopenharmony_cicomplicated, but is possible thanks to API introduced in Python 3.9. 3857db96d56Sopenharmony_ciTo get the state, you need to first get the *defining class*, and then 3867db96d56Sopenharmony_ciget the module state from it. 3877db96d56Sopenharmony_ci 3887db96d56Sopenharmony_ciThe largest roadblock is getting *the class a method was defined in*, or 3897db96d56Sopenharmony_cithat method's "defining class" for short. The defining class can have a 3907db96d56Sopenharmony_cireference to the module it is part of. 3917db96d56Sopenharmony_ci 3927db96d56Sopenharmony_ciDo not confuse the defining class with :c:expr:`Py_TYPE(self)`. If the method 3937db96d56Sopenharmony_ciis called on a *subclass* of your type, ``Py_TYPE(self)`` will refer to 3947db96d56Sopenharmony_cithat subclass, which may be defined in different module than yours. 3957db96d56Sopenharmony_ci 3967db96d56Sopenharmony_ci.. note:: 3977db96d56Sopenharmony_ci The following Python code can illustrate the concept. 3987db96d56Sopenharmony_ci ``Base.get_defining_class`` returns ``Base`` even 3997db96d56Sopenharmony_ci if ``type(self) == Sub``: 4007db96d56Sopenharmony_ci 4017db96d56Sopenharmony_ci .. code-block:: python 4027db96d56Sopenharmony_ci 4037db96d56Sopenharmony_ci class Base: 4047db96d56Sopenharmony_ci def get_type_of_self(self): 4057db96d56Sopenharmony_ci return type(self) 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ci def get_defining_class(self): 4087db96d56Sopenharmony_ci return __class__ 4097db96d56Sopenharmony_ci 4107db96d56Sopenharmony_ci class Sub(Base): 4117db96d56Sopenharmony_ci pass 4127db96d56Sopenharmony_ci 4137db96d56Sopenharmony_ciFor a method to get its "defining class", it must use the 4147db96d56Sopenharmony_ci:data:`METH_METHOD | METH_FASTCALL | METH_KEYWORDS` 4157db96d56Sopenharmony_ci:c:type:`calling convention <PyMethodDef>` 4167db96d56Sopenharmony_ciand the corresponding :c:type:`PyCMethod` signature:: 4177db96d56Sopenharmony_ci 4187db96d56Sopenharmony_ci PyObject *PyCMethod( 4197db96d56Sopenharmony_ci PyObject *self, // object the method was called on 4207db96d56Sopenharmony_ci PyTypeObject *defining_class, // defining class 4217db96d56Sopenharmony_ci PyObject *const *args, // C array of arguments 4227db96d56Sopenharmony_ci Py_ssize_t nargs, // length of "args" 4237db96d56Sopenharmony_ci PyObject *kwnames) // NULL, or dict of keyword arguments 4247db96d56Sopenharmony_ci 4257db96d56Sopenharmony_ciOnce you have the defining class, call :c:func:`PyType_GetModuleState` to get 4267db96d56Sopenharmony_cithe state of its associated module. 4277db96d56Sopenharmony_ci 4287db96d56Sopenharmony_ciFor example:: 4297db96d56Sopenharmony_ci 4307db96d56Sopenharmony_ci static PyObject * 4317db96d56Sopenharmony_ci example_method(PyObject *self, 4327db96d56Sopenharmony_ci PyTypeObject *defining_class, 4337db96d56Sopenharmony_ci PyObject *const *args, 4347db96d56Sopenharmony_ci Py_ssize_t nargs, 4357db96d56Sopenharmony_ci PyObject *kwnames) 4367db96d56Sopenharmony_ci { 4377db96d56Sopenharmony_ci my_struct *state = (my_struct*)PyType_GetModuleState(defining_class); 4387db96d56Sopenharmony_ci if (state === NULL) { 4397db96d56Sopenharmony_ci return NULL; 4407db96d56Sopenharmony_ci } 4417db96d56Sopenharmony_ci ... // rest of logic 4427db96d56Sopenharmony_ci } 4437db96d56Sopenharmony_ci 4447db96d56Sopenharmony_ci PyDoc_STRVAR(example_method_doc, "..."); 4457db96d56Sopenharmony_ci 4467db96d56Sopenharmony_ci static PyMethodDef my_methods[] = { 4477db96d56Sopenharmony_ci {"example_method", 4487db96d56Sopenharmony_ci (PyCFunction)(void(*)(void))example_method, 4497db96d56Sopenharmony_ci METH_METHOD|METH_FASTCALL|METH_KEYWORDS, 4507db96d56Sopenharmony_ci example_method_doc} 4517db96d56Sopenharmony_ci {NULL}, 4527db96d56Sopenharmony_ci } 4537db96d56Sopenharmony_ci 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ciModule State Access from Slot Methods, Getters and Setters 4567db96d56Sopenharmony_ci---------------------------------------------------------- 4577db96d56Sopenharmony_ci 4587db96d56Sopenharmony_ci.. note:: 4597db96d56Sopenharmony_ci 4607db96d56Sopenharmony_ci This is new in Python 3.11. 4617db96d56Sopenharmony_ci 4627db96d56Sopenharmony_ci .. After adding to limited API: 4637db96d56Sopenharmony_ci 4647db96d56Sopenharmony_ci If you use the :ref:`limited API <stable>, 4657db96d56Sopenharmony_ci you must update ``Py_LIMITED_API`` to ``0x030b0000``, losing ABI 4667db96d56Sopenharmony_ci compatibility with earlier versions. 4677db96d56Sopenharmony_ci 4687db96d56Sopenharmony_ciSlot methods—the fast C equivalents for special methods, such as 4697db96d56Sopenharmony_ci:c:member:`~PyNumberMethods.nb_add` for :py:attr:`~object.__add__` or 4707db96d56Sopenharmony_ci:c:member:`~PyType.tp_new` for initialization—have a very simple API that 4717db96d56Sopenharmony_cidoesn't allow passing in the defining class, unlike with :c:type:`PyCMethod`. 4727db96d56Sopenharmony_ciThe same goes for getters and setters defined with 4737db96d56Sopenharmony_ci:c:type:`PyGetSetDef`. 4747db96d56Sopenharmony_ci 4757db96d56Sopenharmony_ciTo access the module state in these cases, use the 4767db96d56Sopenharmony_ci:c:func:`PyType_GetModuleByDef` function, and pass in the module definition. 4777db96d56Sopenharmony_ciOnce you have the module, call :c:func:`PyModule_GetState` 4787db96d56Sopenharmony_cito get the state:: 4797db96d56Sopenharmony_ci 4807db96d56Sopenharmony_ci PyObject *module = PyType_GetModuleByDef(Py_TYPE(self), &module_def); 4817db96d56Sopenharmony_ci my_struct *state = (my_struct*)PyModule_GetState(module); 4827db96d56Sopenharmony_ci if (state === NULL) { 4837db96d56Sopenharmony_ci return NULL; 4847db96d56Sopenharmony_ci } 4857db96d56Sopenharmony_ci 4867db96d56Sopenharmony_ci``PyType_GetModuleByDef`` works by searching the 4877db96d56Sopenharmony_ci:term:`method resolution order` (i.e. all superclasses) for the first 4887db96d56Sopenharmony_cisuperclass that has a corresponding module. 4897db96d56Sopenharmony_ci 4907db96d56Sopenharmony_ci.. note:: 4917db96d56Sopenharmony_ci 4927db96d56Sopenharmony_ci In very exotic cases (inheritance chains spanning multiple modules 4937db96d56Sopenharmony_ci created from the same definition), ``PyType_GetModuleByDef`` might not 4947db96d56Sopenharmony_ci return the module of the true defining class. However, it will always 4957db96d56Sopenharmony_ci return a module with the same definition, ensuring a compatible 4967db96d56Sopenharmony_ci C memory layout. 4977db96d56Sopenharmony_ci 4987db96d56Sopenharmony_ci 4997db96d56Sopenharmony_ciLifetime of the Module State 5007db96d56Sopenharmony_ci---------------------------- 5017db96d56Sopenharmony_ci 5027db96d56Sopenharmony_ciWhen a module object is garbage-collected, its module state is freed. 5037db96d56Sopenharmony_ciFor each pointer to (a part of) the module state, you must hold a reference 5047db96d56Sopenharmony_cito the module object. 5057db96d56Sopenharmony_ci 5067db96d56Sopenharmony_ciUsually this is not an issue, because types created with 5077db96d56Sopenharmony_ci:c:func:`PyType_FromModuleAndSpec`, and their instances, hold a reference 5087db96d56Sopenharmony_cito the module. 5097db96d56Sopenharmony_ciHowever, you must be careful in reference counting when you reference 5107db96d56Sopenharmony_cimodule state from other places, such as callbacks for external 5117db96d56Sopenharmony_cilibraries. 5127db96d56Sopenharmony_ci 5137db96d56Sopenharmony_ci 5147db96d56Sopenharmony_ciOpen Issues 5157db96d56Sopenharmony_ci=========== 5167db96d56Sopenharmony_ci 5177db96d56Sopenharmony_ciSeveral issues around per-module state and heap types are still open. 5187db96d56Sopenharmony_ci 5197db96d56Sopenharmony_ciDiscussions about improving the situation are best held on the `capi-sig 5207db96d56Sopenharmony_cimailing list <https://mail.python.org/mailman3/lists/capi-sig.python.org/>`__. 5217db96d56Sopenharmony_ci 5227db96d56Sopenharmony_ci 5237db96d56Sopenharmony_ciPer-Class Scope 5247db96d56Sopenharmony_ci--------------- 5257db96d56Sopenharmony_ci 5267db96d56Sopenharmony_ciIt is currently (as of Python 3.11) not possible to attach state to individual 5277db96d56Sopenharmony_ci*types* without relying on CPython implementation details (which may change 5287db96d56Sopenharmony_ciin the future—perhaps, ironically, to allow a proper solution for 5297db96d56Sopenharmony_ciper-class scope). 5307db96d56Sopenharmony_ci 5317db96d56Sopenharmony_ci 5327db96d56Sopenharmony_ciLossless Conversion to Heap Types 5337db96d56Sopenharmony_ci--------------------------------- 5347db96d56Sopenharmony_ci 5357db96d56Sopenharmony_ciThe heap type API was not designed for "lossless" conversion from static types; 5367db96d56Sopenharmony_cithat is, creating a type that works exactly like a given static type. 537