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)
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)
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)
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:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
78 Add an object to *module* as *name*. This is a convenience function which can
79 be used from the module's initialization function. This steals a reference to
80 *value*. Return ``-1`` on error, ``0`` on success.
83 .. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
85 Add an integer constant to *module* as *name*. This convenience function can be
86 used from the module's initialization function. Return ``-1`` on error, ``0`` on
90 .. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
92 Add a string constant to *module* as *name*. This convenience function can be
93 used from the module's initialization function. The string *value* must be
94 null-terminated. Return ``-1`` on error, ``0`` on success.
97 .. cfunction:: int PyModule_AddIntMacro(PyObject *module, macro)
99 Add an int constant to *module*. The name and the value are taken from
100 *macro*. For example ``PyModule_AddConstant(module, AF_INET)`` adds the int
101 constant *AF_INET* with the value of *AF_INET* to *module*.
102 Return ``-1`` on error, ``0`` on success.
105 .. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro)
107 Add a string constant to *module*.