blob: ffd68e308489ae1d4bd1db09682c0cb844663a4f [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
Georg Brandl60203b42010-10-06 10:11:56 +000032.. c:function:: PyObject* PyModule_New(const char *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
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: PyObject* PyModule_GetDict(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000045
46 .. index:: single: __dict__ (module attribute)
47
48 Return the dictionary object that implements *module*'s namespace; this object
49 is the same as the :attr:`__dict__` attribute of the module object. This
50 function never fails. It is recommended extensions use other
Georg Brandl60203b42010-10-06 10:11:56 +000051 :c:func:`PyModule_\*` and :c:func:`PyObject_\*` functions rather than directly
Georg Brandl54a3faa2008-01-20 09:30:57 +000052 manipulate a module's :attr:`__dict__`.
53
54
Georg Brandl60203b42010-10-06 10:11:56 +000055.. c:function:: char* PyModule_GetName(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000056
57 .. index::
58 single: __name__ (module attribute)
59 single: SystemError (built-in exception)
60
61 Return *module*'s :attr:`__name__` value. If the module does not provide one,
62 or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
63
64
Georg Brandl60203b42010-10-06 10:11:56 +000065.. c:function:: char* PyModule_GetFilename(PyObject *module)
Georg Brandl54a3faa2008-01-20 09:30:57 +000066
Georg Brandl60203b42010-10-06 10:11:56 +000067 Similar to :c:func:`PyModule_GetFilenameObject` but return the filename
Victor Stinner6c00c142010-08-17 23:37:11 +000068 encoded to 'utf-8'.
69
70 .. deprecated:: 3.2
Georg Brandl60203b42010-10-06 10:11:56 +000071 :c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on
72 unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead.
Victor Stinner6c00c142010-08-17 23:37:11 +000073
74
Georg Brandl60203b42010-10-06 10:11:56 +000075.. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module)
Victor Stinner6c00c142010-08-17 23:37:11 +000076
Georg Brandl54a3faa2008-01-20 09:30:57 +000077 .. index::
78 single: __file__ (module attribute)
79 single: SystemError (built-in exception)
80
81 Return the name of the file from which *module* was loaded using *module*'s
Victor Stinner6c00c142010-08-17 23:37:11 +000082 :attr:`__file__` attribute. If this is not defined, or if it is not a
83 unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return
Georg Brandl60203b42010-10-06 10:11:56 +000084 a reference to a :c:type:`PyUnicodeObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
Victor Stinnerc14190d2010-08-18 10:57:33 +000086 .. versionadded:: 3.2
87
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
Georg Brandl60203b42010-10-06 10:11:56 +000089.. c:function:: void* PyModule_GetState(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +000090
91 Return the "state" of the module, that is, a pointer to the block of memory
92 allocated at module creation time, or *NULL*. See
Georg Brandl60203b42010-10-06 10:11:56 +000093 :c:member:`PyModuleDef.m_size`.
Georg Brandle69cdf92009-01-04 23:20:14 +000094
95
Georg Brandl60203b42010-10-06 10:11:56 +000096.. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module)
Georg Brandle69cdf92009-01-04 23:20:14 +000097
Georg Brandl60203b42010-10-06 10:11:56 +000098 Return a pointer to the :c:type:`PyModuleDef` struct from which the module was
Georg Brandle69cdf92009-01-04 23:20:14 +000099 created, or *NULL* if the module wasn't created with
Georg Brandl60203b42010-10-06 10:11:56 +0000100 :c:func:`PyModule_Create`.
Georg Brandle69cdf92009-01-04 23:20:14 +0000101
102
103Initializing C modules
104^^^^^^^^^^^^^^^^^^^^^^
105
106These functions are usually used in the module initialization function.
107
Georg Brandl60203b42010-10-06 10:11:56 +0000108.. c:function:: PyObject* PyModule_Create(PyModuleDef *module)
Georg Brandle69cdf92009-01-04 23:20:14 +0000109
110 Create a new module object, given the definition in *module*. This behaves
Georg Brandl60203b42010-10-06 10:11:56 +0000111 like :c:func:`PyModule_Create2` with *module_api_version* set to
Georg Brandle69cdf92009-01-04 23:20:14 +0000112 :const:`PYTHON_API_VERSION`.
113
114
Georg Brandl60203b42010-10-06 10:11:56 +0000115.. c:function:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version)
Georg Brandle69cdf92009-01-04 23:20:14 +0000116
117 Create a new module object, given the definition in *module*, assuming the
118 API version *module_api_version*. If that version does not match the version
119 of the running interpreter, a :exc:`RuntimeWarning` is emitted.
120
121 .. note::
122
Georg Brandl60203b42010-10-06 10:11:56 +0000123 Most uses of this function should be using :c:func:`PyModule_Create`
Georg Brandle69cdf92009-01-04 23:20:14 +0000124 instead; only use this if you are sure you need it.
125
126
Georg Brandl60203b42010-10-06 10:11:56 +0000127.. c:type:: PyModuleDef
Georg Brandle69cdf92009-01-04 23:20:14 +0000128
129 This struct holds all information that is needed to create a module object.
130 There is usually only one static variable of that type for each module, which
Georg Brandl60203b42010-10-06 10:11:56 +0000131 is statically initialized and then passed to :c:func:`PyModule_Create` in the
Georg Brandle69cdf92009-01-04 23:20:14 +0000132 module initialization function.
133
Georg Brandl60203b42010-10-06 10:11:56 +0000134 .. c:member:: PyModuleDef_Base m_base
Georg Brandle69cdf92009-01-04 23:20:14 +0000135
136 Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
137
Georg Brandl60203b42010-10-06 10:11:56 +0000138 .. c:member:: char* m_name
Georg Brandle69cdf92009-01-04 23:20:14 +0000139
140 Name for the new module.
141
Georg Brandl60203b42010-10-06 10:11:56 +0000142 .. c:member:: char* m_doc
Georg Brandle69cdf92009-01-04 23:20:14 +0000143
144 Docstring for the module; usually a docstring variable created with
Georg Brandl60203b42010-10-06 10:11:56 +0000145 :c:func:`PyDoc_STRVAR` is used.
Georg Brandle69cdf92009-01-04 23:20:14 +0000146
Georg Brandl60203b42010-10-06 10:11:56 +0000147 .. c:member:: Py_ssize_t m_size
Georg Brandle69cdf92009-01-04 23:20:14 +0000148
149 If the module object needs additional memory, this should be set to the
150 number of bytes to allocate; a pointer to the block of memory can be
Georg Brandl60203b42010-10-06 10:11:56 +0000151 retrieved with :c:func:`PyModule_GetState`. If no memory is needed, set
Georg Brandle69cdf92009-01-04 23:20:14 +0000152 this to ``-1``.
153
154 This memory should be used, rather than static globals, to hold per-module
155 state, since it is then safe for use in multiple sub-interpreters. It is
Georg Brandl60203b42010-10-06 10:11:56 +0000156 freed when the module object is deallocated, after the :c:member:`m_free`
Georg Brandle69cdf92009-01-04 23:20:14 +0000157 function has been called, if present.
158
Georg Brandl60203b42010-10-06 10:11:56 +0000159 .. c:member:: PyMethodDef* m_methods
Georg Brandle69cdf92009-01-04 23:20:14 +0000160
161 A pointer to a table of module-level functions, described by
Georg Brandl60203b42010-10-06 10:11:56 +0000162 :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present.
Georg Brandle69cdf92009-01-04 23:20:14 +0000163
Georg Brandl60203b42010-10-06 10:11:56 +0000164 .. c:member:: inquiry m_reload
Georg Brandle69cdf92009-01-04 23:20:14 +0000165
166 Currently unused, should be *NULL*.
167
Georg Brandl60203b42010-10-06 10:11:56 +0000168 .. c:member:: traverseproc m_traverse
Georg Brandle69cdf92009-01-04 23:20:14 +0000169
170 A traversal function to call during GC traversal of the module object, or
171 *NULL* if not needed.
172
Georg Brandl60203b42010-10-06 10:11:56 +0000173 .. c:member:: inquiry m_clear
Georg Brandle69cdf92009-01-04 23:20:14 +0000174
175 A clear function to call during GC clearing of the module object, or
176 *NULL* if not needed.
177
Georg Brandl60203b42010-10-06 10:11:56 +0000178 .. c:member:: freefunc m_free
Georg Brandle69cdf92009-01-04 23:20:14 +0000179
180 A function to call during deallocation of the module object, or *NULL* if
181 not needed.
182
183
Georg Brandl60203b42010-10-06 10:11:56 +0000184.. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000185
186 Add an object to *module* as *name*. This is a convenience function which can
187 be used from the module's initialization function. This steals a reference to
188 *value*. Return ``-1`` on error, ``0`` on success.
189
190
Georg Brandl60203b42010-10-06 10:11:56 +0000191.. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000192
193 Add an integer constant to *module* as *name*. This convenience function can be
194 used from the module's initialization function. Return ``-1`` on error, ``0`` on
195 success.
196
197
Georg Brandl60203b42010-10-06 10:11:56 +0000198.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000199
200 Add a string constant to *module* as *name*. This convenience function can be
201 used from the module's initialization function. The string *value* must be
202 null-terminated. Return ``-1`` on error, ``0`` on success.
Christian Heimes1af737c2008-01-23 08:24:23 +0000203
204
Georg Brandl60203b42010-10-06 10:11:56 +0000205.. c:function:: int PyModule_AddIntMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000206
Georg Brandl48310cd2009-01-03 21:18:54 +0000207 Add an int constant to *module*. The name and the value are taken from
Benjamin Peterson4c020882011-04-30 13:14:56 -0500208 *macro*. For example ``PyModule_AddIntMacro(module, AF_INET)`` adds the int
Christian Heimes1af737c2008-01-23 08:24:23 +0000209 constant *AF_INET* with the value of *AF_INET* to *module*.
210 Return ``-1`` on error, ``0`` on success.
211
212
Georg Brandl60203b42010-10-06 10:11:56 +0000213.. c:function:: int PyModule_AddStringMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000214
215 Add a string constant to *module*.