blob: 9e1af09906ed14928ae227551dd195cb6aeb06ad [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
13.. cvar:: PyTypeObject PyModule_Type
14
15 .. index:: single: ModuleType (in module types)
16
17 This instance of :ctype:`PyTypeObject` represents the Python module type. This
18 is exposed to Python programs as ``types.ModuleType``.
19
20
Christian Heimesaf98da12008-01-27 15:18:18 +000021.. cfunction:: 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
Christian Heimesaf98da12008-01-27 15:18:18 +000026.. cfunction:: 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
29 :cdata:`PyModule_Type`.
30
31
32.. cfunction:: PyObject* PyModule_New(const char *name)
33
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
44.. cfunction:: PyObject* PyModule_GetDict(PyObject *module)
45
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
51 :cfunc:`PyModule_\*` and :cfunc:`PyObject_\*` functions rather than directly
52 manipulate a module's :attr:`__dict__`.
53
54
55.. cfunction:: char* PyModule_GetName(PyObject *module)
56
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
65.. cfunction:: char* PyModule_GetFilename(PyObject *module)
66
Victor Stinner6c00c142010-08-17 23:37:11 +000067 Similar to :cfunc:`PyModule_GetFilenameObject` but return the filename
68 encoded to 'utf-8'.
69
70 .. deprecated:: 3.2
71 :cfunc:`PyModule_GetFilename` raises :ctype:`UnicodeEncodeError` on
72 unencodable filenames, use :cfunc:`PyModule_GetFilenameObject` instead.
73
74
75.. cfunction:: PyObject* PyModule_GetFilenameObject(PyObject *module)
76
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
84 a reference to a :ctype:`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 Brandle69cdf92009-01-04 23:20:14 +000089.. cfunction:: void* PyModule_GetState(PyObject *module)
90
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
93 :cmember:`PyModuleDef.m_size`.
94
95
96.. cfunction:: PyModuleDef* PyModule_GetDef(PyObject *module)
97
98 Return a pointer to the :ctype:`PyModuleDef` struct from which the module was
99 created, or *NULL* if the module wasn't created with
100 :cfunc:`PyModule_Create`.
101
102
103Initializing C modules
104^^^^^^^^^^^^^^^^^^^^^^
105
106These functions are usually used in the module initialization function.
107
108.. cfunction:: PyObject* PyModule_Create(PyModuleDef *module)
109
110 Create a new module object, given the definition in *module*. This behaves
111 like :cfunc:`PyModule_Create2` with *module_api_version* set to
112 :const:`PYTHON_API_VERSION`.
113
114
115.. cfunction:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version)
116
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
123 Most uses of this function should be using :cfunc:`PyModule_Create`
124 instead; only use this if you are sure you need it.
125
126
127.. ctype:: PyModuleDef
128
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
131 is statically initialized and then passed to :cfunc:`PyModule_Create` in the
132 module initialization function.
133
134 .. cmember:: PyModuleDef_Base m_base
135
136 Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
137
138 .. cmember:: char* m_name
139
140 Name for the new module.
141
142 .. cmember:: char* m_doc
143
144 Docstring for the module; usually a docstring variable created with
145 :cfunc:`PyDoc_STRVAR` is used.
146
147 .. cmember:: Py_ssize_t m_size
148
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
151 retrieved with :cfunc:`PyModule_GetState`. If no memory is needed, set
152 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
156 freed when the module object is deallocated, after the :cmember:`m_free`
157 function has been called, if present.
158
159 .. cmember:: PyMethodDef* m_methods
160
161 A pointer to a table of module-level functions, described by
162 :ctype:`PyMethodDef` values. Can be *NULL* if no functions are present.
163
164 .. cmember:: inquiry m_reload
165
166 Currently unused, should be *NULL*.
167
168 .. cmember:: traverseproc m_traverse
169
170 A traversal function to call during GC traversal of the module object, or
171 *NULL* if not needed.
172
173 .. cmember:: inquiry m_clear
174
175 A clear function to call during GC clearing of the module object, or
176 *NULL* if not needed.
177
178 .. cmember:: freefunc m_free
179
180 A function to call during deallocation of the module object, or *NULL* if
181 not needed.
182
183
Georg Brandl54a3faa2008-01-20 09:30:57 +0000184.. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
185
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
191.. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
192
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
198.. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
199
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
Christian Heimesaf98da12008-01-27 15:18:18 +0000205.. cfunction:: 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
Christian Heimes1af737c2008-01-23 08:24:23 +0000208 *macro*. For example ``PyModule_AddConstant(module, AF_INET)`` adds the int
209 constant *AF_INET* with the value of *AF_INET* to *module*.
210 Return ``-1`` on error, ``0`` on success.
211
212
Christian Heimesaf98da12008-01-27 15:18:18 +0000213.. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro)
Christian Heimes1af737c2008-01-23 08:24:23 +0000214
215 Add a string constant to *module*.