Author iritkatriel
Recipients Christopher Stelma, chris.jerdonek, iritkatriel
Date 2021-01-08.15:41:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610120500.48.0.485618385539.issue31213@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-01-08 15:41:40iritkatrielsetrecipients: + iritkatriel, chris.jerdonek, Christopher Stelma
2021-01-08 15:41:40iritkatrielsetmessageid: <1610120500.48.0.485618385539.issue31213@roundup.psfhosted.org>
2021-01-08 15:41:40iritkatriellinkissue31213 messages
2021-01-08 15:41:40iritkatrielcreate