More info on the __exit__ return issue, per Guido's request
This commit is contained in:
parent
0d09dd2a6a
commit
47e612f17c
17
pep-0343.txt
17
pep-0343.txt
|
@ -312,9 +312,10 @@ Specification: The 'with' Statement
|
|||
non-local goto should be considered unexceptional for the purposes
|
||||
of a database transaction roll-back decision.
|
||||
|
||||
To facilitate chaining of contexts, __exit__() methods should
|
||||
*not* reraise the error that is passed in, because it is always the
|
||||
responsibility of the *caller* to do any reraising.
|
||||
To facilitate chaining of contexts in Python code that directly
|
||||
manipulates context objects, __exit__() methods should *not*
|
||||
re-raise the error that is passed in to them, because it is always
|
||||
the responsibility of the *caller* to do any reraising in that case.
|
||||
|
||||
That way, if the caller needs to tell whether the __exit__()
|
||||
invocation *failed* (as opposed to successfully cleaning up before
|
||||
|
@ -379,8 +380,16 @@ Generator Decorator
|
|||
except StopIteration:
|
||||
return True
|
||||
except:
|
||||
# only re-raise if it's *not* the exception that was
|
||||
# passed to throw(), because __exit__() must not raise
|
||||
# an exception unless __exit__() itself failed. But
|
||||
# throw() has to raise the exception to signal
|
||||
# propagation, so this fixes the impedance mismatch
|
||||
# between the throw() protocol and the __exit__()
|
||||
# protocol.
|
||||
#
|
||||
if sys.exc_info()[1] is not value:
|
||||
raise # only reraise if it's a new exception
|
||||
raise
|
||||
|
||||
def contextmanager(func):
|
||||
def helper(*args, **kwds):
|
||||
|
|
Loading…
Reference in New Issue