/Doc/c-api/module.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 121 lines · 70 code · 51 blank · 0 comment · 0 complexity · 7330e1c56698f94186447897d139e412 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _moduleobjects:
  3. Module Objects
  4. --------------
  5. .. index:: object: module
  6. There are only a few functions special to module objects.
  7. .. cvar:: PyTypeObject PyModule_Type
  8. .. index:: single: ModuleType (in module types)
  9. This instance of :ctype:`PyTypeObject` represents the Python module type. This
  10. is exposed to Python programs as ``types.ModuleType``.
  11. .. cfunction:: int PyModule_Check(PyObject *p)
  12. Return true if *p* is a module object, or a subtype of a module object.
  13. .. versionchanged:: 2.2
  14. Allowed subtypes to be accepted.
  15. .. cfunction:: int PyModule_CheckExact(PyObject *p)
  16. Return true if *p* is a module object, but not a subtype of
  17. :cdata:`PyModule_Type`.
  18. .. versionadded:: 2.2
  19. .. cfunction:: PyObject* PyModule_New(const char *name)
  20. .. index::
  21. single: __name__ (module attribute)
  22. single: __doc__ (module attribute)
  23. single: __file__ (module attribute)
  24. Return a new module object with the :attr:`__name__` attribute set to *name*.
  25. Only the module's :attr:`__doc__` and :attr:`__name__` attributes are filled in;
  26. the caller is responsible for providing a :attr:`__file__` attribute.
  27. .. cfunction:: PyObject* PyModule_GetDict(PyObject *module)
  28. .. index:: single: __dict__ (module attribute)
  29. Return the dictionary object that implements *module*'s namespace; this object
  30. is the same as the :attr:`__dict__` attribute of the module object. This
  31. function never fails. It is recommended extensions use other
  32. :cfunc:`PyModule_\*` and :cfunc:`PyObject_\*` functions rather than directly
  33. manipulate a module's :attr:`__dict__`.
  34. .. cfunction:: char* PyModule_GetName(PyObject *module)
  35. .. index::
  36. single: __name__ (module attribute)
  37. single: SystemError (built-in exception)
  38. Return *module*'s :attr:`__name__` value. If the module does not provide one,
  39. or if it is not a string, :exc:`SystemError` is raised and *NULL* is returned.
  40. .. cfunction:: char* PyModule_GetFilename(PyObject *module)
  41. .. index::
  42. single: __file__ (module attribute)
  43. single: SystemError (built-in exception)
  44. Return the name of the file from which *module* was loaded using *module*'s
  45. :attr:`__file__` attribute. If this is not defined, or if it is not a string,
  46. raise :exc:`SystemError` and return *NULL*.
  47. .. cfunction:: int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)
  48. Add an object to *module* as *name*. This is a convenience function which can
  49. be used from the module's initialization function. This steals a reference to
  50. *value*. Return ``-1`` on error, ``0`` on success.
  51. .. versionadded:: 2.0
  52. .. cfunction:: int PyModule_AddIntConstant(PyObject *module, const char *name, long value)
  53. Add an integer constant to *module* as *name*. This convenience function can be
  54. used from the module's initialization function. Return ``-1`` on error, ``0`` on
  55. success.
  56. .. versionadded:: 2.0
  57. .. cfunction:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
  58. Add a string constant to *module* as *name*. This convenience function can be
  59. used from the module's initialization function. The string *value* must be
  60. null-terminated. Return ``-1`` on error, ``0`` on success.
  61. .. versionadded:: 2.0
  62. .. cfunction:: int PyModule_AddIntMacro(PyObject *module, macro)
  63. Add an int constant to *module*. The name and the value are taken from
  64. *macro*. For example ``PyModule_AddConstant(module, AF_INET)`` adds the int
  65. constant *AF_INET* with the value of *AF_INET* to *module*.
  66. Return ``-1`` on error, ``0`` on success.
  67. .. versionadded:: 2.6
  68. .. cfunction:: int PyModule_AddStringMacro(PyObject *module, macro)
  69. Add a string constant to *module*.
  70. .. versionadded:: 2.6