blob: 95a01695b07a95f9f714435db74687a2a70de3e8 [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)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040038 single: __package__ (module attribute)
39 single: __loader__ (module attribute)
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
41 Return a new module object with the :attr:`__name__` attribute set to *name*.
Brett Cannon4c14b5d2013-05-04 13:56:58 -040042 The module's :attr:`__name__`, :attr:`__doc__`, :attr:`__package__`, and
43 :attr:`__loader__` attributes are filled in (all but :attr:`__name__` are set
44 to ``None``); the caller is responsible for providing a :attr:`__file__`
45 attribute.
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
Victor Stinner0639b562011-03-04 12:57:07 +000047 .. versionadded:: 3.3
48
Brett Cannon4c14b5d2013-05-04 13:56:58 -040049 .. versionchanged:: 3.4
50 :attr:`__package__` and :attr:`__loader__` are set to ``None``.
51
Victor Stinner0639b562011-03-04 12:57:07 +000052
53.. c:function:: PyObject* PyModule_New(const char *name)
54
55 Similar to :c:func:`PyImport_NewObject`, but the name is an UTF-8 encoded
56 string instead of a Unicode object.
57
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: PyObject* PyModule_GetDict(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
61 .. index:: single: __dict__ (module attribute)
62
63 Return the dictionary object that implements *module*'s namespace; this object
64 is the same as the :attr:`__dict__` attribute of the module object. This
65 function never fails. It is recommended extensions use other
Georg Brandl60203b42010-10-06 10:11:56 +000066 :c:func:`PyModule_\*` and :c:func:`PyObject_\*` functions rather than directly
Georg Brandl54a3faa2008-01-20 09:30:57 +000067 manipulate a module's :attr:`__dict__`.
68
69
Victor Stinnerbd475112011-02-23 00:21:43 +000070.. c:function:: PyObject* PyModule_GetNameObject(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72 .. index::
73 single: __name__ (module attribute)
74 single: SystemError (built-in exception)
75
76 Return *module*'s :attr:`__name__` value. If the module does not provide one,
77 or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
78
Victor Stinnerbd475112011-02-23 00:21:43 +000079 .. versionadded:: 3.3
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Victor Stinnerbd475112011-02-23 00:21:43 +000082.. c:function:: char* PyModule_GetName(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000083
Victor Stinnerbd475112011-02-23 00:21:43 +000084 Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to
85 ``'utf-8'``.
Victor Stinner6c00c142010-08-17 23:37:11 +000086
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000089
Georg Brandl54a3faa2008-01-20 09:30:57 +000090 .. index::
91 single: __file__ (module attribute)
92 single: SystemError (built-in exception)
93
94 Return the name of the file from which *module* was loaded using *module*'s
Victor Stinner6c00c142010-08-17 23:37:11 +000095 :attr:`__file__` attribute. If this is not defined, or if it is not a
96 unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return
Georg Brandldb6c7f52011-10-07 11:19:11 +020097 a reference to a Unicode object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
Victor Stinnerc14190d2010-08-18 10:57:33 +000099 .. versionadded:: 3.2
100
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101
Victor Stinnerbd475112011-02-23 00:21:43 +0000102.. c:function:: char* PyModule_GetFilename(PyObject *module)
103
104 Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
105 encoded to 'utf-8'.
106
107 .. deprecated:: 3.2
108 :c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
109 unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
110
111
Georg Brandl60203b42010-10-06 10:11:56 +0000112.. c:function:: void* PyModule_GetState(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000113
114 Return the "state" of the module, that is, a pointer to the block of memory
115 allocated at module creation time, or *NULL*. See
Georg Brandl60203b42010-10-06 10:11:56 +0000116 :c:member:`PyModuleDef.m_size`.
Georg Brandle69cdf92009-01-04 23:20:14 +0000117
118
Georg Brandl60203b42010-10-06 10:11:56 +0000119.. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000120
Georg Brandl60203b42010-10-06 10:11:56 +0000121 Return a pointer to the :c:type:`PyModuleDef` struct from which the module was
Georg Brandle69cdf92009-01-04 23:20:14 +0000122 created, or *NULL* if the module wasn't created with
Martin v. Löwis7800f752012-06-22 12:20:55 +0200123 :c:func:`PyModule_Create`.i
Georg Brandle69cdf92009-01-04 23:20:14 +0000124
Martin v. Löwis7800f752012-06-22 12:20:55 +0200125.. c:function:: PyObject* PyState_FindModule(PyModuleDef *def)
126
127 Returns the module object that was created from *def* for the current interpreter.
128 This method requires that the module object has been attached to the interpreter state with
129 :c:func:`PyState_AddModule` beforehand. In case the corresponding module object is not
130 found or has not been attached to the interpreter state yet, it returns NULL.
131
Ezio Melotti32b0f022013-03-06 02:57:25 +0200132.. c:function:: int PyState_AddModule(PyObject *module, PyModuleDef *def)
Martin v. Löwis7800f752012-06-22 12:20:55 +0200133
134 Attaches the module object passed to the function to the interpreter state. This allows
135 the module object to be accessible via
136 :c:func:`PyState_FindModule`.
137
138 .. versionadded:: 3.3
139
Ezio Melotti32b0f022013-03-06 02:57:25 +0200140.. c:function:: int PyState_RemoveModule(PyModuleDef *def)
Martin v. Löwis7800f752012-06-22 12:20:55 +0200141
142 Removes the module object created from *def* from the interpreter state.
143
144 .. versionadded:: 3.3
Georg Brandle69cdf92009-01-04 23:20:14 +0000145
146Initializing C modules
147^^^^^^^^^^^^^^^^^^^^^^
148
149These functions are usually used in the module initialization function.
150
Georg Brandl60203b42010-10-06 10:11:56 +0000151.. c:function:: PyObject* PyModule_Create(PyModuleDef *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000152
153 Create a new module object, given the definition in *module*. This behaves
Georg Brandl60203b42010-10-06 10:11:56 +0000154 like :c:func:`PyModule_Create2` with *module_api_version* set to
Georg Brandle69cdf92009-01-04 23:20:14 +0000155 :const:`PYTHON_API_VERSION`.
156
157
Georg Brandl60203b42010-10-06 10:11:56 +0000158.. c:function:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version)
Georg Brandle69cdf92009-01-04 23:20:14 +0000159
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
Georg Brandl60203b42010-10-06 10:11:56 +0000166 Most uses of this function should be using :c:func:`PyModule_Create`
Georg Brandle69cdf92009-01-04 23:20:14 +0000167 instead; only use this if you are sure you need it.
168
169
Georg Brandl60203b42010-10-06 10:11:56 +0000170.. c:type:: PyModuleDef
Georg Brandle69cdf92009-01-04 23:20:14 +0000171
172 This struct holds all information that is needed to create a module object.
173 There is usually only one static variable of that type for each module, which
Georg Brandl60203b42010-10-06 10:11:56 +0000174 is statically initialized and then passed to :c:func:`PyModule_Create` in the
Georg Brandle69cdf92009-01-04 23:20:14 +0000175 module initialization function.
176
Georg Brandl60203b42010-10-06 10:11:56 +0000177 .. c:member:: PyModuleDef_Base m_base
Georg Brandle69cdf92009-01-04 23:20:14 +0000178
179 Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
180
Georg Brandl60203b42010-10-06 10:11:56 +0000181 .. c:member:: char* m_name
Georg Brandle69cdf92009-01-04 23:20:14 +0000182
183 Name for the new module.
184
Georg Brandl60203b42010-10-06 10:11:56 +0000185 .. c:member:: char* m_doc
Georg Brandle69cdf92009-01-04 23:20:14 +0000186
187 Docstring for the module; usually a docstring variable created with
Georg Brandl60203b42010-10-06 10:11:56 +0000188 :c:func:`PyDoc_STRVAR` is used.
Georg Brandle69cdf92009-01-04 23:20:14 +0000189
Georg Brandl60203b42010-10-06 10:11:56 +0000190 .. c:member:: Py_ssize_t m_size
Georg Brandle69cdf92009-01-04 23:20:14 +0000191
192 If the module object needs additional memory, this should be set to the
193 number of bytes to allocate; a pointer to the block of memory can be
Georg Brandl60203b42010-10-06 10:11:56 +0000194 retrieved with :c:func:`PyModule_GetState`. If no memory is needed, set
Georg Brandle69cdf92009-01-04 23:20:14 +0000195 this to ``-1``.
196
197 This memory should be used, rather than static globals, to hold per-module
198 state, since it is then safe for use in multiple sub-interpreters. It is
Georg Brandl60203b42010-10-06 10:11:56 +0000199 freed when the module object is deallocated, after the :c:member:`m_free`
Georg Brandle69cdf92009-01-04 23:20:14 +0000200 function has been called, if present.
201
Georg Brandl60203b42010-10-06 10:11:56 +0000202 .. c:member:: PyMethodDef* m_methods
Georg Brandle69cdf92009-01-04 23:20:14 +0000203
204 A pointer to a table of module-level functions, described by
Georg Brandl60203b42010-10-06 10:11:56 +0000205 :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present.
Georg Brandle69cdf92009-01-04 23:20:14 +0000206
Georg Brandl60203b42010-10-06 10:11:56 +0000207 .. c:member:: inquiry m_reload
Georg Brandle69cdf92009-01-04 23:20:14 +0000208
209 Currently unused, should be *NULL*.
210
Georg Brandl60203b42010-10-06 10:11:56 +0000211 .. c:member:: traverseproc m_traverse
Georg Brandle69cdf92009-01-04 23:20:14 +0000212
213 A traversal function to call during GC traversal of the module object, or
214 *NULL* if not needed.
215
Georg Brandl60203b42010-10-06 10:11:56 +0000216 .. c:member:: inquiry m_clear
Georg Brandle69cdf92009-01-04 23:20:14 +0000217
218 A clear function to call during GC clearing of the module object, or
219 *NULL* if not needed.
220
Georg Brandl60203b42010-10-06 10:11:56 +0000221 .. c:member:: freefunc m_free
Georg Brandle69cdf92009-01-04 23:20:14 +0000222
223 A function to call during deallocation of the module object, or *NULL* if
224 not needed.
225
226
Georg Brandl60203b42010-10-06 10:11:56 +0000227.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000228
229 Add an object to *module* as *name*. This is a convenience function which can
230 be used from the module's initialization function. This steals a reference to
231 *value*. Return ``-1`` on error, ``0`` on success.
232
233
Georg Brandl60203b42010-10-06 10:11:56 +0000234.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000235
236 Add an integer constant to *module* as *name*. This convenience function can be
237 used from the module's initialization function. Return ``-1`` on error, ``0`` on
238 success.
239
240
Georg Brandl60203b42010-10-06 10:11:56 +0000241.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000242
243 Add a string constant to *module* as *name*. This convenience function can be
244 used from the module's initialization function. The string *value* must be
245 null-terminated. Return ``-1`` on error, ``0`` on success.
Christian Heimes1af737c2008-01-23 08:24:23 +0000246
247
Georg Brandl60203b42010-10-06 10:11:56 +0000248.. c:function:: int PyModule_AddIntMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000249
Georg Brandl48310cd2009-01-03 21:18:54 +0000250 Add an int constant to *module*. The name and the value are taken from
Benjamin Peterson4c020882011-04-30 13:14:56 -0500251 *macro*. For example ``PyModule_AddIntMacro(module, AF_INET)`` adds the int
Christian Heimes1af737c2008-01-23 08:24:23 +0000252 constant *AF_INET* with the value of *AF_INET* to *module*.
253 Return ``-1`` on error, ``0`` on success.
254
255
Georg Brandl60203b42010-10-06 10:11:56 +0000256.. c:function:: int PyModule_AddStringMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000257
258 Add a string constant to *module*.