blob: e7cb09460720565c37dd36e153b3b32ba58ae1bb [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{The Very High Level Layer \label{veryhigh}}
2
3
4The functions in this chapter will let you execute Python source code
5given in a file or a buffer, but they will not let you interact in a
6more detailed way with the interpreter.
7
8Several of these functions accept a start symbol from the grammar as a
9parameter. The available start symbols are \constant{Py_eval_input},
10\constant{Py_file_input}, and \constant{Py_single_input}. These are
11described following the functions which accept them as parameters.
12
13Note also that several of these functions take \ctype{FILE*}
14parameters. On particular issue which needs to be handled carefully
15is that the \ctype{FILE} structure for different C libraries can be
16different and incompatible. Under Windows (at least), it is possible
17for dynamically linked extensions to actually use different libraries,
18so care should be taken that \ctype{FILE*} parameters are only passed
19to these functions if it is certain that they were created by the same
20library that the Python runtime is using.
21
22
23\begin{cfuncdesc}{int}{Py_Main}{int argc, char **argv}
24 The main program for the standard interpreter. This is made
25 available for programs which embed Python. The \var{argc} and
26 \var{argv} parameters should be prepared exactly as those which are
27 passed to a C program's \cfunction{main()} function. It is
28 important to note that the argument list may be modified (but the
29 contents of the strings pointed to by the argument list are not).
30 The return value will be the integer passed to the
31 \function{sys.exit()} function, \code{1} if the interpreter exits
32 due to an exception, or \code{2} if the parameter list does not
33 represent a valid Python command line.
34\end{cfuncdesc}
35
36\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *fp, char *filename}
37 If \var{fp} refers to a file associated with an interactive device
38 (console or terminal input or \UNIX{} pseudo-terminal), return the
39 value of \cfunction{PyRun_InteractiveLoop()}, otherwise return the
40 result of \cfunction{PyRun_SimpleFile()}. If \var{filename} is
41 \NULL, this function uses \code{"???"} as the filename.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *command}
45 Executes the Python source code from \var{command} in the
46 \module{__main__} module. If \module{__main__} does not already
47 exist, it is created. Returns \code{0} on success or \code{-1} if
48 an exception was raised. If there was an error, there is no way to
49 get the exception information.
50\end{cfuncdesc}
51
52\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *fp, char *filename}
53 Similar to \cfunction{PyRun_SimpleString()}, but the Python source
54 code is read from \var{fp} instead of an in-memory string.
55 \var{filename} should be the name of the file.
56\end{cfuncdesc}
57
58\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *fp, char *filename}
59 Read and execute a single statement from a file associated with an
60 interactive device. If \var{filename} is \NULL, \code{"???"} is
61 used instead. The user will be prompted using \code{sys.ps1} and
62 \code{sys.ps2}. Returns \code{0} when the input was executed
63 successfully, \code{-1} if there was an exception, or an error code
64 from the \file{errcode.h} include file distributed as part of Python
65 if there was a parse error. (Note that \file{errcode.h} is not
66 included by \file{Python.h}, so must be included specifically if
67 needed.)
68\end{cfuncdesc}
69
70\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *fp, char *filename}
71 Read and execute statements from a file associated with an
72 interactive device until \EOF{} is reached. If \var{filename} is
73 \NULL, \code{"???"} is used instead. The user will be prompted
74 using \code{sys.ps1} and \code{sys.ps2}. Returns \code{0} at \EOF.
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseString}{char *str,
78 int start}
79 Parse Python source code from \var{str} using the start token
80 \var{start}. The result can be used to create a code object which
81 can be evaluated efficiently. This is useful if a code fragment
82 must be evaluated many times.
83\end{cfuncdesc}
84
85\begin{cfuncdesc}{struct _node*}{PyParser_SimpleParseFile}{FILE *fp,
86 char *filename, int start}
87 Similar to \cfunction{PyParser_SimpleParseString()}, but the Python
88 source code is read from \var{fp} instead of an in-memory string.
89 \var{filename} should be the name of the file.
90\end{cfuncdesc}
91
92\begin{cfuncdesc}{PyObject*}{PyRun_String}{char *str, int start,
93 PyObject *globals,
94 PyObject *locals}
95 Execute Python source code from \var{str} in the context specified
96 by the dictionaries \var{globals} and \var{locals}. The parameter
97 \var{start} specifies the start token that should be used to parse
98 the source code.
99
100 Returns the result of executing the code as a Python object, or
101 \NULL{} if an exception was raised.
102\end{cfuncdesc}
103
104\begin{cfuncdesc}{PyObject*}{PyRun_File}{FILE *fp, char *filename,
105 int start, PyObject *globals,
106 PyObject *locals}
107 Similar to \cfunction{PyRun_String()}, but the Python source code is
108 read from \var{fp} instead of an in-memory string.
109 \var{filename} should be the name of the file.
110\end{cfuncdesc}
111
112\begin{cfuncdesc}{PyObject*}{Py_CompileString}{char *str, char *filename,
113 int start}
114 Parse and compile the Python source code in \var{str}, returning the
115 resulting code object. The start token is given by \var{start};
116 this can be used to constrain the code which can be compiled and should
117 be \constant{Py_eval_input}, \constant{Py_file_input}, or
118 \constant{Py_single_input}. The filename specified by
119 \var{filename} is used to construct the code object and may appear
120 in tracebacks or \exception{SyntaxError} exception messages. This
121 returns \NULL{} if the code cannot be parsed or compiled.
122\end{cfuncdesc}
123
124\begin{cvardesc}{int}{Py_eval_input}
125 The start symbol from the Python grammar for isolated expressions;
126 for use with
127 \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}.
128\end{cvardesc}
129
130\begin{cvardesc}{int}{Py_file_input}
131 The start symbol from the Python grammar for sequences of statements
132 as read from a file or other source; for use with
133 \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}. This is
134 the symbol to use when compiling arbitrarily long Python source code.
135\end{cvardesc}
136
137\begin{cvardesc}{int}{Py_single_input}
138 The start symbol from the Python grammar for a single statement; for
139 use with \cfunction{Py_CompileString()}\ttindex{Py_CompileString()}.
140 This is the symbol used for the interactive interpreter loop.
141\end{cvardesc}