10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#!/usr/bin/env python
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Unit tests for the with statement specified in PEP 343."""
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__author__ = "Mike Bland"
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao__email__ = "mbland at acm dot org"
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom collections import deque
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom contextlib import GeneratorContextManager, contextmanager
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test.test_support import run_unittest
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass MockContextManager(GeneratorContextManager):
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, gen):
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        GeneratorContextManager.__init__(self, gen)
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.enter_called = False
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_called = False
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_args = None
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __enter__(self):
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.enter_called = True
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return GeneratorContextManager.__enter__(self)
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __exit__(self, type, value, traceback):
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_called = True
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_args = (type, value, traceback)
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return GeneratorContextManager.__exit__(self, type,
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                value, traceback)
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef mock_contextmanager(func):
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def helper(*args, **kwds):
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return MockContextManager(func(*args, **kwds))
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return helper
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass MockResource(object):
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self):
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.yielded = False
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.stopped = False
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao@mock_contextmanager
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef mock_contextmanager_generator():
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    mock = MockResource()
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    try:
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock.yielded = True
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        yield mock
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    finally:
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock.stopped = True
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Nested(object):
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, *managers):
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.managers = managers
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.entered = None
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __enter__(self):
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if self.entered is not None:
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise RuntimeError("Context is not reentrant")
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.entered = deque()
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        vars = []
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for mgr in self.managers:
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                vars.append(mgr.__enter__())
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.entered.appendleft(mgr)
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if not self.__exit__(*sys.exc_info()):
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return vars
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __exit__(self, *exc_info):
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Behave like nested with statements
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # first in, last out
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # New exceptions override old ones
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ex = exc_info
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for mgr in self.entered:
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if mgr.__exit__(*ex):
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    ex = (None, None, None)
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                ex = sys.exc_info()
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.entered = None
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if ex is not exc_info:
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise ex[0], ex[1], ex[2]
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass MockNested(Nested):
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, *managers):
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        Nested.__init__(self, *managers)
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.enter_called = False
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_called = False
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_args = None
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __enter__(self):
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.enter_called = True
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Nested.__enter__(self)
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __exit__(self, *exc_info):
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_called = True
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.exit_args = exc_info
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return Nested.__exit__(self, *exc_info)
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass FailureTestCase(unittest.TestCase):
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testNameError(self):
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def fooNotDeclared():
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with foo: pass
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(NameError, fooNotDeclared)
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testEnterAttributeError(self):
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class LacksEnter(object):
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, type, value, traceback):
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def fooLacksEnter():
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            foo = LacksEnter()
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with foo: pass
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(AttributeError, fooLacksEnter)
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExitAttributeError(self):
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class LacksExit(object):
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def fooLacksExit():
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            foo = LacksExit()
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with foo: pass
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(AttributeError, fooLacksExit)
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertRaisesSyntaxError(self, codestr):
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldRaiseSyntaxError(s):
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            compile(s, '', 'single')
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testAssignmentToNoneError(self):
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaisesSyntaxError('with mock as None:\n  pass')
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaisesSyntaxError(
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'with mock as (None):\n'
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            '  pass')
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testAssignmentToEmptyTupleError(self):
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaisesSyntaxError(
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'with mock as ():\n'
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            '  pass')
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testAssignmentToTupleOnlyContainingNoneError(self):
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaisesSyntaxError('with mock as None,:\n  pass')
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaisesSyntaxError(
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'with mock as (None,):\n'
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            '  pass')
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testAssignmentToTupleContainingNoneError(self):
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaisesSyntaxError(
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            'with mock as (foo, None, bar):\n'
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            '  pass')
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testEnterThrows(self):
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class EnterThrows(object):
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise RuntimeError("Enter threw")
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, *args):
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            ct = EnterThrows()
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.foo = None
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with ct as self.foo:
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(self.foo, None)
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExitThrows(self):
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class ExitThrows(object):
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, *args):
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise RuntimeError(42)
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with ExitThrows():
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ContextmanagerAssertionMixin(object):
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    TEST_EXCEPTION = RuntimeError("test exception")
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertInWithManagerInvariants(self, mock_manager):
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_manager.enter_called)
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(mock_manager.exit_called)
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mock_manager.exit_args, None)
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertAfterWithManagerInvariants(self, mock_manager, exit_args):
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_manager.enter_called)
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_manager.exit_called)
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mock_manager.exit_args, exit_args)
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertAfterWithManagerInvariantsNoError(self, mock_manager):
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariants(mock_manager,
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            (None, None, None))
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertInWithGeneratorInvariants(self, mock_generator):
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_generator.yielded)
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(mock_generator.stopped)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertAfterWithGeneratorInvariantsNoError(self, mock_generator):
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_generator.yielded)
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_generator.stopped)
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def raiseTestException(self):
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        raise self.TEST_EXCEPTION
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertAfterWithManagerInvariantsWithError(self, mock_manager,
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                                  exc_type=None):
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_manager.enter_called)
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_manager.exit_called)
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if exc_type is None:
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            exc_type = type(self.TEST_EXCEPTION)
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mock_manager.exit_args[0], exc_type)
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Test the __exit__ arguments. Issue #7853
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(mock_manager.exit_args[1], exc_type)
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsNot(mock_manager.exit_args[2], None)
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_generator.yielded)
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(mock_generator.stopped)
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testInlineGeneratorSyntax(self):
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator():
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testUnboundGenerator(self):
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock = mock_contextmanager_generator()
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock:
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock)
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testInlineGeneratorBoundSyntax(self):
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as foo:
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(foo)
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # FIXME: In the future, we'll try to keep the bound names from leaking
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(foo)
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testInlineGeneratorBoundToExistingVariable(self):
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        foo = None
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as foo:
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(foo)
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(foo)
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testInlineGeneratorBoundToDottedVariable(self):
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as self.foo:
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(self.foo)
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(self.foo)
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testBoundGenerator(self):
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock = mock_contextmanager_generator()
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock as foo:
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(foo)
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(mock)
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(foo)
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock)
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testNestedSingleStatements(self):
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_a = mock_contextmanager_generator()
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_a as foo:
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mock_b = mock_contextmanager_generator()
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_b as bar:
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(mock_a)
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(mock_b)
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithGeneratorInvariants(foo)
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithGeneratorInvariants(bar)
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertAfterWithManagerInvariantsNoError(mock_b)
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertAfterWithGeneratorInvariantsNoError(bar)
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(mock_a)
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(foo)
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock_a)
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(foo)
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass NestedNonexceptionalTestCase(unittest.TestCase,
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ContextmanagerAssertionMixin):
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleArgInlineGeneratorSyntax(self):
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with Nested(mock_contextmanager_generator()):
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleArgBoundToNonTuple(self):
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = mock_contextmanager_generator()
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This will bind all the arguments to nested() into a single list
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # assigned to foo.
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with Nested(m) as foo:
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(m)
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(m)
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleArgBoundToSingleElementParenthesizedList(self):
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = mock_contextmanager_generator()
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # This will bind all the arguments to nested() into a single list
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # assigned to foo.
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with Nested(m) as (foo):
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(m)
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(m)
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleArgBoundToMultipleElementTupleError(self):
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrowValueError():
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with Nested(mock_contextmanager_generator()) as (foo, bar):
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, shouldThrowValueError)
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleArgUnbound(self):
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_contextmanager = mock_contextmanager_generator()
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_nested = MockNested(mock_contextmanager)
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_nested:
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(mock_contextmanager)
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(mock_nested)
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock_nested)
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testMultipleArgUnbound(self):
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        m = mock_contextmanager_generator()
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        n = mock_contextmanager_generator()
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        o = mock_contextmanager_generator()
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_nested = MockNested(m, n, o)
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_nested:
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(m)
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(n)
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(o)
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(mock_nested)
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(m)
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(n)
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(o)
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock_nested)
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testMultipleArgBound(self):
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_nested = MockNested(mock_contextmanager_generator(),
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mock_contextmanager_generator(), mock_contextmanager_generator())
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_nested as (m, n, o):
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(m)
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(n)
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithGeneratorInvariants(o)
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertInWithManagerInvariants(mock_nested)
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(m)
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(n)
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(o)
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock_nested)
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleResource(self):
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cm = mock_contextmanager_generator()
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm as self.resource:
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(cm)
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithGeneratorInvariants(self.resource)
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.raiseTestException()
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(cm)
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.resource)
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExceptionNormalized(self):
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cm = mock_contextmanager_generator()
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm as self.resource:
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Note this relies on the fact that 1 // 0 produces an exception
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # that is not normalized immediately.
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ZeroDivisionError, shouldThrow)
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(cm, ZeroDivisionError)
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testNestedSingleStatements(self):
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_a = mock_contextmanager_generator()
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_b = mock_contextmanager_generator()
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_a as self.foo:
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with mock_b as self.bar:
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithManagerInvariants(mock_a)
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithManagerInvariants(mock_b)
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithGeneratorInvariants(self.foo)
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithGeneratorInvariants(self.bar)
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.raiseTestException()
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(mock_a)
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(mock_b)
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.foo)
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.bar)
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testMultipleResourcesInSingleStatement(self):
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cm_a = mock_contextmanager_generator()
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cm_b = mock_contextmanager_generator()
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_nested = MockNested(cm_a, cm_b)
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_nested as (self.resource_a, self.resource_b):
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(cm_a)
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(cm_b)
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(mock_nested)
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithGeneratorInvariants(self.resource_a)
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithGeneratorInvariants(self.resource_b)
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.raiseTestException()
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(cm_a)
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(cm_b)
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(mock_nested)
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testNestedExceptionBeforeInnerStatement(self):
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_a = mock_contextmanager_generator()
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_b = mock_contextmanager_generator()
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.bar = None
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_a as self.foo:
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithManagerInvariants(mock_a)
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertInWithGeneratorInvariants(self.foo)
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.raiseTestException()
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with mock_b as self.bar:
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    pass
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(mock_a)
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.foo)
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # The inner statement stuff should never have been touched
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(self.bar, None)
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(mock_b.enter_called)
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(mock_b.exit_called)
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(mock_b.exit_args, None)
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testNestedExceptionAfterInnerStatement(self):
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_a = mock_contextmanager_generator()
4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mock_b = mock_contextmanager_generator()
4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_a as self.foo:
4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with mock_b as self.bar:
4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithManagerInvariants(mock_a)
4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithManagerInvariants(mock_b)
4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithGeneratorInvariants(self.foo)
4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertInWithGeneratorInvariants(self.bar)
4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.raiseTestException()
4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(RuntimeError, shouldThrow)
4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsWithError(mock_a)
4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithManagerInvariantsNoError(mock_b)
4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsWithError(self.foo)
4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertAfterWithGeneratorInvariantsNoError(self.bar)
4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testRaisedStopIteration1(self):
4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # From bug 1462485
4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def cm():
4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm():
4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise StopIteration("from with")
4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(StopIteration, shouldThrow)
4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testRaisedStopIteration2(self):
4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # From bug 1462485
4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class cm(object):
4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, type, value, traceback):
4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm():
4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise StopIteration("from with")
4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(StopIteration, shouldThrow)
4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testRaisedStopIteration3(self):
4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Another variant where the exception hasn't been instantiated
4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # From bug 1705170
4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def cm():
4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm():
4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise iter([]).next()
4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(StopIteration, shouldThrow)
4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testRaisedGeneratorExit1(self):
4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # From bug 1462485
4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def cm():
4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm():
4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise GeneratorExit("from with")
4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(GeneratorExit, shouldThrow)
4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testRaisedGeneratorExit2(self):
5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # From bug 1462485
5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class cm (object):
5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, type, value, traceback):
5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def shouldThrow():
5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm():
5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise GeneratorExit("from with")
5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(GeneratorExit, shouldThrow)
5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testErrorsInBool(self):
5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # issue4589: __exit__ return code may raise an exception
5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # when looking at its truth value.
5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class cm(object):
5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __init__(self, bool_conversion):
5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                class Bool:
5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    def __nonzero__(self):
5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        return bool_conversion()
5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.exit_result = Bool()
5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 3
5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, a, b, c):
5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return self.exit_result
5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def trueAsBool():
5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm(lambda: True):
5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail("Should NOT see this")
5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        trueAsBool()
5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def falseAsBool():
5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm(lambda: False):
5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail("Should raise")
5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(AssertionError, falseAsBool)
5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def failAsBool():
5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with cm(lambda: 1 // 0):
5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail("Should NOT see this")
5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ZeroDivisionError, failAsBool)
5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass NonLocalFlowControlTestCase(unittest.TestCase):
5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithBreak(self):
5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        counter = 0
5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while True:
5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter += 1
5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_contextmanager_generator():
5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                counter += 10
5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                break
5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter += 100 # Not reached
5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(counter, 11)
5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithContinue(self):
5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        counter = 0
5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while True:
5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter += 1
5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if counter > 2:
5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                break
5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_contextmanager_generator():
5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                counter += 10
5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                continue
5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter += 100 # Not reached
5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(counter, 12)
5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithReturn(self):
5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def foo():
5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter = 0
5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            while True:
5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                counter += 1
5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with mock_contextmanager_generator():
5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    counter += 10
5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return counter
5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                counter += 100 # Not reached
5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(foo(), 11)
5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithYield(self):
5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def gen():
5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_contextmanager_generator():
5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield 12
5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield 13
5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = list(gen())
5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(x, [12, 13])
5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithRaise(self):
5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        counter = 0
5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter += 1
5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with mock_contextmanager_generator():
5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                counter += 10
5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise RuntimeError
5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            counter += 100 # Not reached
5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except RuntimeError:
5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(counter, 11)
5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Didn't raise RuntimeError")
5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass AssignmentTargetTestCase(unittest.TestCase):
6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testSingleComplexTarget(self):
6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        targets = {1: [0, 1, 2]}
6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as targets[1][0]:
6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets.keys(), [1])
6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets[1][0].__class__, MockResource)
6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as targets.values()[0][1]:
6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets.keys(), [1])
6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets[1][1].__class__, MockResource)
6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as targets[2]:
6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            keys = targets.keys()
6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            keys.sort()
6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(keys, [1, 2])
6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class C: pass
6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        blah = C()
6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with mock_contextmanager_generator() as blah.foo:
6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(hasattr(blah, "foo"), True)
6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testMultipleComplexTargets(self):
6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class C:
6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self): return 1, 2, 3
6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, t, v, tb): pass
6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        targets = {1: [0, 1, 2]}
6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with C() as (targets[1][0], targets[1][1], targets[1][2]):
6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets, {1: [1, 2, 3]})
6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]):
6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets, {1: [3, 2, 1]})
6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with C() as (targets[1], targets[2], targets[3]):
6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(targets, {1: 1, 2: 2, 3: 3})
6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class B: pass
6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        blah = B()
6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with C() as (blah.one, blah.two, blah.three):
6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(blah.one, 1)
6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(blah.two, 2)
6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(blah.three, 3)
6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ExitSwallowsExceptionTestCase(unittest.TestCase):
6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExitTrueSwallowsException(self):
6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class AfricanSwallow:
6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self): pass
6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, t, v, tb): return True
6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with AfricanSwallow():
6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except ZeroDivisionError:
6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("ZeroDivisionError should have been swallowed")
6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExitFalseDoesntSwallowException(self):
6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class EuropeanSwallow:
6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self): pass
6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, t, v, tb): return False
6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with EuropeanSwallow():
6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except ZeroDivisionError:
6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("ZeroDivisionError should have been raised")
6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass NestedWith(unittest.TestCase):
6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    class Dummy(object):
6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __init__(self, value=None, gobble=False):
6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if value is None:
6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                value = self
6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.value = value
6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.gobble = gobble
6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.enter_called = False
6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.exit_called = False
6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __enter__(self):
6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.enter_called = True
6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return self.value
6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __exit__(self, *exc_info):
6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.exit_called = True
6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.exc_info = exc_info
6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if self.gobble:
6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return True
6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    class InitRaises(object):
6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __init__(self): raise RuntimeError()
6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    class EnterRaises(object):
6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __enter__(self): raise RuntimeError()
6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __exit__(self, *exc_info): pass
6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    class ExitRaises(object):
6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __enter__(self): pass
6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def __exit__(self, *exc_info): raise RuntimeError()
6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testNoExceptions(self):
6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.Dummy() as a, self.Dummy() as b:
6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(a.enter_called)
6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(b.enter_called)
7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.exit_called)
7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(b.exit_called)
7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExceptionInExprList(self):
7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.Dummy() as a, self.InitRaises():
7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except:
7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.enter_called)
7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.exit_called)
7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExceptionInEnter(self):
7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.Dummy() as a, self.EnterRaises():
7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.fail('body of bad with executed')
7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except RuntimeError:
7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail('RuntimeError not reraised')
7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.enter_called)
7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.exit_called)
7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testExceptionInExit(self):
7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        body_executed = False
7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.Dummy(gobble=True) as a, self.ExitRaises():
7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            body_executed = True
7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.enter_called)
7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(a.exit_called)
7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(body_executed)
7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertNotEqual(a.exc_info[0], None)
7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testEnterReturnsTuple(self):
7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.Dummy(value=(1,2)) as (a1, a2), \
7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             self.Dummy(value=(10, 20)) as (b1, b2):
7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(1, a1)
7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(2, a2)
7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(10, b1)
7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(20, b2)
7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main():
7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    run_unittest(FailureTestCase, NonexceptionalTestCase,
7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 NestedNonexceptionalTestCase, ExceptionalTestCase,
7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 NonLocalFlowControlTestCase,
7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 AssignmentTargetTestCase,
7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 ExitSwallowsExceptionTestCase,
7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                 NestedWith)
7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__':
7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
751