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