mirror of
https://github.com/python/cpython.git
synced 2025-08-24 10:45:53 +00:00
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:
parent
20d48a44a5
commit
0784a2e5b1
4 changed files with 57 additions and 19 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue