/Doc/c-api/veryhigh.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 323 lines · 194 code · 129 blank · 0 comment · 0 complexity · 83fea87e694bc251c423e63e7c152853 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _veryhigh:
  3. *************************
  4. The Very High Level Layer
  5. *************************
  6. The functions in this chapter will let you execute Python source code given in a
  7. file or a buffer, but they will not let you interact in a more detailed way with
  8. the interpreter.
  9. Several of these functions accept a start symbol from the grammar as a
  10. parameter. The available start symbols are :const:`Py_eval_input`,
  11. :const:`Py_file_input`, and :const:`Py_single_input`. These are described
  12. following the functions which accept them as parameters.
  13. Note also that several of these functions take :ctype:`FILE\*` parameters. One
  14. particular issue which needs to be handled carefully is that the :ctype:`FILE`
  15. structure for different C libraries can be different and incompatible. Under
  16. Windows (at least), it is possible for dynamically linked extensions to actually
  17. use different libraries, so care should be taken that :ctype:`FILE\*` parameters
  18. are only passed to these functions if it is certain that they were created by
  19. the same library that the Python runtime is using.
  20. .. cfunction:: int Py_Main(int argc, char **argv)
  21. The main program for the standard interpreter. This is made available for
  22. programs which embed Python. The *argc* and *argv* parameters should be
  23. prepared exactly as those which are passed to a C program's :cfunc:`main`
  24. function. It is important to note that the argument list may be modified (but
  25. the contents of the strings pointed to by the argument list are not). The return
  26. value will be the integer passed to the :func:`sys.exit` function, ``1`` if the
  27. interpreter exits due to an exception, or ``2`` if the parameter list does not
  28. represent a valid Python command line.
  29. Note that if an otherwise unhandled :exc:`SystemError` is raised, this
  30. function will not return ``1``, but exit the process, as long as
  31. ``Py_InspectFlag`` is not set.
  32. .. cfunction:: int PyRun_AnyFile(FILE *fp, const char *filename)
  33. This is a simplified interface to :cfunc:`PyRun_AnyFileExFlags` below, leaving
  34. *closeit* set to ``0`` and *flags* set to *NULL*.
  35. .. cfunction:: int PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
  36. This is a simplified interface to :cfunc:`PyRun_AnyFileExFlags` below, leaving
  37. the *closeit* argument set to ``0``.
  38. .. cfunction:: int PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
  39. This is a simplified interface to :cfunc:`PyRun_AnyFileExFlags` below, leaving
  40. the *flags* argument set to *NULL*.
  41. .. cfunction:: int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
  42. If *fp* refers to a file associated with an interactive device (console or
  43. terminal input or Unix pseudo-terminal), return the value of
  44. :cfunc:`PyRun_InteractiveLoop`, otherwise return the result of
  45. :cfunc:`PyRun_SimpleFile`. If *filename* is *NULL*, this function uses
  46. ``"???"`` as the filename.
  47. .. cfunction:: int PyRun_SimpleString(const char *command)
  48. This is a simplified interface to :cfunc:`PyRun_SimpleStringFlags` below,
  49. leaving the *PyCompilerFlags\** argument set to NULL.
  50. .. cfunction:: int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
  51. Executes the Python source code from *command* in the :mod:`__main__` module
  52. according to the *flags* argument. If :mod:`__main__` does not already exist, it
  53. is created. Returns ``0`` on success or ``-1`` if an exception was raised. If
  54. there was an error, there is no way to get the exception information. For the
  55. meaning of *flags*, see below.
  56. Note that if an otherwise unhandled :exc:`SystemError` is raised, this
  57. function will not return ``-1``, but exit the process, as long as
  58. ``Py_InspectFlag`` is not set.
  59. .. cfunction:: int PyRun_SimpleFile(FILE *fp, const char *filename)
  60. This is a simplified interface to :cfunc:`PyRun_SimpleFileExFlags` below,
  61. leaving *closeit* set to ``0`` and *flags* set to *NULL*.
  62. .. cfunction:: int PyRun_SimpleFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
  63. This is a simplified interface to :cfunc:`PyRun_SimpleFileExFlags` below,
  64. leaving *closeit* set to ``0``.
  65. .. cfunction:: int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
  66. This is a simplified interface to :cfunc:`PyRun_SimpleFileExFlags` below,
  67. leaving *flags* set to *NULL*.
  68. .. cfunction:: int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
  69. Similar to :cfunc:`PyRun_SimpleStringFlags`, but the Python source code is read
  70. from *fp* instead of an in-memory string. *filename* should be the name of the
  71. file. If *closeit* is true, the file is closed before PyRun_SimpleFileExFlags
  72. returns.
  73. .. cfunction:: int PyRun_InteractiveOne(FILE *fp, const char *filename)
  74. This is a simplified interface to :cfunc:`PyRun_InteractiveOneFlags` below,
  75. leaving *flags* set to *NULL*.
  76. .. cfunction:: int PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
  77. Read and execute a single statement from a file associated with an interactive
  78. device according to the *flags* argument. If *filename* is *NULL*, ``"???"`` is
  79. used instead. The user will be prompted using ``sys.ps1`` and ``sys.ps2``.
  80. Returns ``0`` when the input was executed successfully, ``-1`` if there was an
  81. exception, or an error code from the :file:`errcode.h` include file distributed
  82. as part of Python if there was a parse error. (Note that :file:`errcode.h` is
  83. not included by :file:`Python.h`, so must be included specifically if needed.)
  84. .. cfunction:: int PyRun_InteractiveLoop(FILE *fp, const char *filename)
  85. This is a simplified interface to :cfunc:`PyRun_InteractiveLoopFlags` below,
  86. leaving *flags* set to *NULL*.
  87. .. cfunction:: int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
  88. Read and execute statements from a file associated with an interactive device
  89. until EOF is reached. If *filename* is *NULL*, ``"???"`` is used instead. The
  90. user will be prompted using ``sys.ps1`` and ``sys.ps2``. Returns ``0`` at EOF.
  91. .. cfunction:: struct _node* PyParser_SimpleParseString(const char *str, int start)
  92. This is a simplified interface to
  93. :cfunc:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set
  94. to *NULL* and *flags* set to ``0``.
  95. .. cfunction:: struct _node* PyParser_SimpleParseStringFlags( const char *str, int start, int flags)
  96. This is a simplified interface to
  97. :cfunc:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set
  98. to *NULL*.
  99. .. cfunction:: struct _node* PyParser_SimpleParseStringFlagsFilename( const char *str, const char *filename, int start, int flags)
  100. Parse Python source code from *str* using the start token *start* according to
  101. the *flags* argument. The result can be used to create a code object which can
  102. be evaluated efficiently. This is useful if a code fragment must be evaluated
  103. many times.
  104. .. cfunction:: struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
  105. This is a simplified interface to :cfunc:`PyParser_SimpleParseFileFlags` below,
  106. leaving *flags* set to ``0``
  107. .. cfunction:: struct _node* PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
  108. Similar to :cfunc:`PyParser_SimpleParseStringFlagsFilename`, but the Python
  109. source code is read from *fp* instead of an in-memory string.
  110. .. cfunction:: PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
  111. This is a simplified interface to :cfunc:`PyRun_StringFlags` below, leaving
  112. *flags* set to *NULL*.
  113. .. cfunction:: PyObject* PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
  114. Execute Python source code from *str* in the context specified by the
  115. dictionaries *globals* and *locals* with the compiler flags specified by
  116. *flags*. The parameter *start* specifies the start token that should be used to
  117. parse the source code.
  118. Returns the result of executing the code as a Python object, or *NULL* if an
  119. exception was raised.
  120. .. cfunction:: PyObject* PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals)
  121. This is a simplified interface to :cfunc:`PyRun_FileExFlags` below, leaving
  122. *closeit* set to ``0`` and *flags* set to *NULL*.
  123. .. cfunction:: PyObject* PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit)
  124. This is a simplified interface to :cfunc:`PyRun_FileExFlags` below, leaving
  125. *flags* set to *NULL*.
  126. .. cfunction:: PyObject* PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
  127. This is a simplified interface to :cfunc:`PyRun_FileExFlags` below, leaving
  128. *closeit* set to ``0``.
  129. .. cfunction:: PyObject* PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags)
  130. Similar to :cfunc:`PyRun_StringFlags`, but the Python source code is read from
  131. *fp* instead of an in-memory string. *filename* should be the name of the file.
  132. If *closeit* is true, the file is closed before :cfunc:`PyRun_FileExFlags`
  133. returns.
  134. .. cfunction:: PyObject* Py_CompileString(const char *str, const char *filename, int start)
  135. This is a simplified interface to :cfunc:`Py_CompileStringFlags` below, leaving
  136. *flags* set to *NULL*.
  137. .. cfunction:: PyObject* Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags)
  138. Parse and compile the Python source code in *str*, returning the resulting code
  139. object. The start token is given by *start*; this can be used to constrain the
  140. code which can be compiled and should be :const:`Py_eval_input`,
  141. :const:`Py_file_input`, or :const:`Py_single_input`. The filename specified by
  142. *filename* is used to construct the code object and may appear in tracebacks or
  143. :exc:`SyntaxError` exception messages. This returns *NULL* if the code cannot
  144. be parsed or compiled.
  145. .. cfunction:: PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
  146. This is a simplified interface to :cfunc:`PyEval_EvalCodeEx`, with just
  147. the code object, and the dictionaries of global and local variables.
  148. The other arguments are set to *NULL*.
  149. .. cfunction:: PyObject* PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, PyObject **args, int argcount, PyObject **kws, int kwcount, PyObject **defs, int defcount, PyObject *closure)
  150. Evaluate a precompiled code object, given a particular environment for its
  151. evaluation. This environment consists of dictionaries of global and local
  152. variables, arrays of arguments, keywords and defaults, and a closure tuple of
  153. cells.
  154. .. cfunction:: PyObject* PyEval_EvalFrame(PyFrameObject *f)
  155. Evaluate an execution frame. This is a simplified interface to
  156. PyEval_EvalFrameEx, for backward compatibility.
  157. .. cfunction:: PyObject* PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
  158. This is the main, unvarnished function of Python interpretation. It is
  159. literally 2000 lines long. The code object associated with the execution
  160. frame *f* is executed, interpreting bytecode and executing calls as needed.
  161. The additional *throwflag* parameter can mostly be ignored - if true, then
  162. it causes an exception to immediately be thrown; this is used for the
  163. :meth:`throw` methods of generator objects.
  164. .. cfunction:: int PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
  165. This function changes the flags of the current evaluation frame, and returns
  166. true on success, false on failure.
  167. .. cvar:: int Py_eval_input
  168. .. index:: single: Py_CompileString()
  169. The start symbol from the Python grammar for isolated expressions; for use with
  170. :cfunc:`Py_CompileString`.
  171. .. cvar:: int Py_file_input
  172. .. index:: single: Py_CompileString()
  173. The start symbol from the Python grammar for sequences of statements as read
  174. from a file or other source; for use with :cfunc:`Py_CompileString`. This is
  175. the symbol to use when compiling arbitrarily long Python source code.
  176. .. cvar:: int Py_single_input
  177. .. index:: single: Py_CompileString()
  178. The start symbol from the Python grammar for a single statement; for use with
  179. :cfunc:`Py_CompileString`. This is the symbol used for the interactive
  180. interpreter loop.
  181. .. ctype:: struct PyCompilerFlags
  182. This is the structure used to hold compiler flags. In cases where code is only
  183. being compiled, it is passed as ``int flags``, and in cases where code is being
  184. executed, it is passed as ``PyCompilerFlags *flags``. In this case, ``from
  185. __future__ import`` can modify *flags*.
  186. Whenever ``PyCompilerFlags *flags`` is *NULL*, :attr:`cf_flags` is treated as
  187. equal to ``0``, and any modification due to ``from __future__ import`` is
  188. discarded. ::
  189. struct PyCompilerFlags {
  190. int cf_flags;
  191. }
  192. .. cvar:: int CO_FUTURE_DIVISION
  193. This bit can be set in *flags* to cause division operator ``/`` to be
  194. interpreted as "true division" according to :pep:`238`.