Skip to content
Snippets Groups Projects
Commit a5a22d3c authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added empty context manager

parent 0a4cc996
No related branches found
No related tags found
No related merge requests found
# v2.17.22 # v2.17.22
* added empty context manager
Context managers
================
.. autoclass:: satella.coding.EmptyContextManager
:members:
...@@ -12,6 +12,7 @@ Visit the project's page at GitHub_! ...@@ -12,6 +12,7 @@ Visit the project's page at GitHub_!
configuration/schema configuration/schema
configuration/sources configuration/sources
coding/ctxt_managers
coding/functions coding/functions
coding/futures coding/futures
coding/structures coding/structures
......
__version__ = '2.17.22a1' __version__ = '2.17.22a2'
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Just useful objects to make your coding nicer every day Just useful objects to make your coding nicer every day
""" """
from .ctxt_managers import EmptyContextManager
from .algos import merge_dicts from .algos import merge_dicts
from .concurrent import Monitor, RMonitor from .concurrent import Monitor, RMonitor
from .decorators import precondition, short_none, has_keys, \ from .decorators import precondition, short_none, has_keys, \
...@@ -23,6 +24,7 @@ from .expect_exception import expect_exception ...@@ -23,6 +24,7 @@ from .expect_exception import expect_exception
from .deep_compare import assert_equal, InequalityReason, Inequal from .deep_compare import assert_equal, InequalityReason, Inequal
__all__ = [ __all__ = [
'EmptyContextManager',
'assert_equal', 'InequalityReason', 'Inequal', 'assert_equal', 'InequalityReason', 'Inequal',
'Closeable', 'contains', 'enum_value', 'reraise_as', 'Closeable', 'contains', 'enum_value', 'reraise_as',
'expect_exception', 'expect_exception',
......
class EmptyContextManager:
"""
A context manager that does nothing. Only to support conditional change of context managers,
eg in such a way:
>>> if tx_enabled:
>>> ctxt = transaction.atomic
>>> else:
>>> ctxt = EmptyContextManager()
>>> with ctxt:
>>> ...
"""
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return False
import unittest import unittest
from satella.coding import precondition, short_none, has_keys, update_if_not_none, postcondition, \ from satella.coding import precondition, short_none, has_keys, update_if_not_none, postcondition, \
get_arguments, expect_exception get_arguments, expect_exception, EmptyContextManager
from satella.coding.decorators import for_argument from satella.coding.decorators import for_argument
from satella.exceptions import PreconditionError from satella.exceptions import PreconditionError
class TestTypecheck(unittest.TestCase): class TestTypecheck(unittest.TestCase):
def test_empty_ctxt_manager(self):
with EmptyContextManager() as p:
pass
def test_except_exception(self): def test_except_exception(self):
def expect_exception_1(): def expect_exception_1():
with expect_exception(KeyError, ValueError, 'Hello world!'): with expect_exception(KeyError, ValueError, 'Hello world!'):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment