Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a573a0

Browse files
Added more tests for issue #27122.
2 parents b84f029 + ce1a9f3 commit 9a573a0

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

Lib/test/test_contextlib.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,9 @@ class Example(object): pass
796796
self.assertIs(stack._exit_callbacks[-1], cm)
797797

798798
def test_dont_reraise_RuntimeError(self):
799-
"""https://bugs.python.org/issue27122"""
799+
# https://bugs.python.org/issue27122
800800
class UniqueException(Exception): pass
801+
class UniqueRuntimeError(RuntimeError): pass
801802

802803
@contextmanager
803804
def second():
@@ -813,15 +814,20 @@ def first():
813814
except Exception as exc:
814815
raise exc
815816

816-
# The RuntimeError should be caught by second()'s exception
817+
# The UniqueRuntimeError should be caught by second()'s exception
817818
# handler which chain raised a new UniqueException.
818819
with self.assertRaises(UniqueException) as err_ctx:
819820
with ExitStack() as es_ctx:
820821
es_ctx.enter_context(second())
821822
es_ctx.enter_context(first())
822-
raise RuntimeError("please no infinite loop.")
823-
824-
self.assertEqual(err_ctx.exception.args[0], "new exception")
823+
raise UniqueRuntimeError("please no infinite loop.")
824+
825+
exc = err_ctx.exception
826+
self.assertIsInstance(exc, UniqueException)
827+
self.assertIsInstance(exc.__context__, UniqueRuntimeError)
828+
self.assertIsNone(exc.__context__.__context__)
829+
self.assertIsNone(exc.__context__.__cause__)
830+
self.assertIs(exc.__cause__, exc.__context__)
825831

826832

827833
class TestRedirectStream:

0 commit comments

Comments
 (0)