[New-bugs-announce] [issue31213] __context__ reset to None in nested exception

Christopher Stelma report at bugs.python.org
Tue Aug 15 13:43:59 EDT 2017

New submission from Christopher Stelma:

When I try to re-raise an exception with a __cause__ inside a nested exception, the __context__ of the outer __context__ appears to be reset to None.


>>> try:
...     try:
...         raise Exception('foo')
...     except Exception as foo:
...         print(foo, foo.__context__, foo.__cause__)
...         try:
...             raise Exception('bar') from foo
...         except Exception as bar:
...             print(bar, bar.__context__, bar.__context__.__context__)
...             raise foo from bar
... except Exception as foo:
...     wat = foo
foo None None
bar foo None
>>> print(wat, wat.__context__, wat.__context__.__context__)
foo bar None
>>> print(wat, wat.__cause__, wat.__cause__.__context__)
foo bar None
>>> print(wat, wat.__cause__, wat.__cause__.__cause__)
foo bar foo

here, between "raise foo from bar" and the end, bar.__context__ goes from foo to None.  since bar is only raised once, clearly in the context of foo (twice over), I would expect bar.__context__ to stay foo.

messages: 300305
nosy: Christopher Stelma
priority: normal
severity: normal
status: open
title: __context__ reset to None in nested exception
type: behavior
versions: Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list