This commit was manufactured by cvs2svn to create tag 'release101'.
[python/dscho.git] / Doc / ref8.tex
blob1ae61eda422bedcd8e25ef9ec2cf6de056bfb6c9
1 \chapter{Top-level components}
3 The Python interpreter can get its input from a number of sources:
4 from a script passed to it as standard input or as program argument,
5 typed in interactively, from a module source file, etc. This chapter
6 gives the syntax used in these cases.
7 \index{interpreter}
9 \section{Complete Python programs}
10 \index{program}
12 While a language specification need not prescribe how the language
13 interpreter is invoked, it is useful to have a notion of a complete
14 Python program. A complete Python program is executed in a minimally
15 initialized environment: all built-in and standard modules are
16 available, but none have been initialized, except for \verb@sys@
17 (various system services), \verb@__builtin__@ (built-in functions,
18 exceptions and \verb@None@) and \verb@__main__@. The latter is used
19 to provide the local and global name space for execution of the
20 complete program.
21 \bimodindex{sys}
22 \bimodindex{__main__}
23 \bimodindex{__builtin__}
25 The syntax for a complete Python program is that for file input,
26 described in the next section.
28 The interpreter may also be invoked in interactive mode; in this case,
29 it does not read and execute a complete program but reads and executes
30 one statement (possibly compound) at a time. The initial environment
31 is identical to that of a complete program; each statement is executed
32 in the name space of \verb@__main__@.
33 \index{interactive mode}
35 Under {\UNIX}, a complete program can be passed to the interpreter in
36 three forms: with the {\bf -c} {\it string} command line option, as a
37 file passed as the first command line argument, or as standard input.
38 If the file or standard input is a tty device, the interpreter enters
39 interactive mode; otherwise, it executes the file as a complete
40 program.
41 \index{UNIX}
42 \index{command line}
43 \index{standard input}
45 \section{File input}
47 All input read from non-interactive files has the same form:
49 \begin{verbatim}
50 file_input: (NEWLINE | statement)*
51 \end{verbatim}
53 This syntax is used in the following situations:
55 \begin{itemize}
57 \item when parsing a complete Python program (from a file or from a string);
59 \item when parsing a module;
61 \item when parsing a string passed to the \verb@exec@ statement;
63 \end{itemize}
65 \section{Interactive input}
67 Input in interactive mode is parsed using the following grammar:
69 \begin{verbatim}
70 interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE
71 \end{verbatim}
73 Note that a (top-level) compound statement must be followed by a blank
74 line in interactive mode; this is needed to help the parser detect the
75 end of the input.
77 \section{Expression input}
78 \index{input}
80 There are two forms of expression input. Both ignore leading
81 whitespace.
83 The string argument to \verb@eval()@ must have the following form:
84 \bifuncindex{eval}
86 \begin{verbatim}
87 eval_input: condition_list NEWLINE*
88 \end{verbatim}
90 The input line read by \verb@input()@ must have the following form:
91 \bifuncindex{input}
93 \begin{verbatim}
94 input_input: condition_list NEWLINE
95 \end{verbatim}
97 Note: to read `raw' input line without interpretation, you can use the
98 built-in function \verb@raw_input()@ or the \verb@readline()@ method
99 of file objects.
100 \obindex{file}
101 \index{input!raw}
102 \index{raw input}
103 \bifuncindex{raw_index}
104 \ttindex{readline}