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 | |
| 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 Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 21 | .. cfunction:: 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 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 26 | .. cfunction:: 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 |
| 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 | |
| 67 | .. index:: |
| 68 | single: __file__ (module attribute) |
| 69 | single: SystemError (built-in exception) |
| 70 | |
| 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*. |
| 74 | |
| 75 | |
| 76 | .. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value) |
| 77 | |
| 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. |
| 81 | |
| 82 | |
| 83 | .. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value) |
| 84 | |
| 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 |
| 87 | success. |
| 88 | |
| 89 | |
| 90 | .. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value) |
| 91 | |
| 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. |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 95 | |
| 96 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 97 | .. cfunction:: int PyModule_AddIntMacro(PyObject *module, macro) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 98 | |
| 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. |
| 103 | |
| 104 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 105 | .. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro) |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 106 | |
| 107 | Add a string constant to *module*. |
| 108 | |