Issue #7205: Fix a possible deadlock when using a BZ2File object from several threads...
[python.git] / Doc / c-api / module.rst
blobabac599f2dacb0d2880235379e38d4f534485b6d
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.
25    .. versionchanged:: 2.2
26       Allowed subtypes to be accepted.
29 .. cfunction:: int PyModule_CheckExact(PyObject *p)
31    Return true if *p* is a module object, but not a subtype of
32    :cdata:`PyModule_Type`.
34    .. versionadded:: 2.2
37 .. cfunction:: PyObject* PyModule_New(const char *name)
39    .. index::
40       single: __name__ (module attribute)
41       single: __doc__ (module attribute)
42       single: __file__ (module attribute)
44    Return a new module object with the :attr:`__name__` attribute set to *name*.
45    Only the module's :attr:`__doc__` and :attr:`__name__` attributes are filled in;
46    the caller is responsible for providing a :attr:`__file__` attribute.
49 .. cfunction:: PyObject* PyModule_GetDict(PyObject *module)
51    .. index:: single: __dict__ (module attribute)
53    Return the dictionary object that implements *module*'s namespace; this object
54    is the same as the :attr:`__dict__` attribute of the module object.  This
55    function never fails.  It is recommended extensions use other
56    :cfunc:`PyModule_\*` and :cfunc:`PyObject_\*` functions rather than directly
57    manipulate a module's :attr:`__dict__`.
60 .. cfunction:: char* PyModule_GetName(PyObject *module)
62    .. index::
63       single: __name__ (module attribute)
64       single: SystemError (built-in exception)
66    Return *module*'s :attr:`__name__` value.  If the module does not provide one,
67    or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
70 .. cfunction:: char* PyModule_GetFilename(PyObject *module)
72    .. index::
73       single: __file__ (module attribute)
74       single: SystemError (built-in exception)
76    Return the name of the file from which *module* was loaded using *module*'s
77    :attr:`__file__` attribute.  If this is not defined, or if it is not a string,
78    raise :exc:`SystemError` and return *NULL*.
81 .. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
83    Add an object to *module* as *name*.  This is a convenience function which can
84    be used from the module's initialization function.  This steals a reference to
85    *value*.  Return ``-1`` on error, ``0`` on success.
87    .. versionadded:: 2.0
90 .. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
92    Add an integer constant to *module* as *name*.  This convenience function can be
93    used from the module's initialization function. Return ``-1`` on error, ``0`` on
94    success.
96    .. versionadded:: 2.0
99 .. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
101    Add a string constant to *module* as *name*.  This convenience function can be
102    used from the module's initialization function.  The string *value* must be
103    null-terminated.  Return ``-1`` on error, ``0`` on success.
105    .. versionadded:: 2.0
107 .. cfunction:: int PyModule_AddIntMacro(PyObject *module, macro)
109    Add an int constant to *module*. The name and the value are taken from
110    *macro*. For example ``PyModule_AddConstant(module, AF_INET)`` adds the int
111    constant *AF_INET* with the value of *AF_INET* to *module*.
112    Return ``-1`` on error, ``0`` on success.
114    .. versionadded:: 2.6
116 .. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro)
118    Add a string constant to *module*.
120   .. versionadded:: 2.6