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