Saved and restored logging._handlerList at the same time as saving/restoring logging...
[python.git] / Doc / ref / ref8.tex
blobd10c87f5bea880f3ab5fc2cb37c7119a1abc8323
1 \chapter{Top-level components\label{top-level}}
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}
10 \section{Complete Python programs\label{programs}}
11 \index{program}
13 While a language specification need not prescribe how the language
14 interpreter is invoked, it is useful to have a notion of a complete
15 Python program. A complete Python program is executed in a minimally
16 initialized environment: all built-in and standard modules are
17 available, but none have been initialized, except for \module{sys}
18 (various system services), \module{__builtin__} (built-in functions,
19 exceptions and \code{None}) and \module{__main__}. The latter is used
20 to provide the local and global namespace for execution of the
21 complete program.
22 \refbimodindex{sys}
23 \refbimodindex{__main__}
24 \refbimodindex{__builtin__}
26 The syntax for a complete Python program is that for file input,
27 described in the next section.
29 The interpreter may also be invoked in interactive mode; in this case,
30 it does not read and execute a complete program but reads and executes
31 one statement (possibly compound) at a time. The initial environment
32 is identical to that of a complete program; each statement is executed
33 in the namespace of \module{__main__}.
34 \index{interactive mode}
35 \refbimodindex{__main__}
37 Under {\UNIX}, a complete program can be passed to the interpreter in
38 three forms: with the \programopt{-c} \var{string} command line option, as a
39 file passed as the first command line argument, or as standard input.
40 If the file or standard input is a tty device, the interpreter enters
41 interactive mode; otherwise, it executes the file as a complete
42 program.
43 \index{UNIX}
44 \index{command line}
45 \index{standard input}
48 \section{File input\label{file-input}}
50 All input read from non-interactive files has the same form:
52 \begin{productionlist}
53 \production{file_input}
54 {(NEWLINE | \token{statement})*}
55 \end{productionlist}
57 This syntax is used in the following situations:
59 \begin{itemize}
61 \item when parsing a complete Python program (from a file or from a string);
63 \item when parsing a module;
65 \item when parsing a string passed to the \keyword{exec} statement;
67 \end{itemize}
70 \section{Interactive input\label{interactive}}
72 Input in interactive mode is parsed using the following grammar:
74 \begin{productionlist}
75 \production{interactive_input}
76 {[\token{stmt_list}] NEWLINE | \token{compound_stmt} NEWLINE}
77 \end{productionlist}
79 Note that a (top-level) compound statement must be followed by a blank
80 line in interactive mode; this is needed to help the parser detect the
81 end of the input.
84 \section{Expression input\label{expression-input}}
85 \index{input}
87 There are two forms of expression input. Both ignore leading
88 whitespace.
89 The string argument to \function{eval()} must have the following form:
90 \bifuncindex{eval}
92 \begin{productionlist}
93 \production{eval_input}
94 {\token{expression_list} NEWLINE*}
95 \end{productionlist}
97 The input line read by \function{input()} must have the following form:
98 \bifuncindex{input}
100 \begin{productionlist}
101 \production{input_input}
102 {\token{expression_list} NEWLINE}
103 \end{productionlist}
105 Note: to read `raw' input line without interpretation, you can use the
106 built-in function \function{raw_input()} or the \method{readline()} method
107 of file objects.
108 \obindex{file}
109 \index{input!raw}
110 \index{raw input}
111 \bifuncindex{raw_input}
112 \withsubitem{(file method)}{\ttindex{readline()}}