summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2014-01-24 13:07:16 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2014-01-24 13:07:16 (GMT)
commitb3c0f4067d992fc2c0d8578e308cc7167dc98f32 (patch)
tree78f3a89912da885be6780abb8512a0c43b3a5683 /Lib/test/test_contextlib.py
parent8f81c3cf3f76ec7991f3c367926faf07357a465e (diff)
parentadd94c9d825dc2d870f6557e1d6510a82d3dba12 (diff)
downloadcpython-b3c0f4067d992fc2c0d8578e308cc7167dc98f32.zip
cpython-b3c0f4067d992fc2c0d8578e308cc7167dc98f32.tar.gz
cpython-b3c0f4067d992fc2c0d8578e308cc7167dc98f32.tar.bz2
Merge removal of issue 20317 debugging code from 3.3
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r--Lib/test/test_contextlib.py58
1 files changed, 48 insertions, 10 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index f947232..39cc776 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -629,24 +629,62 @@ class TestExitStack(unittest.TestCase):
def test_exit_exception_with_correct_context(self):
# http://bugs.python.org/issue20317
@contextmanager
- def gets_the_context_right():
+ def gets_the_context_right(exc):
try:
- yield 6
+ yield
finally:
- 1 / 0
+ raise exc
+
+ exc1 = Exception(1)
+ exc2 = Exception(2)
+ exc3 = Exception(3)
+ exc4 = Exception(4)
# The contextmanager already fixes the context, so prior to the
# fix, ExitStack would try to fix it *again* and get into an
# infinite self-referential loop
try:
with ExitStack() as stack:
- stack.enter_context(gets_the_context_right())
- stack.enter_context(gets_the_context_right())
- stack.enter_context(gets_the_context_right())
- except ZeroDivisionError as exc:
- self.assertIsInstance(exc.__context__, ZeroDivisionError)
- self.assertIsInstance(exc.__context__.__context__, ZeroDivisionError)
- self.assertIsNone(exc.__context__.__context__.__context__)
+ stack.enter_context(gets_the_context_right(exc4))
+ stack.enter_context(gets_the_context_right(exc3))
+ stack.enter_context(gets_the_context_right(exc2))
+ raise exc1
+ except Exception as exc:
+ self.assertIs(exc, exc4)
+ self.assertIs(exc.__context__, exc3)
+ self.assertIs(exc.__context__.__context__, exc2)
+ self.assertIs(exc.__context__.__context__.__context__, exc1)
+ self.assertIsNone(
+ exc.__context__.__context__.__context__.__context__)
+
+ def test_exit_exception_with_existing_context(self):
+ # Addresses a lack of test coverage discovered after checking in a
+ # fix for issue 20317 that still contained debugging code.
+ def raise_nested(inner_exc, outer_exc):
+ try:
+ raise inner_exc
+ finally:
+ raise outer_exc
+ exc1 = Exception(1)
+ exc2 = Exception(2)
+ exc3 = Exception(3)
+ exc4 = Exception(4)
+ exc5 = Exception(5)
+ try:
+ with ExitStack() as stack:
+ stack.callback(raise_nested, exc4, exc5)
+ stack.callback(raise_nested, exc2, exc3)
+ raise exc1
+ except Exception as exc:
+ self.assertIs(exc, exc5)
+ self.assertIs(exc.__context__, exc4)
+ self.assertIs(exc.__context__.__context__, exc3)
+ self.assertIs(exc.__context__.__context__.__context__, exc2)
+ self.assertIs(
+ exc.__context__.__context__.__context__.__context__, exc1)
+ self.assertIsNone(
+ exc.__context__.__context__.__context__.__context__.__context__)
+
def test_body_exception_suppress(self):