mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
Issue #25609: Introduce contextlib.AbstractContextManager and
typing.ContextManager.
This commit is contained in:
parent
c5b5ba9bda
commit
9e080e0e74
8 changed files with 138 additions and 19 deletions
|
@ -12,6 +12,39 @@ except ImportError:
|
|||
threading = None
|
||||
|
||||
|
||||
class TestAbstractContextManager(unittest.TestCase):
|
||||
|
||||
def test_enter(self):
|
||||
class DefaultEnter(AbstractContextManager):
|
||||
def __exit__(self, *args):
|
||||
super().__exit__(*args)
|
||||
|
||||
manager = DefaultEnter()
|
||||
self.assertIs(manager.__enter__(), manager)
|
||||
|
||||
def test_exit_is_abstract(self):
|
||||
class MissingExit(AbstractContextManager):
|
||||
pass
|
||||
|
||||
with self.assertRaises(TypeError):
|
||||
MissingExit()
|
||||
|
||||
def test_structural_subclassing(self):
|
||||
class ManagerFromScratch:
|
||||
def __enter__(self):
|
||||
return self
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
return None
|
||||
|
||||
self.assertTrue(issubclass(ManagerFromScratch, AbstractContextManager))
|
||||
|
||||
class DefaultEnter(AbstractContextManager):
|
||||
def __exit__(self, *args):
|
||||
super().__exit__(*args)
|
||||
|
||||
self.assertTrue(issubclass(DefaultEnter, AbstractContextManager))
|
||||
|
||||
|
||||
class ContextManagerTestCase(unittest.TestCase):
|
||||
|
||||
def test_contextmanager_plain(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue