Irit Katriel <iritkatr...@yahoo.com> added the comment:

This seems to be deliberately done here in order to prevent context cycles from 
forming: 
https://github.com/python/cpython/blob/fe6e5e7cfd68eeaa69fd1511f354a1b4d8d90990/Python/errors.c#L148

In your code you are creating a cycle where foo is the context (and cause) for 
bar which is the context (and cause) of the second time foo is raised. 

If you create a new instance of foo for the second raise then there is no cycle 
and you get what you expect:

try:
    try:
        raise Exception('foo')
    except Exception as foo:
        print("1--", foo, foo.__context__, foo.__cause__)
        try:
            raise Exception('bar') from foo
        except Exception as bar:
            print("2--", bar, bar.__context__, bar.__context__.__context__)
            raise Exception('foo2') from bar
except Exception as foo:
    wat = foo

print("3--", wat, wat.__context__, wat.__context__.__context__)
print("4--", wat, wat.__cause__, wat.__cause__.__context__)
print("5--", wat, wat.__cause__, wat.__cause__.__cause__)


Output is:

1-- foo None None
2-- bar foo None
3-- foo2 bar foo
4-- foo2 bar foo
5-- foo2 bar foo


I think the bug is in your code - you can't create an exception chain that 
contains the same exception instance more than once.

----------
nosy: +iritkatriel

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue31213>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to