10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Unit tests for contextlib.py, and other context managers."""
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport tempfile
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom contextlib import *  # Tests __all__
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotry:
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    import threading
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoexcept ImportError:
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    threading = None
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ContextManagerTestCase(unittest.TestCase):
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_plain(self):
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = []
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def woohoo():
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(1)
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield 42
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(999)
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with woohoo() as x:
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(state, [1])
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(x, 42)
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(x)
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [1, 42, 999])
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_finally(self):
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = []
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def woohoo():
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(1)
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield 42
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(999)
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ZeroDivisionError):
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with woohoo() as x:
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(state, [1])
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(x, 42)
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(x)
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                raise ZeroDivisionError()
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [1, 42, 999])
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_no_reraise(self):
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def whee():
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ctx = whee()
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ctx.__enter__()
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Calling __exit__ should not result in an exception
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(ctx.__exit__(TypeError, TypeError("foo"), None))
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_trap_yield_after_throw(self):
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def whoo():
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ctx = whoo()
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ctx.__enter__()
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        )
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_except(self):
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = []
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def woohoo():
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(1)
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield 42
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except ZeroDivisionError, e:
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(e.args[0])
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(state, [1, 42, 999])
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with woohoo() as x:
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(state, [1])
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(x, 42)
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(x)
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            raise ZeroDivisionError(999)
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [1, 42, 999])
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def _create_contextmanager_attribs(self):
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def attribs(**kw):
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def decorate(func):
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for k,v in kw.items():
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    setattr(func,k,v)
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return func
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return decorate
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @attribs(foo='bar')
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def baz(spam):
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            """Whee!"""
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return baz
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_attribs(self):
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        baz = self._create_contextmanager_attribs()
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(baz.__name__,'baz')
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(baz.foo, 'bar')
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @unittest.skipIf(sys.flags.optimize >= 2,
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "Docstrings are omitted with -O2 and above")
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_contextmanager_doc_attrib(self):
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        baz = self._create_contextmanager_attribs()
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(baz.__doc__, "Whee!")
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass NestedTestCase(unittest.TestCase):
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # XXX This needs more work
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested(self):
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield 1
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def b():
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield 2
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def c():
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield 3
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with nested(a(), b(), c()) as (x, y, z):
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(x, 1)
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(y, 2)
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(z, 3)
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested_cleanup(self):
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = []
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(1)
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield 2
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(3)
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def b():
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state.append(4)
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield 5
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            finally:
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(6)
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ZeroDivisionError):
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with nested(a(), b()) as (x, y):
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(x)
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(y)
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [1, 4, 2, 5, 6, 3])
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested_right_exception(self):
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield 1
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class b(object):
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __enter__(self):
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 2
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __exit__(self, *exc_info):
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    raise Exception()
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except:
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    pass
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ZeroDivisionError):
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with nested(a(), b()) as (x, y):
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual((x, y), (1, 2))
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested_b_swallows(self):
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def b():
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # Swallow the exception
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with nested(a(), b()):
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except ZeroDivisionError:
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("Didn't swallow ZeroDivisionError")
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested_break(self):
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = 0
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while True:
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state += 1
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with nested(a(), a()):
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                break
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state += 10
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, 1)
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested_continue(self):
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            yield
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = 0
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while state < 3:
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state += 1
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with nested(a(), a()):
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                continue
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            state += 10
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, 3)
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nested_return(self):
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        @contextmanager
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def a():
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            try:
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                yield
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            except:
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                pass
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def foo():
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with nested(a(), a()):
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 1
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return 10
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(foo(), 1)
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass ClosingTestCase(unittest.TestCase):
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # XXX This needs more work
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_closing(self):
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = []
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class C:
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def close(self):
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(1)
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = C()
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [])
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with closing(x) as y:
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(x, y)
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [1])
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_closing_error(self):
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        state = []
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class C:
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def close(self):
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                state.append(1)
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = C()
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [])
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ZeroDivisionError):
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with closing(x) as y:
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(x, y)
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(state, [1])
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass FileContextTestCase(unittest.TestCase):
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithOpen(self):
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tfn = tempfile.mktemp()
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f = None
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with open(tfn, "w") as f:
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertFalse(f.closed)
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                f.write("Booh\n")
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(f.closed)
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            f = None
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with self.assertRaises(ZeroDivisionError):
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                with open(tfn, "r") as f:
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertFalse(f.closed)
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertEqual(f.read(), "Booh\n")
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    1 // 0
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(f.closed)
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        finally:
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            test_support.unlink(tfn)
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao@unittest.skipUnless(threading, 'Threading required for this test.')
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass LockContextTestCase(unittest.TestCase):
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def boilerPlate(self, lock, locked):
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(locked())
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with lock:
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(locked())
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(locked())
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with self.assertRaises(ZeroDivisionError):
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            with lock:
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertTrue(locked())
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                1 // 0
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertFalse(locked())
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithLock(self):
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lock = threading.Lock()
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.boilerPlate(lock, lock.locked)
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithRLock(self):
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lock = threading.RLock()
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.boilerPlate(lock, lock._is_owned)
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithCondition(self):
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lock = threading.Condition()
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def locked():
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return lock._is_owned()
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.boilerPlate(lock, locked)
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithSemaphore(self):
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lock = threading.Semaphore()
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def locked():
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if lock.acquire(False):
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                lock.release()
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return False
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return True
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.boilerPlate(lock, locked)
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def testWithBoundedSemaphore(self):
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        lock = threading.BoundedSemaphore()
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def locked():
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if lock.acquire(False):
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                lock.release()
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return False
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return True
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.boilerPlate(lock, locked)
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# This is needed to make the test actually run under regrtest.py!
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main():
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    with test_support.check_warnings(("With-statements now directly support "
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      "multiple context managers",
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                      DeprecationWarning)):
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_support.run_unittest(__name__)
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
327