@@ -7,8 +7,6 @@ Module Objects
77
88.. index :: object: module
99
10- There are only a few functions special to module objects.
11-
1210
1311.. c :var :: PyTypeObject PyModule_Type
1412
@@ -109,6 +107,14 @@ There are only a few functions special to module objects.
109107 unencodable filenames, use :c:func: `PyModule_GetFilenameObject ` instead.
110108
111109
110+ Per-interpreter module state
111+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112+
113+ Single-phase initialization creates singleton modules that can store additional
114+ information as part of the interpreter, allow that state to be retrieved later
115+ with only a reference to the module definition, rather than to the module
116+ itself.
117+
112118.. c :function :: void * PyModule_GetState (PyObject *module)
113119
114120 Return the "state" of the module, that is, a pointer to the block of memory
@@ -146,27 +152,6 @@ There are only a few functions special to module objects.
146152Initializing C modules
147153^^^^^^^^^^^^^^^^^^^^^^
148154
149- These functions are usually used in the module initialization function.
150-
151- .. c :function :: PyObject* PyModule_Create (PyModuleDef *module)
152-
153- Create a new module object, given the definition in *module *. This behaves
154- like :c:func: `PyModule_Create2 ` with *module_api_version * set to
155- :const: `PYTHON_API_VERSION `.
156-
157-
158- .. c :function :: PyObject* PyModule_Create2 (PyModuleDef *module, int module_api_version)
159-
160- Create a new module object, given the definition in *module *, assuming the
161- API version *module_api_version *. If that version does not match the version
162- of the running interpreter, a :exc: `RuntimeWarning ` is emitted.
163-
164- .. note ::
165-
166- Most uses of this function should be using :c:func: `PyModule_Create `
167- instead; only use this if you are sure you need it.
168-
169-
170155.. c :type :: PyModuleDef
171156
172157 This struct holds all information that is needed to create a module object.
@@ -210,9 +195,10 @@ These functions are usually used in the module initialization function.
210195 A pointer to a table of module-level functions, described by
211196 :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present.
212197
213- .. c:member:: inquiry m_reload
198+ .. c:member:: PyModuleDef_Slot* m_slots
214199
215- Currently unused, should be *NULL*.
200+ An array of slot definitions for multi-phase initialization, terminated by
201+ a *NULL* entry.
216202
217203 .. c:member:: traverseproc m_traverse
218204
@@ -229,14 +215,68 @@ These functions are usually used in the module initialization function.
229215 A function to call during deallocation of the module object, or *NULL* if
230216 not needed.
231217
218+ The module initialization function may create and return the module object
219+ directly. This is referred to as "single-phase initialization", and uses one
220+ of the following two module creation functions:
221+
222+ .. c:function:: PyObject* PyModule_Create(PyModuleDef *module)
223+
224+ Create a new module object, given the definition in *module *. This behaves
225+ like :c:func: `PyModule_Create2 ` with *module_api_version * set to
226+ :const: `PYTHON_API_VERSION `.
227+
228+
229+ .. c :function :: PyObject* PyModule_Create2 (PyModuleDef *module, int module_api_version)
230+
231+ Create a new module object, given the definition in *module *, assuming the
232+ API version *module_api_version *. If that version does not match the version
233+ of the running interpreter, a :exc: `RuntimeWarning ` is emitted.
234+
235+ .. note ::
236+
237+ Most uses of this function should be using :c:func: `PyModule_Create `
238+ instead; only use this if you are sure you need it.
239+
240+
241+ Alternatively, the module initialization function may instead return a
242+ :c:type: `PyModuleDef ` instance with a non-empty ``m_slots `` array. This is
243+ referred to as "multi-phase initialization", and ``PyModuleDef `` instance
244+ should be initialized with the following function:
245+
246+ .. c :function :: PyObject* PyModuleDef_Init (PyModuleDef *module)
247+
248+ Ensures a module definition is a properly initialized Python object that
249+ correctly reports its type and reference count.
250+
251+ .. XXX (ncoghlan): It's not clear if it makes sense to document PyModule_ExecDef
252+ PyModule_FromDefAndSpec or PyModule_FromDefAndSpec2 here, as end user code
253+ generally shouldn't be calling those.
254+
255+ The module initialization function (if using single phase initialization) or
256+ a function called from a module execution slot (if using multiphase
257+ initialization), can use the following functions to help initialize the module
258+ state:
259+
260+ .. c:function:: int PyModule_SetDocString(PyObject *module, const char *docstring)
261+
262+ Set the docstring for *module * to *docstring *. Return ``-1 `` on error, ``0 ``
263+ on success.
264+
265+ .. c :function :: int PyModule_AddFunctions (PyObject *module, PyMethodDef *functions)
266+
267+ Add the functions from the ``NULL `` terminated *functions * array to *module *.
268+ Refer to the :c:type: `PyMethodDef ` documentation for details on individual
269+ entries (due to the lack of a shared module namespace, module level
270+ "functions" implemented in C typically receive the module as their first
271+ parameter, making them similar to instance methods on Python classes).
272+
232273
233274.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
234275
235276 Add an object to *module * as *name *. This is a convenience function which can
236277 be used from the module's initialization function. This steals a reference to
237278 *value *. Return ``-1 `` on error, ``0 `` on success.
238279
239-
240280.. c :function :: int PyModule_AddIntConstant (PyObject *module, const char *name, long value)
241281
242282 Add an integer constant to *module * as *name *. This convenience function can be
0 commit comments