Merged revisions 75928 via svnmerge from
[python/dscho.git] / Doc / c-api / module.rst
blob2c7faf0d2e3b15361b8adf344f774d581b800658
1 .. highlightlang:: c
3 .. _moduleobjects:
5 Module Objects
6 --------------
8 .. index:: object: module
10 There are only a few functions special to module objects.
13 .. cvar:: PyTypeObject PyModule_Type
15    .. index:: single: ModuleType (in module types)
17    This instance of :ctype:`PyTypeObject` represents the Python module type.  This
18    is exposed to Python programs as ``types.ModuleType``.
21 .. cfunction:: int PyModule_Check(PyObject *p)
23    Return true if *p* is a module object, or a subtype of a module object.
26 .. cfunction:: int PyModule_CheckExact(PyObject *p)
28    Return true if *p* is a module object, but not a subtype of
29    :cdata:`PyModule_Type`.
32 .. cfunction:: PyObject* PyModule_New(const char *name)
34    .. index::
35       single: __name__ (module attribute)
36       single: __doc__ (module attribute)
37       single: __file__ (module attribute)
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.
44 .. cfunction:: PyObject* PyModule_GetDict(PyObject *module)
46    .. index:: single: __dict__ (module attribute)
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__`.
55 .. cfunction:: char* PyModule_GetName(PyObject *module)
57    .. index::
58       single: __name__ (module attribute)
59       single: SystemError (built-in exception)
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.
65 .. cfunction:: char* PyModule_GetFilename(PyObject *module)
67    .. index::
68       single: __file__ (module attribute)
69       single: SystemError (built-in exception)
71    Return the name of the file from which *module* was loaded using *module*'s
72    :attr:`__file__` attribute.  If this is not defined, or if it is not a string,
73    raise :exc:`SystemError` and return *NULL*.
76 .. cfunction:: void* PyModule_GetState(PyObject *module)
78    Return the "state" of the module, that is, a pointer to the block of memory
79    allocated at module creation time, or *NULL*.  See
80    :cmember:`PyModuleDef.m_size`.
83 .. cfunction:: PyModuleDef* PyModule_GetDef(PyObject *module)
85    Return a pointer to the :ctype:`PyModuleDef` struct from which the module was
86    created, or *NULL* if the module wasn't created with
87    :cfunc:`PyModule_Create`.
90 Initializing C modules
91 ^^^^^^^^^^^^^^^^^^^^^^
93 These functions are usually used in the module initialization function.
95 .. cfunction:: PyObject* PyModule_Create(PyModuleDef *module)
97    Create a new module object, given the definition in *module*.  This behaves
98    like :cfunc:`PyModule_Create2` with *module_api_version* set to
99    :const:`PYTHON_API_VERSION`.
102 .. cfunction:: PyObject* PyModule_Create2(PyModuleDef *module, int module_api_version)
104    Create a new module object, given the definition in *module*, assuming the
105    API version *module_api_version*.  If that version does not match the version
106    of the running interpreter, a :exc:`RuntimeWarning` is emitted.
108    .. note::
110       Most uses of this function should be using :cfunc:`PyModule_Create`
111       instead; only use this if you are sure you need it.
114 .. ctype:: PyModuleDef
116    This struct holds all information that is needed to create a module object.
117    There is usually only one static variable of that type for each module, which
118    is statically initialized and then passed to :cfunc:`PyModule_Create` in the
119    module initialization function.
121    .. cmember:: PyModuleDef_Base m_base
123       Always initialize this member to :const:`PyModuleDef_HEAD_INIT`.
125    .. cmember:: char* m_name
127       Name for the new module.
129    .. cmember:: char* m_doc
131       Docstring for the module; usually a docstring variable created with
132       :cfunc:`PyDoc_STRVAR` is used.
134    .. cmember:: Py_ssize_t m_size
136       If the module object needs additional memory, this should be set to the
137       number of bytes to allocate; a pointer to the block of memory can be
138       retrieved with :cfunc:`PyModule_GetState`.  If no memory is needed, set
139       this to ``-1``.
141       This memory should be used, rather than static globals, to hold per-module
142       state, since it is then safe for use in multiple sub-interpreters.  It is
143       freed when the module object is deallocated, after the :cmember:`m_free`
144       function has been called, if present.
146    .. cmember:: PyMethodDef* m_methods
148       A pointer to a table of module-level functions, described by
149       :ctype:`PyMethodDef` values.  Can be *NULL* if no functions are present.
151    .. cmember:: inquiry m_reload
153       Currently unused, should be *NULL*.
155    .. cmember:: traverseproc m_traverse
157       A traversal function to call during GC traversal of the module object, or
158       *NULL* if not needed.
160    .. cmember:: inquiry m_clear
162       A clear function to call during GC clearing of the module object, or
163       *NULL* if not needed.
165    .. cmember:: freefunc m_free
167       A function to call during deallocation of the module object, or *NULL* if
168       not needed.
171 .. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
173    Add an object to *module* as *name*.  This is a convenience function which can
174    be used from the module's initialization function.  This steals a reference to
175    *value*.  Return ``-1`` on error, ``0`` on success.
178 .. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
180    Add an integer constant to *module* as *name*.  This convenience function can be
181    used from the module's initialization function. Return ``-1`` on error, ``0`` on
182    success.
185 .. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
187    Add a string constant to *module* as *name*.  This convenience function can be
188    used from the module's initialization function.  The string *value* must be
189    null-terminated.  Return ``-1`` on error, ``0`` on success.
192 .. cfunction:: int PyModule_AddIntMacro(PyObject *module, macro)
194    Add an int constant to *module*. The name and the value are taken from
195    *macro*. For example ``PyModule_AddConstant(module, AF_INET)`` adds the int
196    constant *AF_INET* with the value of *AF_INET* to *module*.
197    Return ``-1`` on error, ``0`` on success.
200 .. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro)
202    Add a string constant to *module*.