summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_contextlib.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-06-01 12:48:32 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-06-01 12:48:32 (GMT)
commit77452fc12121a333397ea262a3d29bdb8cbc7a57 (patch)
tree4327efa4d00f3b8a80ccc3b31f466626f4e1a322 /Lib/test/test_contextlib.py
parentc4b78a3e15dcc627a06ba3a0fc92358aade2b7ea (diff)
downloadcpython-77452fc12121a333397ea262a3d29bdb8cbc7a57.zip
cpython-77452fc12121a333397ea262a3d29bdb8cbc7a57.tar.gz
cpython-77452fc12121a333397ea262a3d29bdb8cbc7a57.tar.bz2
Close #14969: Improve the handling of exception chaining in contextlib.ExitStack
Diffstat (limited to 'Lib/test/test_contextlib.py')
-rw-r--r--Lib/test/test_contextlib.py29
1 files changed, 18 insertions, 11 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index efa9dcb..e52ed91 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -505,6 +505,18 @@ class TestExitStack(unittest.TestCase):
def __exit__(self, *exc_details):
raise self.exc
+ class RaiseExcWithContext:
+ def __init__(self, outer, inner):
+ self.outer = outer
+ self.inner = inner
+ def __enter__(self):
+ return self
+ def __exit__(self, *exc_details):
+ try:
+ raise self.inner
+ except:
+ raise self.outer
+
class SuppressExc:
def __enter__(self):
return self
@@ -514,11 +526,10 @@ class TestExitStack(unittest.TestCase):
try:
with RaiseExc(IndexError):
- with RaiseExc(KeyError):
- with RaiseExc(AttributeError):
- with SuppressExc():
- with RaiseExc(ValueError):
- 1 / 0
+ with RaiseExcWithContext(KeyError, AttributeError):
+ with SuppressExc():
+ with RaiseExc(ValueError):
+ 1 / 0
except IndexError as exc:
self.assertIsInstance(exc.__context__, KeyError)
self.assertIsInstance(exc.__context__.__context__, AttributeError)
@@ -553,12 +564,8 @@ class TestExitStack(unittest.TestCase):
except IndexError as exc:
self.assertIsInstance(exc.__context__, KeyError)
self.assertIsInstance(exc.__context__.__context__, AttributeError)
- # Inner exceptions were suppressed, but the with statement
- # cleanup code adds the one from the body back in as the
- # context of the exception raised by the outer callbacks
- # See http://bugs.python.org/issue14969
- suite_exc = exc.__context__.__context__.__context__
- self.assertIsInstance(suite_exc, ZeroDivisionError)
+ # Inner exceptions were suppressed
+ self.assertIsNone(exc.__context__.__context__.__context__)
else:
self.fail("Expected IndexError, but no exception was raised")
# Check the inner exceptions