Message89310
The current behaviour also doesn't match the spec in PEP 3134, which
states the __context__ attribute will only be set by the VM if it hasn't
already been set.
This is not currently the case, as setting __context__ does not keep the
VM from setting it (I also tried this setting __context__ to 1 - it was
still overridden):
>>> try:
... 1/0
... finally:
... exc = RuntimeError()
... exc.__context__ = None
... raise exc
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: int division or modulo by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
RuntimeError
The "raise ... from ..." syntax is not the correct syntax to use
however, since that relates the __cause__ atribute rather than __context__.
A better approach may be to provide a "sys.clear_exc_info()" method in
the sys module to allow exceptions to be raised from exception handlers
without any __context__ information.
A somewhat clumsy workaround that will work with current Python 3 is to
defer raising the exception until after the exception handler has
finished execution:
>>> exc = None
>>> try:
... 1/0
... except:
... exc = RuntimeError()
...
>>> if exc is not None: raise exc
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError |
|
Date |
User |
Action |
Args |
2009-06-13 01:59:23 | ncoghlan | set | recipients:
+ ncoghlan, poke |
2009-06-13 01:59:23 | ncoghlan | set | messageid: <1244858363.21.0.532421826052.issue6210@psf.upfronthosting.co.za> |
2009-06-13 01:59:21 | ncoghlan | link | issue6210 messages |
2009-06-13 01:59:19 | ncoghlan | create | |
|