From: Raymond Hettinger Date: Sat, 5 Oct 2002 06:12:08 +0000 (+0000) Subject: Backport 1.54 1.53 1.52 1.51 1.50 and 1.49: X-Git-Tag: v2.2.2b1~65 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aca53fc9383d707d50e79618dff09d3b840e851b;p=thirdparty%2FPython%2Fcpython.git Backport 1.54 1.53 1.52 1.51 1.50 and 1.49: Try to improve the explanation of the "raise" statement and how its arguments are interpreted. This closes SF bug #532467. Fix Typo. Reword explanation of global statement since an undeclared global is a free variable and is subject to those rules. Note the sole case in which the ban on "from ... import *" within a function is enforced. Remove the following restriction: Names bound by import statements may not occur in global statements in the same scope. Why not? Note that it is illegal to delete a cell variable. Note that deleteing an unbound local will raise a NameError. --- diff --git a/Doc/ref/ref6.tex b/Doc/ref/ref6.tex index f15fcfb60e68..80fae5ab8439 100644 --- a/Doc/ref/ref6.tex +++ b/Doc/ref/ref6.tex @@ -352,12 +352,16 @@ hints. Deletion of a target list recursively deletes each target, from left to right. -Deletion of a name removes the binding of that name (which must exist) +Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name -occurs in a \keyword{global} statement in the same code block. +occurs in a \keyword{global} statement in the same code block. If the +name is unbound, a \exception{NameError} exception will be raised. \stindex{global} \indexii{unbinding}{name} +It is illegal to delete a name from the local namespace if it occurs +as a free variable\indexii{free}{variable} in a nested block. + Deletion of attribute references, subscriptions and slicings is passed to the primary object involved; deletion of a slicing is in general equivalent to assignment of an empty slice of the @@ -517,37 +521,44 @@ from __future__ import generators \end{productionlist} If no expressions are present, \keyword{raise} re-raises the last -expression that was raised in the current scope. - -Otherwise, \keyword{raise} evaluates its first expression, which must yield -a string, class, or instance object. If there is a second expression, -this is evaluated, else \code{None} is substituted. If the first -expression is a class object, then the second expression may be an -instance of that class or one of its derivatives, and then that -instance is raised. If the second expression is not such an instance, -the given class is instantiated. The argument list for the -instantiation is determined as follows: if the second expression is a -tuple, it is used as the argument list; if it is \code{None}, the -argument list is empty; otherwise, the argument list consists of a -single argument which is the second expression. If the first -expression is an instance object, the second expression must be -\code{None}. +expression that was active in the current scope. If no exception has +been active in the current scope, an exception is raised that +indicates indicates that this is the error. \index{exception} \indexii{raising}{exception} -If the first object is a string, it then raises the exception -identified by the first object, with the second one (or \code{None}) -as its parameter. If the first object is a class or instance, -it raises the exception identified by the class of the instance -determined in the previous step, with the instance as -its parameter. +Otherwise, \keyword{raise} evaluates the expressions to get three +objects, using \code{None} as the value of omitted expressions. The +first two objects are used to determine the \emph{type} and +\emph{value} of the exception. + +If the first object is an instance, the type of the exception is the +class of the instance, the instance itself if the value, and the +second object must be \code{None}. + +If the first object is a class, it becomes the type of the exception. +The second object is used to determine the exception value: If it is +an instance of the class, the instance becomes the exception value. +If the second object is a tuple, it is used as the argument list for +the class constructor; if it is \code{None}, an empty argument list is +used, and any other object is treated as a single argument to the +constructor. The instance so created by calling the constructor is +used as the exception value. + +If the first object is a string, the string object is the exception +type, and the second object becomes the exception value. + +If a third object is present and not \code{None}, it must be a +traceback\obindex{traceback} object (see section~\ref{traceback}), and +it is substituted instead of the current location as the place where +the exception occurred. If the third object is present and not a +traceback object or \code{None}, a \exception{TypeError} exception is +raised. The three-expression form of \keyword{raise} is useful to +re-raise an exception transparently in an except clause, but +\keyword{raise} with no expressions should be preferred if the +exception to be re-raised was the most recently active exception in +the current scope. -If a third object is present, and it is not \code{None}, it should be -a traceback object (see section~\ref{traceback}), and it is -substituted instead of the current location as the place where the -exception occurred. This is useful to re-raise an exception -transparently in an except clause. -\obindex{traceback} \section{The \keyword{break} statement \label{break}} @@ -690,11 +701,11 @@ defined, the set of public names includes all names found in the module's namespace which do not begin with an underscore character (\character{_}). -Names bound by \keyword{import} statements may not occur in -\keyword{global} statements in the same scope. -\stindex{global} +The \keyword{from} form with \samp{*} may only occur in a module +scope. If the wild card form of import --- \samp{import *} --- is +used in a function and the function contains or is a nested block with +free variables, the compiler will raise a \exception{SyntaxError}. -The \keyword{from} form with \samp{*} may only occur in a module scope. \kwindex{from} \stindex{from} @@ -729,9 +740,9 @@ information. The \keyword{global} statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be -interpreted as globals. While \emph{using} global names is automatic -if they are not defined in the local scope, \emph{assigning} to global -names would be impossible without \keyword{global}. +interpreted as globals. It would be impossible to assign to a global +variable without \keyword{global}, although free variables may refer +to globals without being declared global. \indexiii{global}{name}{binding} Names listed in a \keyword{global} statement must not be used in the same