cinap_lenrekadd hg and python4bb9e2215 years agoHistory
112 lines · 3.6 KB · TeX
RawDownload
\chapter{Top-level components\label{top-level}} The Python interpreter can get its input from a number of sources:from a script passed to it as standard input or as program argument,typed in interactively, from a module source file, etc.  This chaptergives the syntax used in these cases.\index{interpreter}  \section{Complete Python programs\label{programs}}\index{program} While a language specification need not prescribe how the languageinterpreter is invoked, it is useful to have a notion of a completePython program.  A complete Python program is executed in a minimallyinitialized environment: all built-in and standard modules areavailable, but none have been initialized, except for \module{sys}(various system services), \module{__builtin__} (built-in functions,exceptions and \code{None}) and \module{__main__}.  The latter is usedto provide the local and global namespace for execution of thecomplete program.\refbimodindex{sys}\refbimodindex{__main__}\refbimodindex{__builtin__} The syntax for a complete Python program is that for file input,described in the next section. The interpreter may also be invoked in interactive mode; in this case,it does not read and execute a complete program but reads and executesone statement (possibly compound) at a time.  The initial environmentis identical to that of a complete program; each statement is executedin the namespace of \module{__main__}.\index{interactive mode}\refbimodindex{__main__} Under \UNIX, a complete program can be passed to the interpreter inthree forms: with the \programopt{-c} \var{string} command line option, as afile passed as the first command line argument, or as standard input.If the file or standard input is a tty device, the interpreter entersinteractive mode; otherwise, it executes the file as a completeprogram.\index{UNIX}\index{command line}\index{standard input}  \section{File input\label{file-input}} All input read from non-interactive files has the same form: \begin{productionlist}  \production{file_input}             {(NEWLINE | \token{statement})*}\end{productionlist} This syntax is used in the following situations: \begin{itemize} \item when parsing a complete Python program (from a file or from a string); \item when parsing a module; \item when parsing a string passed to the \keyword{exec} statement; \end{itemize}  \section{Interactive input\label{interactive}} Input in interactive mode is parsed using the following grammar: \begin{productionlist}  \production{interactive_input}             {[\token{stmt_list}] NEWLINE | \token{compound_stmt} NEWLINE}\end{productionlist} Note that a (top-level) compound statement must be followed by a blankline in interactive mode; this is needed to help the parser detect theend of the input.  \section{Expression input\label{expression-input}}\index{input} There are two forms of expression input.  Both ignore leadingwhitespace.The string argument to \function{eval()} must have the following form:\bifuncindex{eval} \begin{productionlist}  \production{eval_input}             {\token{expression_list} NEWLINE*}\end{productionlist} The input line read by \function{input()} must have the following form:\bifuncindex{input} \begin{productionlist}  \production{input_input}             {\token{expression_list} NEWLINE}\end{productionlist} Note: to read `raw' input line without interpretation, you can use thebuilt-in function \function{raw_input()} or the \method{readline()} methodof file objects.\obindex{file}\index{input!raw}\index{raw input}\bifuncindex{raw_input}\withsubitem{(file method)}{\ttindex{readline()}}