blob: 65ff8fa72853ed61f66b056b03cc76d7d8c3b9f2 [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
10There are only a few functions special to module objects.
11
12
Georg Brandl60203b42010-10-06 10:11:56 +000013.. c:var:: PyTypeObject PyModule_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
15 .. index:: single: ModuleType (in module types)
16
Georg Brandl60203b42010-10-06 10:11:56 +000017 This instance of :c:type:`PyTypeObject` represents the Python module type. This
Georg Brandl54a3faa2008-01-20 09:30:57 +000018 is exposed to Python programs as ``types.ModuleType``.
19
20
Georg Brandl60203b42010-10-06 10:11:56 +000021.. c:function:: int PyModule_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23 Return true if *p* is a module object, or a subtype of a module object.
24
25
Georg Brandl60203b42010-10-06 10:11:56 +000026.. c:function:: int PyModule_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000027
28 Return true if *p* is a module object, but not a subtype of
Georg Brandl60203b42010-10-06 10:11:56 +000029 :c:data:`PyModule_Type`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000030
31
Victor Stinner0639b562011-03-04 12:57:07 +000032.. c:function:: PyObject* PyModule_NewObject(PyObject *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000033
34 .. index::
35 single: __name__ (module attribute)
36 single: __doc__ (module attribute)
37 single: __file__ (module attribute)
38
39 Return a new module object with the :attr:`__name__` attribute set to *name*.
40 Only the module's :attr:`__doc__` and :attr:`__name__` attributes are filled in;
41 the caller is responsible for providing a :attr:`__file__` attribute.
42
Victor Stinner0639b562011-03-04 12:57:07 +000043 .. versionadded:: 3.3
44
45
46.. c:function:: PyObject* PyModule_New(const char *name)
47
48 Similar to :c:func:`PyImport_NewObject`, but the name is an UTF-8 encoded
49 string instead of a Unicode object.
50
Georg Brandl54a3faa2008-01-20 09:30:57 +000051
Georg Brandl60203b42010-10-06 10:11:56 +000052.. c:function:: PyObject* PyModule_GetDict(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
54 .. index:: single: __dict__ (module attribute)
55
56 Return the dictionary object that implements *module*'s namespace; this object
57 is the same as the :attr:`__dict__` attribute of the module object. This
58 function never fails. It is recommended extensions use other
Georg Brandl60203b42010-10-06 10:11:56 +000059 :c:func:`PyModule_\*` and :c:func:`PyObject_\*` functions rather than directly
Georg Brandl54a3faa2008-01-20 09:30:57 +000060 manipulate a module's :attr:`__dict__`.
61
62
Victor Stinnerbd475112011-02-23 00:21:43 +000063.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65 .. index::
66 single: __name__ (module attribute)
67 single: SystemError (built-in exception)
68
69 Return *module*'s :attr:`__name__` value. If the module does not provide one,
70 or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
71
Victor Stinnerbd475112011-02-23 00:21:43 +000072 .. versionadded:: 3.3
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Georg Brandl54a3faa2008-01-20 09:30:57 +000074
Victor Stinnerbd475112011-02-23 00:21:43 +000075.. c:function:: char* PyModule_GetName(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000076
Victor Stinnerbd475112011-02-23 00:21:43 +000077 Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to
78 ``'utf-8'``.
Victor Stinner6c00c142010-08-17 23:37:11 +000079
80
Georg Brandl60203b42010-10-06 10:11:56 +000081.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000082
Georg Brandl54a3faa2008-01-20 09:30:57 +000083 .. index::
84 single: __file__ (module attribute)
85 single: SystemError (built-in exception)
86
87 Return the name of the file from which *module* was loaded using *module*'s
Victor Stinner6c00c142010-08-17 23:37:11 +000088 :attr:`__file__` attribute. If this is not defined, or if it is not a
89 unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return
Georg Brandldb6c7f52011-10-07 11:19:11 +020090 a reference to a Unicode object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000091
Victor Stinnerc14190d2010-08-18 10:57:33 +000092 .. versionadded:: 3.2
93
Georg Brandl54a3faa2008-01-20 09:30:57 +000094
Victor Stinnerbd475112011-02-23 00:21:43 +000095.. c:function:: char* PyModule_GetFilename(PyObject *module)
96
97 Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
98 encoded to 'utf-8'.
99
100 .. deprecated:: 3.2
101 :c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
102 unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
103
104
Georg Brandl60203b42010-10-06 10:11:56 +0000105.. c:function:: void* PyModule_GetState(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000106
107 Return the "state" of the module, that is, a pointer to the block of memory
108 allocated at module creation time, or *NULL*. See
Georg Brandl60203b42010-10-06 10:11:56 +0000109 :c:member:`PyModuleDef.m_size`.
Georg Brandle69cdf92009-01-04 23:20:14 +0000110
111
Georg Brandl60203b42010-10-06 10:11:56 +0000112.. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000113
Georg Brandl60203b42010-10-06 10:11:56 +0000114 Return a pointer to the :c:type:`PyModuleDef` struct from which the module was
Georg Brandle69cdf92009-01-04 23:20:14 +0000115 created, or *NULL* if the module wasn't created with
Martin v. Löwis7800f752012-06-22 12:20:55 +0200116 :c:func:`PyModule_Create`.i
Georg Brandle69cdf92009-01-04 23:20:14 +0000117
Martin v. Löwis7800f752012-06-22 12:20:55 +0200118.. c:function:: PyObject* PyState_FindModule(PyModuleDef *def)
119
120 Returns the module object that was created from *def* for the current interpreter.
121 This method requires that the module object has been attached to the interpreter state with
122 :c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not
123 found or has not been attached to the interpreter state yet, it returns NULL.
124
Ezio Melotti32b0f022013-03-06 02:57:25 +0200125.. c:function:: int PyState_AddModule(PyObject *module, PyModuleDef *def)
Martin v. Löwis7800f752012-06-22 12:20:55 +0200126
127 Attaches the module object passed to the function to the interpreter state. This allows
128 the module object to be accessible via
129 :c:func:`PyState_FindModule`.
130
131 .. versionadded:: 3.3
132
Ezio Melotti32b0f022013-03-06 02:57:25 +0200133.. c:function:: int PyState_RemoveModule(PyModuleDef *def)
Martin v. Löwis7800f752012-06-22 12:20:55 +0200134
135 Removes the module object created from *def* from the interpreter state.
136
137 .. versionadded:: 3.3
Georg Brandle69cdf92009-01-04 23:20:14 +0000138
139Initializing C modules
140^^^^^^^^^^^^^^^^^^^^^^
141
142These functions are usually used in the module initialization function.
143
Georg Brandl60203b42010-10-06 10:11:56 +0000144.. c:function:: PyObject* PyModule_Create(PyModuleDef *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000145
146 Create a new module object, given the definition in *module*. This behaves
Georg Brandl60203b42010-10-06 10:11:56 +0000147 like :c:func:`PyModule_Create2` with *module_api_version* set to
Georg Brandle69cdf92009-01-04 23:20:14 +0000148 :const:`PYTHON_API_VERSION`.
149
150
Georg Brandl60203b42010-10-06 10:11:56 +0000151.. c:function:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version)
Georg Brandle69cdf92009-01-04 23:20:14 +0000152
153 Create a new module object, given the definition in *module*, assuming the
154 API version *module_api_version*. If that version does not match the version
155 of the running interpreter, a :exc:`RuntimeWarning` is emitted.
156
157 .. note::
158
Georg Brandl60203b42010-10-06 10:11:56 +0000159 Most uses of this function should be using :c:func:`PyModule_Create`
Georg Brandle69cdf92009-01-04 23:20:14 +0000160 instead; only use this if you are sure you need it.
161
162
Georg Brandl60203b42010-10-06 10:11:56 +0000163.. c:type:: PyModuleDef
Georg Brandle69cdf92009-01-04 23:20:14 +0000164
165 This struct holds all information that is needed to create a module object.
166 There is usually only one static variable of that type for each module, which
Georg Brandl60203b42010-10-06 10:11:56 +0000167 is statically initialized and then passed to :c:func:`PyModule_Create` in the
Georg Brandle69cdf92009-01-04 23:20:14 +0000168 module initialization function.
169
Georg Brandl60203b42010-10-06 10:11:56 +0000170 .. c:member:: PyModuleDef_Base m_base
Georg Brandle69cdf92009-01-04 23:20:14 +0000171
172 Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
173
Georg Brandl60203b42010-10-06 10:11:56 +0000174 .. c:member:: char* m_name
Georg Brandle69cdf92009-01-04 23:20:14 +0000175
176 Name for the new module.
177
Georg Brandl60203b42010-10-06 10:11:56 +0000178 .. c:member:: char* m_doc
Georg Brandle69cdf92009-01-04 23:20:14 +0000179
180 Docstring for the module; usually a docstring variable created with
Georg Brandl60203b42010-10-06 10:11:56 +0000181 :c:func:`PyDoc_STRVAR` is used.
Georg Brandle69cdf92009-01-04 23:20:14 +0000182
Georg Brandl60203b42010-10-06 10:11:56 +0000183 .. c:member:: Py_ssize_t m_size
Georg Brandle69cdf92009-01-04 23:20:14 +0000184
185 If the module object needs additional memory, this should be set to the
186 number of bytes to allocate; a pointer to the block of memory can be
Georg Brandl60203b42010-10-06 10:11:56 +0000187 retrieved with :c:func:`PyModule_GetState`. If no memory is needed, set
Georg Brandle69cdf92009-01-04 23:20:14 +0000188 this to ``-1``.
189
190 This memory should be used, rather than static globals, to hold per-module
191 state, since it is then safe for use in multiple sub-interpreters. It is
Georg Brandl60203b42010-10-06 10:11:56 +0000192 freed when the module object is deallocated, after the :c:member:`m_free`
Georg Brandle69cdf92009-01-04 23:20:14 +0000193 function has been called, if present.
194
Georg Brandl60203b42010-10-06 10:11:56 +0000195 .. c:member:: PyMethodDef* m_methods
Georg Brandle69cdf92009-01-04 23:20:14 +0000196
197 A pointer to a table of module-level functions, described by
Georg Brandl60203b42010-10-06 10:11:56 +0000198 :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present.
Georg Brandle69cdf92009-01-04 23:20:14 +0000199
Georg Brandl60203b42010-10-06 10:11:56 +0000200 .. c:member:: inquiry m_reload
Georg Brandle69cdf92009-01-04 23:20:14 +0000201
202 Currently unused, should be *NULL*.
203
Georg Brandl60203b42010-10-06 10:11:56 +0000204 .. c:member:: traverseproc m_traverse
Georg Brandle69cdf92009-01-04 23:20:14 +0000205
206 A traversal function to call during GC traversal of the module object, or
207 *NULL* if not needed.
208
Georg Brandl60203b42010-10-06 10:11:56 +0000209 .. c:member:: inquiry m_clear
Georg Brandle69cdf92009-01-04 23:20:14 +0000210
211 A clear function to call during GC clearing of the module object, or
212 *NULL* if not needed.
213
Georg Brandl60203b42010-10-06 10:11:56 +0000214 .. c:member:: freefunc m_free
Georg Brandle69cdf92009-01-04 23:20:14 +0000215
216 A function to call during deallocation of the module object, or *NULL* if
217 not needed.
218
219
Georg Brandl60203b42010-10-06 10:11:56 +0000220.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000221
222 Add an object to *module* as *name*. This is a convenience function which can
223 be used from the module's initialization function. This steals a reference to
224 *value*. Return ``-1`` on error, ``0`` on success.
225
226
Georg Brandl60203b42010-10-06 10:11:56 +0000227.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000228
229 Add an integer constant to *module* as *name*. This convenience function can be
230 used from the module's initialization function. Return ``-1`` on error, ``0`` on
231 success.
232
233
Georg Brandl60203b42010-10-06 10:11:56 +0000234.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000235
236 Add a string constant to *module* as *name*. This convenience function can be
237 used from the module's initialization function. The string *value* must be
238 null-terminated. Return ``-1`` on error, ``0`` on success.
Christian Heimes1af737c2008-01-23 08:24:23 +0000239
240
Georg Brandl60203b42010-10-06 10:11:56 +0000241.. c:function:: int PyModule_AddIntMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000242
Georg Brandl48310cd2009-01-03 21:18:54 +0000243 Add an int constant to *module*. The name and the value are taken from
Benjamin Peterson4c020882011-04-30 13:14:56 -0500244 *macro*. For example ``PyModule_AddIntMacro(module, AF_INET)`` adds the int
Christian Heimes1af737c2008-01-23 08:24:23 +0000245 constant *AF_INET* with the value of *AF_INET* to *module*.
246 Return ``-1`` on error, ``0`` on success.
247
248
Georg Brandl60203b42010-10-06 10:11:56 +0000249.. c:function:: int PyModule_AddStringMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000250
251 Add a string constant to *module*.