Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e69cdf9

Browse files
committed
#4614: document PyModule_Create and PyModuleDef struct.
1 parent 42db3ef commit e69cdf9

2 files changed

Lines changed: 102 additions & 33 deletions

File tree

Doc/c-api/allocation.rst

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,40 +54,15 @@ Allocating Objects on the Heap
5454
accessed after this call as the memory is no longer a valid Python object.
5555

5656

57-
.. cfunction:: PyObject* Py_InitModule(char *name, PyMethodDef *methods)
58-
59-
Create a new module object based on a name and table of functions, returning
60-
the new module object; the *methods* argument can be *NULL* if no methods are
61-
to be defined for the module.
62-
63-
64-
.. cfunction:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
65-
66-
Create a new module object based on a name and table of functions, returning
67-
the new module object. The *methods* argument can be *NULL* if no methods
68-
are to be defined for the module. If *doc* is non-*NULL*, it will be used to
69-
define the docstring for the module.
70-
71-
72-
.. cfunction:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
73-
74-
Create a new module object based on a name and table of functions, returning
75-
the new module object. The *methods* argument can be *NULL* if no methods
76-
are to be defined for the module. If *doc* is non-*NULL*, it will be used to
77-
define the docstring for the module. If *self* is non-*NULL*, it will passed
78-
to the functions of the module as their (otherwise *NULL*) first parameter.
79-
(This was added as an experimental feature, and there are no known uses in
80-
the current version of Python.) For *apiver*, the only value which should be
81-
passed is defined by the constant :const:`PYTHON_API_VERSION`.
82-
83-
.. note::
84-
85-
Most uses of this function should probably be using the :cfunc:`Py_InitModule3`
86-
instead; only use this if you are sure you need it.
87-
88-
8957
.. cvar:: PyObject _Py_NoneStruct
9058

9159
Object which is visible in Python as ``None``. This should only be accessed
9260
using the :cmacro:`Py_None` macro, which evaluates to a pointer to this
9361
object.
62+
63+
64+
.. seealso::
65+
66+
:cfunc:`PyModule_Create`
67+
To allocate and create extension modules.
68+

Doc/c-api/module.rst

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,101 @@ There are only a few functions special to module objects.
7373
raise :exc:`SystemError` and return *NULL*.
7474

7575

76+
.. cfunction:: void* PyModule_GetState(PyObject *module)
77+
78+
Return the "state" of the module, that is, a pointer to the block of memory
79+
allocated at module creation time, or *NULL*. See
80+
:cmember:`PyModuleDef.m_size`.
81+
82+
83+
.. cfunction:: PyModuleDef* PyModule_GetDef(PyObject *module)
84+
85+
Return a pointer to the :ctype:`PyModuleDef` struct from which the module was
86+
created, or *NULL* if the module wasn't created with
87+
:cfunc:`PyModule_Create`.
88+
89+
90+
Initializing C modules
91+
^^^^^^^^^^^^^^^^^^^^^^
92+
93+
These functions are usually used in the module initialization function.
94+
95+
.. cfunction:: PyObject* PyModule_Create(PyModuleDef *module)
96+
97+
Create a new module object, given the definition in *module*. This behaves
98+
like :cfunc:`PyModule_Create2` with *module_api_version* set to
99+
:const:`PYTHON_API_VERSION`.
100+
101+
102+
.. cfunction:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version)
103+
104+
Create a new module object, given the definition in *module*, assuming the
105+
API version *module_api_version*. If that version does not match the version
106+
of the running interpreter, a :exc:`RuntimeWarning` is emitted.
107+
108+
.. note::
109+
110+
Most uses of this function should be using :cfunc:`PyModule_Create`
111+
instead; only use this if you are sure you need it.
112+
113+
114+
.. ctype:: PyModuleDef
115+
116+
This struct holds all information that is needed to create a module object.
117+
There is usually only one static variable of that type for each module, which
118+
is statically initialized and then passed to :cfunc:`PyModule_Create` in the
119+
module initialization function.
120+
121+
.. cmember:: PyModuleDef_Base m_base
122+
123+
Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
124+
125+
.. cmember:: char* m_name
126+
127+
Name for the new module.
128+
129+
.. cmember:: char* m_doc
130+
131+
Docstring for the module; usually a docstring variable created with
132+
:cfunc:`PyDoc_STRVAR` is used.
133+
134+
.. cmember:: Py_ssize_t m_size
135+
136+
If the module object needs additional memory, this should be set to the
137+
number of bytes to allocate; a pointer to the block of memory can be
138+
retrieved with :cfunc:`PyModule_GetState`. If no memory is needed, set
139+
this to ``-1``.
140+
141+
This memory should be used, rather than static globals, to hold per-module
142+
state, since it is then safe for use in multiple sub-interpreters. It is
143+
freed when the module object is deallocated, after the :cmember:`m_free`
144+
function has been called, if present.
145+
146+
.. cmember:: PyMethodDef* m_methods
147+
148+
A pointer to a table of module-level functions, described by
149+
:ctype:`PyMethodDef` values. Can be *NULL* if no functions are present.
150+
151+
.. cmember:: inquiry m_reload
152+
153+
Currently unused, should be *NULL*.
154+
155+
.. cmember:: traverseproc m_traverse
156+
157+
A traversal function to call during GC traversal of the module object, or
158+
*NULL* if not needed.
159+
160+
.. cmember:: inquiry m_clear
161+
162+
A clear function to call during GC clearing of the module object, or
163+
*NULL* if not needed.
164+
165+
.. cmember:: freefunc m_free
166+
167+
A function to call during deallocation of the module object, or *NULL* if
168+
not needed.
169+
170+
76171
.. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
77172

78173
Add an object to *module* as *name*. This is a convenience function which can
@@ -105,4 +200,3 @@ There are only a few functions special to module objects.
105200
.. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro)
106201

107202
Add a string constant to *module*.
108-

0 commit comments

Comments
 (0)