Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _moduleobjects: |
| 4 | |
| 5 | Module Objects |
| 6 | -------------- |
| 7 | |
| 8 | .. index:: object: module |
| 9 | |
| 10 | There are only a few functions special to module objects. |
| 11 | |
| 12 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 13 | .. c:var:: PyTypeObject PyModule_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 | |
| 15 | .. index:: single: ModuleType (in module types) |
| 16 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 17 | This instance of :c:type:`PyTypeObject` represents the Python module type. This |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 18 | is exposed to Python programs as ``types.ModuleType``. |
| 19 | |
| 20 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 21 | .. c:function:: int PyModule_Check(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
| 23 | Return true if *p* is a module object, or a subtype of a module object. |
| 24 | |
| 25 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 26 | .. c:function:: int PyModule_CheckExact(PyObject *p) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
| 28 | Return true if *p* is a module object, but not a subtype of |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 29 | :c:data:`PyModule_Type`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 30 | |
| 31 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 32 | .. c:function:: PyObject* PyModule_New(const char *name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:function:: PyObject* PyModule_GetDict(PyObject *module) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 51 | :c:func:`PyModule_\*` and :c:func:`PyObject_\*` functions rather than directly |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | manipulate a module's :attr:`__dict__`. |
| 53 | |
| 54 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 55 | .. c:function:: PyObject* PyModule_GetNameObject(PyObject *module) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 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 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 64 | .. versionadded:: 3.3 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 66 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 67 | .. c:function:: char* PyModule_GetName(PyObject *module) |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 68 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 69 | Similar to :c:func:`PyModule_GetNameObject` but return the name encoded to |
| 70 | ``'utf-8'``. |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 71 | |
| 72 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 73 | .. c:function:: PyObject* PyModule_GetFilenameObject(PyObject *module) |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 74 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | .. index:: |
| 76 | single: __file__ (module attribute) |
| 77 | single: SystemError (built-in exception) |
| 78 | |
| 79 | Return the name of the file from which *module* was loaded using *module*'s |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 80 | :attr:`__file__` attribute. If this is not defined, or if it is not a |
| 81 | unicode string, raise :exc:`SystemError` and return *NULL*; otherwise return |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | a reference to a :c:type:`PyUnicodeObject`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 83 | |
Victor Stinner | c14190d | 2010-08-18 10:57:33 +0000 | [diff] [blame] | 84 | .. versionadded:: 3.2 |
| 85 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 87 | .. c:function:: char* PyModule_GetFilename(PyObject *module) |
| 88 | |
| 89 | Similar to :c:func:`PyModule_GetFilenameObject` but return the filename |
| 90 | encoded to 'utf-8'. |
| 91 | |
| 92 | .. deprecated:: 3.2 |
| 93 | :c:func:`PyModule_GetFilename` raises :c:type:`UnicodeEncodeError` on |
| 94 | unencodable filenames, use :c:func:`PyModule_GetFilenameObject` instead. |
| 95 | |
| 96 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 97 | .. c:function:: void* PyModule_GetState(PyObject *module) |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 98 | |
| 99 | Return the "state" of the module, that is, a pointer to the block of memory |
| 100 | allocated at module creation time, or *NULL*. See |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 101 | :c:member:`PyModuleDef.m_size`. |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 102 | |
| 103 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 104 | .. c:function:: PyModuleDef* PyModule_GetDef(PyObject *module) |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 105 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 106 | Return a pointer to the :c:type:`PyModuleDef` struct from which the module was |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 107 | created, or *NULL* if the module wasn't created with |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 108 | :c:func:`PyModule_Create`. |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | Initializing C modules |
| 112 | ^^^^^^^^^^^^^^^^^^^^^^ |
| 113 | |
| 114 | These functions are usually used in the module initialization function. |
| 115 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 116 | .. c:function:: PyObject* PyModule_Create(PyModuleDef *module) |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 117 | |
| 118 | Create a new module object, given the definition in *module*. This behaves |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 119 | like :c:func:`PyModule_Create2` with *module_api_version* set to |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 120 | :const:`PYTHON_API_VERSION`. |
| 121 | |
| 122 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 123 | .. c:function:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version) |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 124 | |
| 125 | Create a new module object, given the definition in *module*, assuming the |
| 126 | API version *module_api_version*. If that version does not match the version |
| 127 | of the running interpreter, a :exc:`RuntimeWarning` is emitted. |
| 128 | |
| 129 | .. note:: |
| 130 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 131 | Most uses of this function should be using :c:func:`PyModule_Create` |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 132 | instead; only use this if you are sure you need it. |
| 133 | |
| 134 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 135 | .. c:type:: PyModuleDef |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 136 | |
| 137 | This struct holds all information that is needed to create a module object. |
| 138 | There is usually only one static variable of that type for each module, which |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 139 | is statically initialized and then passed to :c:func:`PyModule_Create` in the |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 140 | module initialization function. |
| 141 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 142 | .. c:member:: PyModuleDef_Base m_base |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 143 | |
| 144 | Always initialize this member to :const:`PyModuleDef_HEAD_INIT`. |
| 145 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 146 | .. c:member:: char* m_name |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 147 | |
| 148 | Name for the new module. |
| 149 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 150 | .. c:member:: char* m_doc |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 151 | |
| 152 | Docstring for the module; usually a docstring variable created with |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 153 | :c:func:`PyDoc_STRVAR` is used. |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 154 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 155 | .. c:member:: Py_ssize_t m_size |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 156 | |
| 157 | If the module object needs additional memory, this should be set to the |
| 158 | number of bytes to allocate; a pointer to the block of memory can be |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 159 | retrieved with :c:func:`PyModule_GetState`. If no memory is needed, set |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 160 | this to ``-1``. |
| 161 | |
| 162 | This memory should be used, rather than static globals, to hold per-module |
| 163 | state, since it is then safe for use in multiple sub-interpreters. It is |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 164 | freed when the module object is deallocated, after the :c:member:`m_free` |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 165 | function has been called, if present. |
| 166 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 167 | .. c:member:: PyMethodDef* m_methods |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 168 | |
| 169 | A pointer to a table of module-level functions, described by |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 170 | :c:type:`PyMethodDef` values. Can be *NULL* if no functions are present. |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 171 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 172 | .. c:member:: inquiry m_reload |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 173 | |
| 174 | Currently unused, should be *NULL*. |
| 175 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 176 | .. c:member:: traverseproc m_traverse |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 177 | |
| 178 | A traversal function to call during GC traversal of the module object, or |
| 179 | *NULL* if not needed. |
| 180 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 181 | .. c:member:: inquiry m_clear |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 182 | |
| 183 | A clear function to call during GC clearing of the module object, or |
| 184 | *NULL* if not needed. |
| 185 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 186 | .. c:member:: freefunc m_free |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 187 | |
| 188 | A function to call during deallocation of the module object, or *NULL* if |
| 189 | not needed. |
| 190 | |
| 191 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 192 | .. c:function:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 193 | |
| 194 | Add an object to *module* as *name*. This is a convenience function which can |
| 195 | be used from the module's initialization function. This steals a reference to |
| 196 | *value*. Return ``-1`` on error, ``0`` on success. |
| 197 | |
| 198 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 199 | .. c:function:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 200 | |
| 201 | Add an integer constant to *module* as *name*. This convenience function can be |
| 202 | used from the module's initialization function. Return ``-1`` on error, ``0`` on |
| 203 | success. |
| 204 | |
| 205 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 206 | .. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 207 | |
| 208 | Add a string constant to *module* as *name*. This convenience function can be |
| 209 | used from the module's initialization function. The string *value* must be |
| 210 | null-terminated. Return ``-1`` on error, ``0`` on success. |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 211 | |
| 212 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 213 | .. c:function:: int PyModule_AddIntMacro(PyObject *module, macro) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 214 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 215 | Add an int constant to *module*. The name and the value are taken from |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 216 | *macro*. For example ``PyModule_AddConstant(module, AF_INET)`` adds the int |
| 217 | constant *AF_INET* with the value of *AF_INET* to *module*. |
| 218 | Return ``-1`` on error, ``0`` on success. |
| 219 | |
| 220 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 221 | .. c:function:: int PyModule_AddStringMacro(PyObject *module, macro) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 222 | |
| 223 | Add a string constant to *module*. |