bpo-10049: Add a "no-op" (null) context manager to contextlib (GH-4464)

Adds a simpler and faster alternative to ExitStack for handling
single optional context managers without having to change the
lexical structure of your code.
This commit is contained in:
Jesse-Bakker 2017-11-23 01:23:28 +01:00 committed by Nick Coghlan
parent 20d48a44a5
commit 0784a2e5b1
4 changed files with 57 additions and 19 deletions

View file

@ -252,6 +252,16 @@ class ClosingTestCase(unittest.TestCase):
1 / 0
self.assertEqual(state, [1])
class NullcontextTestCase(unittest.TestCase):
def test_nullcontext(self):
class C:
pass
c = C()
with nullcontext(c) as c_in:
self.assertIs(c_in, c)
class FileContextTestCase(unittest.TestCase):
def testWithOpen(self):