Skip to content

Commit 1887589

Browse files
committed
Add more tests to exception context
1 parent 3ec9b01 commit 1887589

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tests/snippets/try_exceptions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(self):
106106
raise NameError from ex
107107
except NameError as ex2:
108108
assert ex2.__cause__ == cause
109+
assert ex2.__context__ == cause
109110

110111
try:
111112
raise ZeroDivisionError from None
@@ -138,7 +139,25 @@ def __init__(self):
138139
except RuntimeError:
139140
pass
140141

142+
context = None
143+
try:
144+
try:
145+
raise ZeroDivisionError
146+
except ZeroDivisionError as ex:
147+
assert ex.__context__ == None
148+
context = ex
149+
raise NameError
150+
except NameError as ex2:
151+
assert ex2.__context__ == context
152+
assert type(ex2.__context__) == ZeroDivisionError
153+
141154
try:
142155
raise ZeroDivisionError
143156
except ZeroDivisionError as ex:
144157
assert ex.__context__ == None
158+
159+
try:
160+
raise ZeroDivisionError from NameError
161+
except ZeroDivisionError as ex:
162+
assert type(ex.__cause__) == NameError
163+
assert ex.__context__ == None

0 commit comments

Comments
 (0)