/Doc/c-api/import.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 267 lines · 176 code · 91 blank · 0 comment · 0 complexity · 8c2531a1a00b4d111d968fb11a5ab71f MD5 · raw file

  1. .. highlightlang:: c
  2. .. _importing:
  3. Importing Modules
  4. =================
  5. .. cfunction:: PyObject* PyImport_ImportModule(const char *name)
  6. .. index::
  7. single: package variable; __all__
  8. single: __all__ (package variable)
  9. single: modules (in module sys)
  10. This is a simplified interface to :cfunc:`PyImport_ImportModuleEx` below,
  11. leaving the *globals* and *locals* arguments set to *NULL* and *level* set
  12. to 0. When the *name*
  13. argument contains a dot (when it specifies a submodule of a package), the
  14. *fromlist* argument is set to the list ``['*']`` so that the return value is the
  15. named module rather than the top-level package containing it as would otherwise
  16. be the case. (Unfortunately, this has an additional side effect when *name* in
  17. fact specifies a subpackage instead of a submodule: the submodules specified in
  18. the package's ``__all__`` variable are loaded.) Return a new reference to the
  19. imported module, or *NULL* with an exception set on failure. Before Python 2.4,
  20. the module may still be created in the failure case --- examine ``sys.modules``
  21. to find out. Starting with Python 2.4, a failing import of a module no longer
  22. leaves the module in ``sys.modules``.
  23. .. versionchanged:: 2.4
  24. failing imports remove incomplete module objects.
  25. .. versionchanged:: 2.6
  26. always use absolute imports
  27. .. cfunction:: PyObject* PyImport_ImportModuleNoBlock(const char *name)
  28. This version of :cfunc:`PyImport_ImportModule` does not block. It's intended
  29. to be used in C functions that import other modules to execute a function.
  30. The import may block if another thread holds the import lock. The function
  31. :cfunc:`PyImport_ImportModuleNoBlock` never blocks. It first tries to fetch
  32. the module from sys.modules and falls back to :cfunc:`PyImport_ImportModule`
  33. unless the lock is held, in which case the function will raise an
  34. :exc:`ImportError`.
  35. .. versionadded:: 2.6
  36. .. cfunction:: PyObject* PyImport_ImportModuleEx(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)
  37. .. index:: builtin: __import__
  38. Import a module. This is best described by referring to the built-in Python
  39. function :func:`__import__`, as the standard :func:`__import__` function calls
  40. this function directly.
  41. The return value is a new reference to the imported module or top-level package,
  42. or *NULL* with an exception set on failure (before Python 2.4, the module may
  43. still be created in this case). Like for :func:`__import__`, the return value
  44. when a submodule of a package was requested is normally the top-level package,
  45. unless a non-empty *fromlist* was given.
  46. .. versionchanged:: 2.4
  47. failing imports remove incomplete module objects.
  48. .. versionchanged:: 2.6
  49. The function is an alias for :cfunc:`PyImport_ImportModuleLevel` with
  50. -1 as level, meaning relative import.
  51. .. cfunction:: PyObject* PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)
  52. Import a module. This is best described by referring to the built-in Python
  53. function :func:`__import__`, as the standard :func:`__import__` function calls
  54. this function directly.
  55. The return value is a new reference to the imported module or top-level package,
  56. or *NULL* with an exception set on failure. Like for :func:`__import__`,
  57. the return value when a submodule of a package was requested is normally the
  58. top-level package, unless a non-empty *fromlist* was given.
  59. .. versionadded:: 2.5
  60. .. cfunction:: PyObject* PyImport_Import(PyObject *name)
  61. .. index::
  62. module: rexec
  63. module: ihooks
  64. This is a higher-level interface that calls the current "import hook function".
  65. It invokes the :func:`__import__` function from the ``__builtins__`` of the
  66. current globals. This means that the import is done using whatever import hooks
  67. are installed in the current environment, e.g. by :mod:`rexec` or :mod:`ihooks`.
  68. .. versionchanged:: 2.6
  69. always use absolute imports
  70. .. cfunction:: PyObject* PyImport_ReloadModule(PyObject *m)
  71. .. index:: builtin: reload
  72. Reload a module. This is best described by referring to the built-in Python
  73. function :func:`reload`, as the standard :func:`reload` function calls this
  74. function directly. Return a new reference to the reloaded module, or *NULL*
  75. with an exception set on failure (the module still exists in this case).
  76. .. cfunction:: PyObject* PyImport_AddModule(const char *name)
  77. Return the module object corresponding to a module name. The *name* argument
  78. may be of the form ``package.module``. First check the modules dictionary if
  79. there's one there, and if not, create a new one and insert it in the modules
  80. dictionary. Return *NULL* with an exception set on failure.
  81. .. note::
  82. This function does not load or import the module; if the module wasn't already
  83. loaded, you will get an empty module object. Use :cfunc:`PyImport_ImportModule`
  84. or one of its variants to import a module. Package structures implied by a
  85. dotted name for *name* are not created if not already present.
  86. .. cfunction:: PyObject* PyImport_ExecCodeModule(char *name, PyObject *co)
  87. .. index:: builtin: compile
  88. Given a module name (possibly of the form ``package.module``) and a code object
  89. read from a Python bytecode file or obtained from the built-in function
  90. :func:`compile`, load the module. Return a new reference to the module object,
  91. or *NULL* with an exception set if an error occurred. Before Python 2.4, the
  92. module could still be created in error cases. Starting with Python 2.4, *name*
  93. is removed from :attr:`sys.modules` in error cases, and even if *name* was already
  94. in :attr:`sys.modules` on entry to :cfunc:`PyImport_ExecCodeModule`. Leaving
  95. incompletely initialized modules in :attr:`sys.modules` is dangerous, as imports of
  96. such modules have no way to know that the module object is an unknown (and
  97. probably damaged with respect to the module author's intents) state.
  98. This function will reload the module if it was already imported. See
  99. :cfunc:`PyImport_ReloadModule` for the intended way to reload a module.
  100. If *name* points to a dotted name of the form ``package.module``, any package
  101. structures not already created will still not be created.
  102. .. versionchanged:: 2.4
  103. *name* is removed from :attr:`sys.modules` in error cases.
  104. .. cfunction:: long PyImport_GetMagicNumber()
  105. Return the magic number for Python bytecode files (a.k.a. :file:`.pyc` and
  106. :file:`.pyo` files). The magic number should be present in the first four bytes
  107. of the bytecode file, in little-endian byte order.
  108. .. cfunction:: PyObject* PyImport_GetModuleDict()
  109. Return the dictionary used for the module administration (a.k.a.
  110. ``sys.modules``). Note that this is a per-interpreter variable.
  111. .. cfunction:: PyObject* PyImport_GetImporter(PyObject *path)
  112. Return an importer object for a :data:`sys.path`/:attr:`pkg.__path__` item
  113. *path*, possibly by fetching it from the :data:`sys.path_importer_cache`
  114. dict. If it wasn't yet cached, traverse :data:`sys.path_hooks` until a hook
  115. is found that can handle the path item. Return ``None`` if no hook could;
  116. this tells our caller it should fall back to the builtin import mechanism.
  117. Cache the result in :data:`sys.path_importer_cache`. Return a new reference
  118. to the importer object.
  119. .. versionadded:: 2.6
  120. .. cfunction:: void _PyImport_Init()
  121. Initialize the import mechanism. For internal use only.
  122. .. cfunction:: void PyImport_Cleanup()
  123. Empty the module table. For internal use only.
  124. .. cfunction:: void _PyImport_Fini()
  125. Finalize the import mechanism. For internal use only.
  126. .. cfunction:: PyObject* _PyImport_FindExtension(char *, char *)
  127. For internal use only.
  128. .. cfunction:: PyObject* _PyImport_FixupExtension(char *, char *)
  129. For internal use only.
  130. .. cfunction:: int PyImport_ImportFrozenModule(char *name)
  131. Load a frozen module named *name*. Return ``1`` for success, ``0`` if the
  132. module is not found, and ``-1`` with an exception set if the initialization
  133. failed. To access the imported module on a successful load, use
  134. :cfunc:`PyImport_ImportModule`. (Note the misnomer --- this function would
  135. reload the module if it was already imported.)
  136. .. ctype:: struct _frozen
  137. .. index:: single: freeze utility
  138. This is the structure type definition for frozen module descriptors, as
  139. generated by the :program:`freeze` utility (see :file:`Tools/freeze/` in the
  140. Python source distribution). Its definition, found in :file:`Include/import.h`,
  141. is::
  142. struct _frozen {
  143. char *name;
  144. unsigned char *code;
  145. int size;
  146. };
  147. .. cvar:: struct _frozen* PyImport_FrozenModules
  148. This pointer is initialized to point to an array of :ctype:`struct _frozen`
  149. records, terminated by one whose members are all *NULL* or zero. When a frozen
  150. module is imported, it is searched in this table. Third-party code could play
  151. tricks with this to provide a dynamically created collection of frozen modules.
  152. .. cfunction:: int PyImport_AppendInittab(char *name, void (*initfunc)(void))
  153. Add a single module to the existing table of built-in modules. This is a
  154. convenience wrapper around :cfunc:`PyImport_ExtendInittab`, returning ``-1`` if
  155. the table could not be extended. The new module can be imported by the name
  156. *name*, and uses the function *initfunc* as the initialization function called
  157. on the first attempted import. This should be called before
  158. :cfunc:`Py_Initialize`.
  159. .. ctype:: struct _inittab
  160. Structure describing a single entry in the list of built-in modules. Each of
  161. these structures gives the name and initialization function for a module built
  162. into the interpreter. Programs which embed Python may use an array of these
  163. structures in conjunction with :cfunc:`PyImport_ExtendInittab` to provide
  164. additional built-in modules. The structure is defined in
  165. :file:`Include/import.h` as::
  166. struct _inittab {
  167. char *name;
  168. void (*initfunc)(void);
  169. };
  170. .. cfunction:: int PyImport_ExtendInittab(struct _inittab *newtab)
  171. Add a collection of modules to the table of built-in modules. The *newtab*
  172. array must end with a sentinel entry which contains *NULL* for the :attr:`name`
  173. field; failure to provide the sentinel value can result in a memory fault.
  174. Returns ``0`` on success or ``-1`` if insufficient memory could be allocated to
  175. extend the internal table. In the event of failure, no modules are added to the
  176. internal table. This should be called before :cfunc:`Py_Initialize`.