Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 1 | \chapter{Execution model \label{execmodel}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 2 | \index{execution model} |
| 3 | |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 4 | \section{Code blocks, execution frames, and namespaces \label{execframes}} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 5 | \index{code block} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 6 | \index{namespace} |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 7 | \indexii{execution}{frame} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 8 | |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 9 | A \dfn{code block}\indexii{code}{block} is a piece |
| 10 | of Python program text that can be executed as a unit, such as a |
| 11 | module, a class definition or a function body. Some code blocks (like |
| 12 | modules) are normally executed only once, others (like function |
| 13 | bodies) may be executed many times. Code blocks may textually contain |
| 14 | other code blocks. Code blocks may invoke other code blocks (that may |
| 15 | or may not be textually contained in them) as part of their execution, |
| 16 | e.g., by invoking (calling) a function. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 18 | The following are code blocks: A module is a code block. A function |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 19 | body is a code block. A class definition is a code block. Each |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 20 | command typed interactively is a separate code block; a script file (a |
| 21 | file given as standard input to the interpreter or specified on the |
| 22 | interpreter command line the first argument) is a code block; a script |
| 23 | command (a command specified on the interpreter command line with the |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 24 | `\strong{-c}' option) is a code block. The file read by the built-in |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 25 | function \function{execfile()} is a code block. The string argument |
| 26 | passed to the built-in function \function{eval()} and to the |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 27 | \keyword{exec} statement is a code block. And finally, the expression |
| 28 | read and evaluated by the built-in function \function{input()} is a |
| 29 | code block. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 30 | |
Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 31 | A code block is executed in an execution frame. An \dfn{execution |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 32 | frame}\indexii{execution}{frame} contains some administrative |
| 33 | information (used for debugging), determines where and how execution |
| 34 | continues after the code block's execution has completed, and (perhaps |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 35 | most importantly) defines two namespaces, the local and the global |
| 36 | namespace, that affect execution of the code block. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 37 | |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 38 | A \dfn{namespace}\index{namespace} is a mapping from names |
| 39 | (identifiers) to objects. A particular namespace may be referenced by |
| 40 | more than one execution frame, and from other places as well. Adding |
| 41 | a name to a namespace is called \dfn{binding}\indexii{binding}{name} a |
| 42 | name (to an object); changing the mapping of a name is called |
| 43 | \dfn{rebinding}\indexii{rebinding}{name}; removing a name is |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 44 | \dfn{unbinding}\indexii{unbinding}{name}. Namespaces are functionally |
| 45 | equivalent to dictionaries (and often implemented as dictionaries). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 46 | |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 47 | The \dfn{local namespace}\indexii{local}{namespace} of an execution |
| 48 | frame determines the default place where names are defined and |
| 49 | searched. The |
| 50 | \dfn{global namespace}\indexii{global}{namespace} determines the place |
| 51 | where names listed in \keyword{global}\stindex{global} statements are |
| 52 | defined and searched, and where names that are not bound anywhere in |
| 53 | the current code block are searched. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 54 | |
| 55 | Whether a name is local or global in a code block is determined by |
| 56 | static inspection of the source text for the code block: in the |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 57 | absence of \keyword{global} statements, a name that is bound anywhere |
| 58 | in the code block is local in the entire code block; all other names |
| 59 | are considered global. The \keyword{global} statement forces global |
| 60 | interpretation of selected names throughout the code block. The |
| 61 | following constructs bind names: formal parameters to functions, |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 62 | \keyword{import} statements, class and function definitions (these |
| 63 | bind the class or function name in the defining block), and targets |
| 64 | that are identifiers if occurring in an assignment, \keyword{for} loop |
| 65 | header, or in the second position of an \keyword{except} clause |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 66 | header. Local names are searched only on the local namespace; global |
| 67 | names are searched only in the global and built-in |
| 68 | namespace.\footnote{ |
| 69 | If the code block contains \keyword{exec} statements or the |
| 70 | construct ``\samp{from \ldots import *}'', the semantics of local |
| 71 | names change: local name lookup first searches the local namespace, |
| 72 | then the global namespace and the built-in namespace.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 73 | |
| 74 | A target occurring in a \keyword{del} statement is also considered bound |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 75 | for this purpose (though the actual semantics are to ``unbind'' the |
| 76 | name). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 78 | When a global name is not found in the global namespace, it is |
| 79 | searched in the built-in namespace (which is actually the global |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 80 | namespace of the module |
| 81 | \module{__builtin__}\refbimodindex{__builtin__}). The built-in |
| 82 | namespace associated with the execution of a code block is actually |
| 83 | found by looking up the name \code{__builtins__} is its global |
| 84 | namespace; this should be a dictionary or a module (in the latter case |
| 85 | its dictionary is used). Normally, the \code{__builtins__} namespace |
| 86 | is the dictionary of the built-in module \module{__builtin__} (note: |
| 87 | no `s'); if it isn't, restricted |
| 88 | execution\indexii{restricted}{execution} mode is in effect. When a |
| 89 | name is not found at all, a |
| 90 | \exception{NameError}\withsubitem{(built-in |
| 91 | exception)}{\ttindex{NameError}} exception is raised. |
| 92 | \stindex{from} |
| 93 | \stindex{exec} |
| 94 | \stindex{global} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 95 | |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 96 | The following table lists the meaning of the local and global |
| 97 | namespace for various types of code blocks. The namespace for a |
| 98 | particular module is automatically created when the module is first |
| 99 | imported (i.e., when it is loaded). Note that in almost all cases, |
| 100 | the global namespace is the namespace of the containing module --- |
| 101 | scopes in Python do not nest! |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 102 | |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 103 | \begin{tableiv}{l|l|l|l}{textrm} |
| 104 | {Code block type}{Global namespace}{Local namespace}{Notes} |
| 105 | \lineiv{Module} |
| 106 | {n.s. for this module} |
| 107 | {same as global}{} |
| 108 | \lineiv{Script (file or command)} |
| 109 | {n.s. for \module{__main__}\refbimodindex{__main__}} |
| 110 | {same as global}{(1)} |
| 111 | \lineiv{Interactive command} |
| 112 | {n.s. for \module{__main__}\refbimodindex{__main__}} |
| 113 | {same as global}{} |
| 114 | \lineiv{Class definition} |
| 115 | {global n.s. of containing block} |
| 116 | {new n.s.}{} |
| 117 | \lineiv{Function body} |
| 118 | {global n.s. of containing block} |
| 119 | {new n.s.}{(2)} |
| 120 | \lineiv{String passed to \keyword{exec} statement} |
| 121 | {global n.s. of containing block} |
| 122 | {local n.s. of containing block}{(2), (3)} |
| 123 | \lineiv{String passed to \function{eval()}} |
| 124 | {global n.s. of caller} |
| 125 | {local n.s. of caller}{(2), (3)} |
| 126 | \lineiv{File read by \function{execfile()}} |
| 127 | {global n.s. of caller} |
| 128 | {local n.s. of caller}{(2), (3)} |
| 129 | \lineiv{Expression read by \function{input()}} |
| 130 | {global n.s. of caller} |
| 131 | {local n.s. of caller}{} |
| 132 | \end{tableiv} |
| 133 | |
| 134 | Notes: |
| 135 | |
| 136 | \begin{description} |
| 137 | |
| 138 | \item[n.s.] means \emph{namespace} |
| 139 | |
| 140 | \item[(1)] The main module for a script is always called |
| 141 | \module{__main__}; ``the filename don't enter into it.'' |
| 142 | |
| 143 | \item[(2)] The global and local namespace for these can be |
| 144 | overridden with optional extra arguments. |
| 145 | |
| 146 | \item[(3)] The \keyword{exec} statement and the \function{eval()} and |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 147 | \function{execfile()} functions have optional arguments to override |
| 148 | the global and local namespace. If only one namespace is specified, |
| 149 | it is used for both. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 150 | |
Jeremy Hylton | e702481 | 2001-03-23 14:05:16 +0000 | [diff] [blame^] | 151 | \end{description} |
| 152 | |
| 153 | The built-in functions \function{globals()} and \function{locals()} returns a |
| 154 | dictionary representing the current global and local namespace, |
| 155 | respectively. The effect of modifications to this dictionary on the |
| 156 | namespace are undefined.\footnote{ |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 157 | The current implementations return the dictionary actually used to |
| 158 | implement the namespace, \emph{except} for functions, where the |
| 159 | optimizer may cause the local namespace to be implemented |
| 160 | differently, and \function{locals()} returns a read-only |
| 161 | dictionary.} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 162 | |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 163 | |
| 164 | \section{Exceptions \label{exceptions}} |
| 165 | \index{exception} |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 166 | |
| 167 | Exceptions are a means of breaking out of the normal flow of control |
| 168 | of a code block in order to handle errors or other exceptional |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 169 | conditions. An exception is |
| 170 | \emph{raised}\index{raise an exception} at the point where the error |
| 171 | is detected; it may be \emph{handled}\index{handle an exception} by |
| 172 | the surrounding code block or by any code block that directly or |
| 173 | indirectly invoked the code block where the error occurred. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 174 | \index{exception handler} |
| 175 | \index{errors} |
| 176 | \index{error handling} |
| 177 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 178 | The Python interpreter raises an exception when it detects a run-time |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 179 | error (such as division by zero). A Python program can also |
| 180 | explicitly raise an exception with the \keyword{raise} statement. |
| 181 | Exception handlers are specified with the \keyword{try} ... \keyword{except} |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 182 | statement. The \keyword{try} ... \keyword{finally} statement |
| 183 | specifies cleanup code which does not handle the exception, but is |
| 184 | executed whether an exception occurred or not in the preceding code. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 185 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 186 | Python uses the ``termination'' \index{termination model}model of |
| 187 | error handling: an exception handler can find out what happened and |
| 188 | continue execution at an outer level, but it cannot repair the cause |
| 189 | of the error and retry the failing operation (except by re-entering |
| 190 | the offending piece of code from the top). |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 191 | |
| 192 | When an exception is not handled at all, the interpreter terminates |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 193 | execution of the program, or returns to its interactive main loop. In |
| 194 | either case, it prints a stack backtrace, except when the exception is |
Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 195 | \exception{SystemExit}\withsubitem{(built-in |
| 196 | exception)}{\ttindex{SystemExit}}. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 198 | Exceptions are identified by string objects or class instances. |
| 199 | Selection of a matching except clause is based on object identity |
| 200 | (i.e., two different string objects with the same value represent |
| 201 | different exceptions!) For string exceptions, the \keyword{except} |
| 202 | clause must reference the same string object. For class exceptions, |
| 203 | the \keyword{except} clause must reference the same class or a base |
| 204 | class of it. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 205 | |
| 206 | When an exception is raised, an object (maybe \code{None}) is passed |
Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 207 | as the exception's ``parameter'' or ``value''; this object does not |
| 208 | affect the selection of an exception handler, but is passed to the |
| 209 | selected exception handler as additional information. For class |
| 210 | exceptions, this object must be an instance of the exception class |
| 211 | being raised. |
Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 212 | |
Fred Drake | e15956b | 2000-04-03 04:51:13 +0000 | [diff] [blame] | 213 | See also the description of the \keyword{try} statement in section |
| 214 | \ref{try} and \keyword{raise} statement in section \ref{raise}. |