blob: df9301f987b4df1e893eab1b47962566f9fb7d95 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _moduleobjects:
4
5Module Objects
6--------------
7
8.. index:: object: module
9
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:var:: PyTypeObject PyModule_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000012
13 .. index:: single: ModuleType (in module types)
14
Georg Brandl60203b42010-10-06 10:11:56 +000015 This instance of :c:type:`PyTypeObject` represents the Python module type. This
Georg Brandl54a3faa2008-01-20 09:30:57 +000016 is exposed to Python programs as ``types.ModuleType``.
17
18
Georg Brandl60203b42010-10-06 10:11:56 +000019.. c:function:: int PyModule_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000020
21 Return true if *p* is a module object, or a subtype of a module object.
22
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: int PyModule_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
26 Return true if *p* is a module object, but not a subtype of
Georg Brandl60203b42010-10-06 10:11:56 +000027 :c:data:`PyModule_Type`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000028
29
Victor Stinner0639b562011-03-04 12:57:07 +000030.. c:function:: PyObject* PyModule_NewObject(PyObject *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
32 .. index::
33 single: __name__ (module attribute)
34 single: __doc__ (module attribute)
35 single: __file__ (module attribute)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040036 single: __package__ (module attribute)
37 single: __loader__ (module attribute)
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
39 Return a new module object with the :attr:`__name__` attribute set to *name*.
Brett Cannon4c14b5d2013-05-04 13:56:58 -040040 The module's :attr:`__name__`, :attr:`__doc__`, :attr:`__package__`, and
41 :attr:`__loader__` attributes are filled in (all but :attr:`__name__` are set
42 to ``None``); the caller is responsible for providing a :attr:`__file__`
43 attribute.
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
Victor Stinner0639b562011-03-04 12:57:07 +000045 .. versionadded:: 3.3
46
Brett Cannon4c14b5d2013-05-04 13:56:58 -040047 .. versionchanged:: 3.4
48 :attr:`__package__` and :attr:`__loader__` are set to ``None``.
49
Victor Stinner0639b562011-03-04 12:57:07 +000050
51.. c:function:: PyObject* PyModule_New(const char *name)
52
53 Similar to :c:func:`PyImport_NewObject`, but the name is an UTF-8 encoded
54 string instead of a Unicode object.
55
Georg Brandl54a3faa2008-01-20 09:30:57 +000056
Georg Brandl60203b42010-10-06 10:11:56 +000057.. c:function:: PyObject* PyModule_GetDict(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59 .. index:: single: __dict__ (module attribute)
60
61 Return the dictionary object that implements *module*'s namespace; this object
62 is the same as the :attr:`__dict__` attribute of the module object. This
63 function never fails. It is recommended extensions use other
Georg Brandl60203b42010-10-06 10:11:56 +000064 :c:func:`PyModule_\*` and :c:func:`PyObject_\*` functions rather than directly
Georg Brandl54a3faa2008-01-20 09:30:57 +000065 manipulate a module's :attr:`__dict__`.
66
67
Victor Stinnerbd475112011-02-23 00:21:43 +000068.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
70 .. index::
71 single: __name__ (module attribute)
72 single: SystemError (built-in exception)
73
74 Return *module*'s :attr:`__name__` value. If the module does not provide one,
75 or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
76
Victor Stinnerbd475112011-02-23 00:21:43 +000077 .. versionadded:: 3.3
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Georg Brandl54a3faa2008-01-20 09:30:57 +000079
Victor Stinnerbd475112011-02-23 00:21:43 +000080.. c:function:: char* PyModule_GetName(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000081
Victor Stinnerbd475112011-02-23 00:21:43 +000082 Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to
83 ``'utf-8'``.
Victor Stinner6c00c142010-08-17 23:37:11 +000084
85
Georg Brandl60203b42010-10-06 10:11:56 +000086.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000087
Georg Brandl54a3faa2008-01-20 09:30:57 +000088 .. index::
89 single: __file__ (module attribute)
90 single: SystemError (built-in exception)
91
92 Return the name of the file from which *module* was loaded using *module*'s
Victor Stinner6c00c142010-08-17 23:37:11 +000093 :attr:`__file__` attribute. If this is not defined, or if it is not a
94 unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return
Georg Brandldb6c7f52011-10-07 11:19:11 +020095 a reference to a Unicode object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000096
Victor Stinnerc14190d2010-08-18 10:57:33 +000097 .. versionadded:: 3.2
98
Georg Brandl54a3faa2008-01-20 09:30:57 +000099
Victor Stinnerbd475112011-02-23 00:21:43 +0000100.. c:function:: char* PyModule_GetFilename(PyObject *module)
101
102 Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
103 encoded to 'utf-8'.
104
105 .. deprecated:: 3.2
106 :c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
107 unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
108
109
Nick Coghland5cacbb2015-05-23 22:24:10 +1000110Per-interpreter module state
111^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112
113Single-phase initialization creates singleton modules that can store additional
114information as part of the interpreter, allow that state to be retrieved later
115with only a reference to the module definition, rather than to the module
116itself.
117
Georg Brandl60203b42010-10-06 10:11:56 +0000118.. c:function:: void* PyModule_GetState(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000119
120 Return the "state" of the module, that is, a pointer to the block of memory
121 allocated at module creation time, or *NULL*. See
Georg Brandl60203b42010-10-06 10:11:56 +0000122 :c:member:`PyModuleDef.m_size`.
Georg Brandle69cdf92009-01-04 23:20:14 +0000123
124
Georg Brandl60203b42010-10-06 10:11:56 +0000125.. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000126
Georg Brandl60203b42010-10-06 10:11:56 +0000127 Return a pointer to the :c:type:`PyModuleDef` struct from which the module was
Georg Brandle69cdf92009-01-04 23:20:14 +0000128 created, or *NULL* if the module wasn't created with
Zachary Ware7bbd1012014-02-26 10:40:38 -0600129 :c:func:`PyModule_Create`.
Georg Brandle69cdf92009-01-04 23:20:14 +0000130
Martin v. Löwis7800f752012-06-22 12:20:55 +0200131.. c:function:: PyObject* PyState_FindModule(PyModuleDef *def)
132
133 Returns the module object that was created from *def* for the current interpreter.
134 This method requires that the module object has been attached to the interpreter state with
135 :c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not
136 found or has not been attached to the interpreter state yet, it returns NULL.
137
Ezio Melotti32b0f022013-03-06 02:57:25 +0200138.. c:function:: int PyState_AddModule(PyObject *module, PyModuleDef *def)
Martin v. Löwis7800f752012-06-22 12:20:55 +0200139
140 Attaches the module object passed to the function to the interpreter state. This allows
141 the module object to be accessible via
142 :c:func:`PyState_FindModule`.
143
144 .. versionadded:: 3.3
145
Ezio Melotti32b0f022013-03-06 02:57:25 +0200146.. c:function:: int PyState_RemoveModule(PyModuleDef *def)
Martin v. Löwis7800f752012-06-22 12:20:55 +0200147
148 Removes the module object created from *def* from the interpreter state.
149
150 .. versionadded:: 3.3
Georg Brandle69cdf92009-01-04 23:20:14 +0000151
152Initializing C modules
153^^^^^^^^^^^^^^^^^^^^^^
154
Georg Brandl60203b42010-10-06 10:11:56 +0000155.. c:type:: PyModuleDef
Georg Brandle69cdf92009-01-04 23:20:14 +0000156
157 This struct holds all information that is needed to create a module object.
158 There is usually only one static variable of that type for each module, which
Georg Brandl60203b42010-10-06 10:11:56 +0000159 is statically initialized and then passed to :c:func:`PyModule_Create` in the
Georg Brandle69cdf92009-01-04 23:20:14 +0000160 module initialization function.
161
Georg Brandl60203b42010-10-06 10:11:56 +0000162 .. c:member:: PyModuleDef_Base m_base
Georg Brandle69cdf92009-01-04 23:20:14 +0000163
164 Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
165
Georg Brandl60203b42010-10-06 10:11:56 +0000166 .. c:member:: char* m_name
Georg Brandle69cdf92009-01-04 23:20:14 +0000167
168 Name for the new module.
169
Georg Brandl60203b42010-10-06 10:11:56 +0000170 .. c:member:: char* m_doc
Georg Brandle69cdf92009-01-04 23:20:14 +0000171
172 Docstring for the module; usually a docstring variable created with
Georg Brandl60203b42010-10-06 10:11:56 +0000173 :c:func:`PyDoc_STRVAR` is used.
Georg Brandle69cdf92009-01-04 23:20:14 +0000174
Georg Brandl60203b42010-10-06 10:11:56 +0000175 .. c:member:: Py_ssize_t m_size
Georg Brandle69cdf92009-01-04 23:20:14 +0000176
Eli Bendersky0d2d2b82013-08-07 05:52:20 -0700177 Some modules allow re-initialization (calling their ``PyInit_*`` function
178 more than once). These modules should keep their state in a per-module
179 memory area that can be retrieved with :c:func:`PyModule_GetState`.
Georg Brandle69cdf92009-01-04 23:20:14 +0000180
181 This memory should be used, rather than static globals, to hold per-module
182 state, since it is then safe for use in multiple sub-interpreters. It is
Georg Brandl60203b42010-10-06 10:11:56 +0000183 freed when the module object is deallocated, after the :c:member:`m_free`
Georg Brandle69cdf92009-01-04 23:20:14 +0000184 function has been called, if present.
185
Eli Bendersky43694a52013-08-10 05:57:27 -0700186 Setting ``m_size`` to ``-1`` means that the module can not be
187 re-initialized because it has global state. Setting it to a non-negative
188 value means that the module can be re-initialized and specifies the
189 additional amount of memory it requires for its state.
Eli Bendersky0d2d2b82013-08-07 05:52:20 -0700190
191 See :PEP:`3121` for more details.
192
Georg Brandl60203b42010-10-06 10:11:56 +0000193 .. c:member:: PyMethodDef* m_methods
Georg Brandle69cdf92009-01-04 23:20:14 +0000194
195 A pointer to a table of module-level functions, described by
Georg Brandl60203b42010-10-06 10:11:56 +0000196 :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present.
Georg Brandle69cdf92009-01-04 23:20:14 +0000197
Nick Coghland5cacbb2015-05-23 22:24:10 +1000198 .. c:member:: PyModuleDef_Slot* m_slots
Georg Brandle69cdf92009-01-04 23:20:14 +0000199
Nick Coghland5cacbb2015-05-23 22:24:10 +1000200 An array of slot definitions for multi-phase initialization, terminated by
201 a *NULL* entry.
Georg Brandle69cdf92009-01-04 23:20:14 +0000202
Georg Brandl60203b42010-10-06 10:11:56 +0000203 .. c:member:: traverseproc m_traverse
Georg Brandle69cdf92009-01-04 23:20:14 +0000204
205 A traversal function to call during GC traversal of the module object, or
206 *NULL* if not needed.
207
Georg Brandl60203b42010-10-06 10:11:56 +0000208 .. c:member:: inquiry m_clear
Georg Brandle69cdf92009-01-04 23:20:14 +0000209
210 A clear function to call during GC clearing of the module object, or
211 *NULL* if not needed.
212
Georg Brandl60203b42010-10-06 10:11:56 +0000213 .. c:member:: freefunc m_free
Georg Brandle69cdf92009-01-04 23:20:14 +0000214
215 A function to call during deallocation of the module object, or *NULL* if
216 not needed.
217
Nick Coghland5cacbb2015-05-23 22:24:10 +1000218The module initialization function may create and return the module object
219directly. This is referred to as "single-phase initialization", and uses one
220of 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
241Alternatively, the module initialization function may instead return a
242:c:type:`PyModuleDef` instance with a non-empty ``m_slots`` array. This is
243referred to as "multi-phase initialization", and ``PyModuleDef`` instance
244should 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
255The module initialization function (if using single phase initialization) or
256a function called from a module execution slot (if using multiphase
257initialization), can use the following functions to help initialize the module
258state:
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
Georg Brandle69cdf92009-01-04 23:20:14 +0000273
Georg Brandl60203b42010-10-06 10:11:56 +0000274.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000275
276 Add an object to *module* as *name*. This is a convenience function which can
277 be used from the module's initialization function. This steals a reference to
278 *value*. Return ``-1`` on error, ``0`` on success.
279
Georg Brandl60203b42010-10-06 10:11:56 +0000280.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000281
282 Add an integer constant to *module* as *name*. This convenience function can be
283 used from the module's initialization function. Return ``-1`` on error, ``0`` on
284 success.
285
286
Georg Brandl60203b42010-10-06 10:11:56 +0000287.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000288
289 Add a string constant to *module* as *name*. This convenience function can be
290 used from the module's initialization function. The string *value* must be
291 null-terminated. Return ``-1`` on error, ``0`` on success.
Christian Heimes1af737c2008-01-23 08:24:23 +0000292
293
Georg Brandl60203b42010-10-06 10:11:56 +0000294.. c:function:: int PyModule_AddIntMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000295
Georg Brandl48310cd2009-01-03 21:18:54 +0000296 Add an int constant to *module*. The name and the value are taken from
Benjamin Peterson4c020882011-04-30 13:14:56 -0500297 *macro*. For example ``PyModule_AddIntMacro(module, AF_INET)`` adds the int
Christian Heimes1af737c2008-01-23 08:24:23 +0000298 constant *AF_INET* with the value of *AF_INET* to *module*.
299 Return ``-1`` on error, ``0`` on success.
300
301
Georg Brandl60203b42010-10-06 10:11:56 +0000302.. c:function:: int PyModule_AddStringMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000303
304 Add a string constant to *module*.