| 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 | 
|  | 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 | 
|  | 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 |  | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +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 | 
|  | 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 |  | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +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 | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +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 | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 60 | interpretation of selected names throughout the code block.  The | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 61 | following constructs bind names: formal parameters to functions, | 
|  | 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 | 
|  | 66 | header.  Local names are searched only on the local namespace; global | 
| Fred Drake | b55ce1e | 1999-04-05 21:32:52 +0000 | [diff] [blame] | 67 | names are searched only in the global and built-in | 
|  | 68 | namespace.\footnote{ | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 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 | 
|  | 75 | for this purpose (though the actual semantics are to ``unbind'' the | 
|  | 76 | name). | 
|  | 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 | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +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. | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 92 | \stindex{from} | 
|  | 93 | \stindex{exec} | 
|  | 94 | \stindex{global} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 95 |  | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +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 | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 98 | particular module is automatically created when the module is first | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 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 |  | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 103 | \begin{tableiv}{l|l|l|l}{textrm} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 104 | {Code block type}{Global namespace}{Local namespace}{Notes} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 105 | \lineiv{Module} | 
|  | 106 | {n.s. for this module} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 107 | {same as global}{} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 108 | \lineiv{Script (file or command)} | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 109 | {n.s. for \module{__main__}\refbimodindex{__main__}} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 110 | {same as global}{(1)} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 111 | \lineiv{Interactive command} | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 112 | {n.s. for \module{__main__}\refbimodindex{__main__}} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 113 | {same as global}{} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 114 | \lineiv{Class definition} | 
|  | 115 | {global n.s. of containing block} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 116 | {new n.s.}{} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 117 | \lineiv{Function body} | 
|  | 118 | {global n.s. of containing block} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 119 | {new n.s.}{(2)} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 120 | \lineiv{String passed to \keyword{exec} statement} | 
|  | 121 | {global n.s. of containing block} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 122 | {local n.s. of containing block}{(2), (3)} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 123 | \lineiv{String passed to \function{eval()}} | 
|  | 124 | {global n.s. of caller} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 125 | {local n.s. of caller}{(2), (3)} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 126 | \lineiv{File read by \function{execfile()}} | 
|  | 127 | {global n.s. of caller} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 128 | {local n.s. of caller}{(2), (3)} | 
| Fred Drake | dad1132 | 1998-10-21 00:26:56 +0000 | [diff] [blame] | 129 | \lineiv{Expression read by \function{input()}} | 
|  | 130 | {global n.s. of caller} | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 131 | {local n.s. of caller}{} | 
|  | 132 | \end{tableiv} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 133 |  | 
|  | 134 | Notes: | 
|  | 135 |  | 
|  | 136 | \begin{description} | 
|  | 137 |  | 
| Fred Drake | a1cce71 | 1998-07-24 22:12:32 +0000 | [diff] [blame] | 138 | \item[n.s.] means \emph{namespace} | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 139 |  | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 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 | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 144 | overridden with optional extra arguments. | 
|  | 145 |  | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 146 | \item[(3)] The \keyword{exec} statement and the \function{eval()} and | 
|  | 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 |  | 
|  | 151 | \end{description} | 
|  | 152 |  | 
|  | 153 | The built-in functions \function{globals()} and \function{locals()} returns a | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 154 | dictionary representing the current global and local namespace, | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 155 | respectively.  The effect of modifications to this dictionary on the | 
| Fred Drake | b55ce1e | 1999-04-05 21:32:52 +0000 | [diff] [blame] | 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 |  | 
|  | 186 | Python uses the ``termination'' model of error handling: an exception | 
|  | 187 | handler can find out what happened and continue execution at an outer | 
|  | 188 | level, but it cannot repair the cause of the error and retry the | 
| Guido van Rossum | b18a93b | 1998-07-23 19:36:00 +0000 | [diff] [blame] | 189 | failing operation (except by re-entering the offending piece of | 
| Fred Drake | f666917 | 1998-05-06 19:52:49 +0000 | [diff] [blame] | 190 | code from the top). | 
|  | 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 |  | 
|  | 213 | See also the description of the \keyword{try} and \keyword{raise} | 
| Fred Drake | 431f0ce | 1999-05-13 18:38:11 +0000 | [diff] [blame] | 214 | statements in chapter \ref{compound}. |