blob: d1940a8d9db347728a4cf07676b5aeb3329bac81 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. highlightlang:: c
2
3
4.. _veryhigh:
5
6*************************
7The Very High Level Layer
8*************************
9
10The functions in this chapter will let you execute Python source code given in a
11file or a buffer, but they will not let you interact in a more detailed way with
12the interpreter.
13
14Several of these functions accept a start symbol from the grammar as a
15parameter. The available start symbols are :const:`Py_eval_input`,
16:const:`Py_file_input`, and :const:`Py_single_input`. These are described
17following the functions which accept them as parameters.
18
Benjamin Petersonad3d5c22009-02-26 03:38:59 +000019Note also that several of these functions take :ctype:`FILE\*` parameters. One
Georg Brandl116aa622007-08-15 14:28:22 +000020particular issue which needs to be handled carefully is that the :ctype:`FILE`
21structure for different C libraries can be different and incompatible. Under
22Windows (at least), it is possible for dynamically linked extensions to actually
23use different libraries, so care should be taken that :ctype:`FILE\*` parameters
24are only passed to these functions if it is certain that they were created by
25the same library that the Python runtime is using.
26
27
Martin v. Löwis790465f2008-04-05 20:41:37 +000028.. cfunction:: int Py_Main(int argc, wchar_t **argv)
Georg Brandl116aa622007-08-15 14:28:22 +000029
Martin v. Löwis790465f2008-04-05 20:41:37 +000030 The main program for the standard interpreter. This is made
31 available for programs which embed Python. The *argc* and *argv*
32 parameters should be prepared exactly as those which are passed to
33 a C program's :cfunc:`main` function (converted to wchar_t
34 according to the user's locale). It is important to note that the
35 argument list may be modified (but the contents of the strings
36 pointed to by the argument list are not). The return value will be
Georg Brandl0b2489e2011-05-15 08:49:12 +020037 ```0``` if the interpreter exits normally (ie, without an
38 exception), ``1`` if the interpreter exits due to an exception, or
39 ``2`` if the parameter list does not represent a valid Python
40 command line.
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl0b2489e2011-05-15 08:49:12 +020042 Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
Benjamin Petersond23f8222009-04-05 19:13:16 +000043 function will not return ``1``, but exit the process, as long as
44 ``Py_InspectFlag`` is not set.
45
Georg Brandl116aa622007-08-15 14:28:22 +000046
47.. cfunction:: int PyRun_AnyFile(FILE *fp, const char *filename)
48
49 This is a simplified interface to :cfunc:`PyRun_AnyFileExFlags` below, leaving
50 *closeit* set to ``0`` and *flags* set to *NULL*.
51
52
53.. cfunction:: int PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
54
55 This is a simplified interface to :cfunc:`PyRun_AnyFileExFlags` below, leaving
56 the *closeit* argument set to ``0``.
57
58
59.. cfunction:: int PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)
60
61 This is a simplified interface to :cfunc:`PyRun_AnyFileExFlags` below, leaving
62 the *flags* argument set to *NULL*.
63
64
65.. cfunction:: int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
66
67 If *fp* refers to a file associated with an interactive device (console or
68 terminal input or Unix pseudo-terminal), return the value of
69 :cfunc:`PyRun_InteractiveLoop`, otherwise return the result of
70 :cfunc:`PyRun_SimpleFile`. If *filename* is *NULL*, this function uses
71 ``"???"`` as the filename.
72
73
74.. cfunction:: int PyRun_SimpleString(const char *command)
75
76 This is a simplified interface to :cfunc:`PyRun_SimpleStringFlags` below,
77 leaving the *PyCompilerFlags\** argument set to NULL.
78
79
80.. cfunction:: int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
81
82 Executes the Python source code from *command* in the :mod:`__main__` module
83 according to the *flags* argument. If :mod:`__main__` does not already exist, it
84 is created. Returns ``0`` on success or ``-1`` if an exception was raised. If
85 there was an error, there is no way to get the exception information. For the
86 meaning of *flags*, see below.
87
Georg Brandl0b2489e2011-05-15 08:49:12 +020088 Note that if an otherwise unhandled :exc:`SystemExit` is raised, this
Benjamin Petersond23f8222009-04-05 19:13:16 +000089 function will not return ``-1``, but exit the process, as long as
90 ``Py_InspectFlag`` is not set.
91
Georg Brandl116aa622007-08-15 14:28:22 +000092
93.. cfunction:: int PyRun_SimpleFile(FILE *fp, const char *filename)
94
95 This is a simplified interface to :cfunc:`PyRun_SimpleFileExFlags` below,
96 leaving *closeit* set to ``0`` and *flags* set to *NULL*.
97
98
99.. cfunction:: int PyRun_SimpleFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
100
101 This is a simplified interface to :cfunc:`PyRun_SimpleFileExFlags` below,
102 leaving *closeit* set to ``0``.
103
104
105.. cfunction:: int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)
106
107 This is a simplified interface to :cfunc:`PyRun_SimpleFileExFlags` below,
108 leaving *flags* set to *NULL*.
109
110
111.. cfunction:: int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)
112
113 Similar to :cfunc:`PyRun_SimpleStringFlags`, but the Python source code is read
114 from *fp* instead of an in-memory string. *filename* should be the name of the
115 file. If *closeit* is true, the file is closed before PyRun_SimpleFileExFlags
116 returns.
117
118
119.. cfunction:: int PyRun_InteractiveOne(FILE *fp, const char *filename)
120
121 This is a simplified interface to :cfunc:`PyRun_InteractiveOneFlags` below,
122 leaving *flags* set to *NULL*.
123
124
125.. cfunction:: int PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
126
Georg Brandlab32fec2010-11-26 08:49:15 +0000127 Read and execute a single statement from a file associated with an
128 interactive device according to the *flags* argument. The user will be
129 prompted using ``sys.ps1`` and ``sys.ps2``. Returns ``0`` when the input was
130 executed successfully, ``-1`` if there was an exception, or an error code
131 from the :file:`errcode.h` include file distributed as part of Python if
132 there was a parse error. (Note that :file:`errcode.h` is not included by
133 :file:`Python.h`, so must be included specifically if needed.)
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
136.. cfunction:: int PyRun_InteractiveLoop(FILE *fp, const char *filename)
137
138 This is a simplified interface to :cfunc:`PyRun_InteractiveLoopFlags` below,
139 leaving *flags* set to *NULL*.
140
141
Georg Brandlab32fec2010-11-26 08:49:15 +0000142.. cfunction:: int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000143
144 Read and execute statements from a file associated with an interactive device
Georg Brandlab32fec2010-11-26 08:49:15 +0000145 until EOF is reached. The user will be prompted using ``sys.ps1`` and
146 ``sys.ps2``. Returns ``0`` at EOF.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
149.. cfunction:: struct _node* PyParser_SimpleParseString(const char *str, int start)
150
151 This is a simplified interface to
152 :cfunc:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set
153 to *NULL* and *flags* set to ``0``.
154
155
156.. cfunction:: struct _node* PyParser_SimpleParseStringFlags( const char *str, int start, int flags)
157
158 This is a simplified interface to
159 :cfunc:`PyParser_SimpleParseStringFlagsFilename` below, leaving *filename* set
160 to *NULL*.
161
162
163.. cfunction:: struct _node* PyParser_SimpleParseStringFlagsFilename( const char *str, const char *filename, int start, int flags)
164
165 Parse Python source code from *str* using the start token *start* according to
166 the *flags* argument. The result can be used to create a code object which can
167 be evaluated efficiently. This is useful if a code fragment must be evaluated
168 many times.
169
170
171.. cfunction:: struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
172
173 This is a simplified interface to :cfunc:`PyParser_SimpleParseFileFlags` below,
174 leaving *flags* set to ``0``
175
176
177.. cfunction:: struct _node* PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
178
179 Similar to :cfunc:`PyParser_SimpleParseStringFlagsFilename`, but the Python
180 source code is read from *fp* instead of an in-memory string.
181
182
183.. cfunction:: PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)
184
185 This is a simplified interface to :cfunc:`PyRun_StringFlags` below, leaving
186 *flags* set to *NULL*.
187
188
189.. cfunction:: PyObject* PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
190
191 Execute Python source code from *str* in the context specified by the
192 dictionaries *globals* and *locals* with the compiler flags specified by
193 *flags*. The parameter *start* specifies the start token that should be used to
194 parse the source code.
195
196 Returns the result of executing the code as a Python object, or *NULL* if an
197 exception was raised.
198
199
200.. cfunction:: PyObject* PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals)
201
202 This is a simplified interface to :cfunc:`PyRun_FileExFlags` below, leaving
203 *closeit* set to ``0`` and *flags* set to *NULL*.
204
205
206.. cfunction:: PyObject* PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit)
207
208 This is a simplified interface to :cfunc:`PyRun_FileExFlags` below, leaving
209 *flags* set to *NULL*.
210
211
212.. cfunction:: PyObject* PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)
213
214 This is a simplified interface to :cfunc:`PyRun_FileExFlags` below, leaving
215 *closeit* set to ``0``.
216
217
218.. cfunction:: PyObject* PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags)
219
220 Similar to :cfunc:`PyRun_StringFlags`, but the Python source code is read from
221 *fp* instead of an in-memory string. *filename* should be the name of the file.
222 If *closeit* is true, the file is closed before :cfunc:`PyRun_FileExFlags`
223 returns.
224
225
226.. cfunction:: PyObject* Py_CompileString(const char *str, const char *filename, int start)
227
228 This is a simplified interface to :cfunc:`Py_CompileStringFlags` below, leaving
229 *flags* set to *NULL*.
230
231
232.. cfunction:: PyObject* Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags)
233
234 Parse and compile the Python source code in *str*, returning the resulting code
235 object. The start token is given by *start*; this can be used to constrain the
236 code which can be compiled and should be :const:`Py_eval_input`,
237 :const:`Py_file_input`, or :const:`Py_single_input`. The filename specified by
238 *filename* is used to construct the code object and may appear in tracebacks or
239 :exc:`SyntaxError` exception messages. This returns *NULL* if the code cannot
240 be parsed or compiled.
241
242
Christian Heimesd8654cf2007-12-02 15:22:16 +0000243.. cfunction:: PyObject* PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
244
245 This is a simplified interface to :cfunc:`PyEval_EvalCodeEx`, with just
246 the code object, and the dictionaries of global and local variables.
247 The other arguments are set to *NULL*.
248
249
250.. cfunction:: PyObject* PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, PyObject **args, int argcount, PyObject **kws, int kwcount, PyObject **defs, int defcount, PyObject *closure)
251
252 Evaluate a precompiled code object, given a particular environment for its
253 evaluation. This environment consists of dictionaries of global and local
254 variables, arrays of arguments, keywords and defaults, and a closure tuple of
255 cells.
256
257
258.. cfunction:: PyObject* PyEval_EvalFrame(PyFrameObject *f)
259
260 Evaluate an execution frame. This is a simplified interface to
261 PyEval_EvalFrameEx, for backward compatibility.
262
263
264.. cfunction:: PyObject* PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
265
266 This is the main, unvarnished function of Python interpretation. It is
267 literally 2000 lines long. The code object associated with the execution
268 frame *f* is executed, interpreting bytecode and executing calls as needed.
269 The additional *throwflag* parameter can mostly be ignored - if true, then
270 it causes an exception to immediately be thrown; this is used for the
271 :meth:`throw` methods of generator objects.
272
273
274.. cfunction:: int PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
275
276 This function changes the flags of the current evaluation frame, and returns
277 true on success, false on failure.
278
279
Georg Brandl116aa622007-08-15 14:28:22 +0000280.. cvar:: int Py_eval_input
281
282 .. index:: single: Py_CompileString()
283
284 The start symbol from the Python grammar for isolated expressions; for use with
285 :cfunc:`Py_CompileString`.
286
287
288.. cvar:: int Py_file_input
289
290 .. index:: single: Py_CompileString()
291
292 The start symbol from the Python grammar for sequences of statements as read
293 from a file or other source; for use with :cfunc:`Py_CompileString`. This is
294 the symbol to use when compiling arbitrarily long Python source code.
295
296
297.. cvar:: int Py_single_input
298
299 .. index:: single: Py_CompileString()
300
301 The start symbol from the Python grammar for a single statement; for use with
302 :cfunc:`Py_CompileString`. This is the symbol used for the interactive
303 interpreter loop.
304
305
306.. ctype:: struct PyCompilerFlags
307
308 This is the structure used to hold compiler flags. In cases where code is only
309 being compiled, it is passed as ``int flags``, and in cases where code is being
310 executed, it is passed as ``PyCompilerFlags *flags``. In this case, ``from
311 __future__ import`` can modify *flags*.
312
313 Whenever ``PyCompilerFlags *flags`` is *NULL*, :attr:`cf_flags` is treated as
314 equal to ``0``, and any modification due to ``from __future__ import`` is
315 discarded. ::
316
317 struct PyCompilerFlags {
318 int cf_flags;
319 }
320
321
322.. cvar:: int CO_FUTURE_DIVISION
323
324 This bit can be set in *flags* to cause division operator ``/`` to be
325 interpreted as "true division" according to :pep:`238`.
326