summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_with.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-05-25 21:10:36 (GMT)
committerGeorg Brandl <georg@python.org>2009-05-25 21:10:36 (GMT)
commit0c31562a913e9a49842bd73c04847861c23774f1 (patch)
treef5fb007402b1a1863c4d317b08be6de4f5547b66 /Lib/test/test_with.py
parent0c1829b919cce6f6823e843a16c52e104f28c7f9 (diff)
downloadcpython-0c31562a913e9a49842bd73c04847861c23774f1.zip
cpython-0c31562a913e9a49842bd73c04847861c23774f1.tar.gz
cpython-0c31562a913e9a49842bd73c04847861c23774f1.tar.bz2
Merged revisions 72924 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r72924 | georg.brandl | 2009-05-25 23:02:56 +0200 (Mo, 25 Mai 2009) | 6 lines Allow multiple context managers in one with statement, as proposed in http://codereview.appspot.com/53094 and accepted by Guido. The construct is transformed into multiple With AST nodes so that there should be no problems with the semantics. ........
Diffstat (limited to 'Lib/test/test_with.py')
-rw-r--r--Lib/test/test_with.py78
1 files changed, 77 insertions, 1 deletions
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py
index b192429..105be8b 100644
--- a/Lib/test/test_with.py
+++ b/Lib/test/test_with.py
@@ -656,12 +656,88 @@ class ExitSwallowsExceptionTestCase(unittest.TestCase):
self.fail("ZeroDivisionError should have been raised")
+class NestedWith(unittest.TestCase):
+
+ class Dummy(object):
+ def __init__(self, value=None, gobble=False):
+ if value is None:
+ value = self
+ self.value = value
+ self.gobble = gobble
+ self.enter_called = False
+ self.exit_called = False
+
+ def __enter__(self):
+ self.enter_called = True
+ return self.value
+
+ def __exit__(self, *exc_info):
+ self.exit_called = True
+ self.exc_info = exc_info
+ if self.gobble:
+ return True
+
+ class CtorRaises(object):
+ def __init__(self): raise RuntimeError()
+
+ class EnterRaises(object):
+ def __enter__(self): raise RuntimeError()
+ def __exit__(self, *exc_info): pass
+
+ class ExitRaises(object):
+ def __enter__(self): pass
+ def __exit__(self, *exc_info): raise RuntimeError()
+
+ def testNoExceptions(self):
+ with self.Dummy() as a, self.Dummy() as b:
+ self.assertTrue(a.enter_called)
+ self.assertTrue(b.enter_called)
+ self.assertTrue(a.exit_called)
+ self.assertTrue(b.exit_called)
+
+ def testExceptionInExprList(self):
+ try:
+ with self.Dummy() as a, self.CtorRaises():
+ pass
+ except:
+ pass
+ self.assertTrue(a.enter_called)
+ self.assertTrue(a.exit_called)
+
+ def testExceptionInEnter(self):
+ try:
+ with self.Dummy() as a, self.EnterRaises():
+ self.fail('body of bad with executed')
+ except RuntimeError:
+ pass
+ else:
+ self.fail('RuntimeError not reraised')
+ self.assertTrue(a.enter_called)
+ self.assertTrue(a.exit_called)
+
+ def testExceptionInExit(self):
+ body_executed = False
+ with self.Dummy(gobble=True) as a, self.ExitRaises():
+ body_executed = True
+ self.assertTrue(a.enter_called)
+ self.assertTrue(a.exit_called)
+ self.assertNotEqual(a.exc_info[0], None)
+
+ def testEnterReturnsTuple(self):
+ with self.Dummy(value=(1,2)) as (a1, a2), \
+ self.Dummy(value=(10, 20)) as (b1, b2):
+ self.assertEquals(1, a1)
+ self.assertEquals(2, a2)
+ self.assertEquals(10, b1)
+ self.assertEquals(20, b2)
+
def test_main():
run_unittest(FailureTestCase, NonexceptionalTestCase,
NestedNonexceptionalTestCase, ExceptionalTestCase,
NonLocalFlowControlTestCase,
AssignmentTargetTestCase,
- ExitSwallowsExceptionTestCase)
+ ExitSwallowsExceptionTestCase,
+ NestedWith)
if __name__ == '__main__':