You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2008-06-11.16:04:44.920>created_at=<Date2008-05-12.07:06:53.021>labels= ['interpreter-core', 'type-bug', 'release-blocker']
title='__exit__ silences the active exception'updated_at=<Date2008-06-11.16:04:44.893>user='https://bugs.python.org/ddvoinikov'
I've just discovered that the patch in r62847 doesn't clean up the
exception state if the except clause does not mention a local variable,
e.g. "except MyException" instead of "except MyException as e".
As Amaury said, lexically nested exception handlers make re-raising
behaviour buggy. In Py3k, a workaround is to instead write:
try:
raise Exception("foo")
except Exception as :
try: raise KeyError("caught")
except KeyError: pass
raise e
With the slight inconvenience that the "raise e" line will be appended
to the original traceback.
If we want bare "raise" statements to work as expected after a nested
exception handler, we'll need to add proper exception stacking, for
example by adding the exception value as a member of PyTryBlock. The
effect on performance should also be measured.
In Py3k (with or without r62487), we get "RuntimeError: No active
exception to reraise".
In Python 2.5, we get "TypeError: exceptions must be classes, instances,
or strings (deprecated), not NoneType".
Activity
ddvoinikov commentedon May 12, 2008
If a context manager is used within exception handling block, the active
exception is silenced after the context block completes and __exit__ exits.
try:
raise Exception("foo")
except:
with SomeContextManager():
pass
raise # in Py2.5 throws 'foo', in Py3.0 fails with RuntimeError
amauryfa commentedon May 12, 2008
This problem was introduced by r62847.
amauryfa commentedon May 13, 2008
Note that the problem is not related to "with", but with nested
exception handlers:
try:
raise Exception("foo")
except:
try: pass
except: pass
raise # in Py2.5 throws 'foo', in Py3.0 fails with RuntimeError
OTOH, python has always had poor support for nested exceptions; tried
with python24 and python25::
try:
raise Exception("foo")
except:
try: raise KeyError("caught")
except KeyError: pass
raise # reraise the KeyError...
This does not happen if the two lines with KeyError are moved in another
function.
pitrou commentedon May 13, 2008
I've just discovered that the patch in r62847 doesn't clean up the
exception state if the except clause does not mention a local variable,
e.g. "except MyException" instead of "except MyException as e".
birkenfeld commentedon May 13, 2008
Raising priority.
pitrou commentedon May 14, 2008
As Amaury said, lexically nested exception handlers make re-raising
behaviour buggy. In Py3k, a workaround is to instead write:
try:
raise Exception("foo")
except Exception as :
try: raise KeyError("caught")
except KeyError: pass
raise e
With the slight inconvenience that the "raise e" line will be appended
to the original traceback.
If we want bare "raise" statements to work as expected after a nested
exception handler, we'll need to add proper exception stacking, for
example by adding the exception value as a member of PyTryBlock. The
effect on performance should also be measured.
pitrou commentedon May 14, 2008
Small typo in the snippet above, this should obviously read:
try:
raise Exception("foo")
except Exception as e:
try: raise KeyError("caught")
except KeyError: pass
raise e
pitrou commentedon May 24, 2008
Just found another funny example. This one fails also before r62847.
In Py3k (with or without r62487), we get "RuntimeError: No active
exception to reraise".
In Python 2.5, we get "TypeError: exceptions must be classes, instances,
or strings (deprecated), not NoneType".
pitrou commentedon Jun 1, 2008
A clean solution to both bpo-2507 and bpo-2833 is now proposed in bpo-3021.
benjaminp commentedon Jun 11, 2008
Fixed in r64121.