diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fcc263666fc22ce9ce656dead149e247c746bd3..00a244bb454dd7413bf29803749c0ae17ed3a557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ # v2.17.22 + +* added empty context manager diff --git a/docs/coding/ctxt_managers.rst b/docs/coding/ctxt_managers.rst new file mode 100644 index 0000000000000000000000000000000000000000..2bcd52fa3c498cbd2fa967c375ee92f876962d00 --- /dev/null +++ b/docs/coding/ctxt_managers.rst @@ -0,0 +1,5 @@ +Context managers +================ + +.. autoclass:: satella.coding.EmptyContextManager + :members: diff --git a/docs/index.rst b/docs/index.rst index fe5dbf0cf21b926261b4451c1e75eeec83d0c346..601fa559a1d69a3c9a875b155c5519cbd32b7d1a 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,6 +12,7 @@ Visit the project's page at GitHub_! configuration/schema configuration/sources + coding/ctxt_managers coding/functions coding/futures coding/structures diff --git a/satella/__init__.py b/satella/__init__.py index 0583b47668151ea0d04c1bac69634ccdb14db621..b9ea9dfcae90c79113a2d668b088f97b36b13fab 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.17.22a1' +__version__ = '2.17.22a2' diff --git a/satella/coding/__init__.py b/satella/coding/__init__.py index 1d58a54bb1864415f7401fb26b4b3b241c3680f3..b8963fa4d7c99dd537db82e6d03f5a7db76d3aca 100644 --- a/satella/coding/__init__.py +++ b/satella/coding/__init__.py @@ -2,6 +2,7 @@ Just useful objects to make your coding nicer every day """ +from .ctxt_managers import EmptyContextManager from .algos import merge_dicts from .concurrent import Monitor, RMonitor from .decorators import precondition, short_none, has_keys, \ @@ -23,6 +24,7 @@ from .expect_exception import expect_exception from .deep_compare import assert_equal, InequalityReason, Inequal __all__ = [ + 'EmptyContextManager', 'assert_equal', 'InequalityReason', 'Inequal', 'Closeable', 'contains', 'enum_value', 'reraise_as', 'expect_exception', diff --git a/satella/coding/ctxt_managers.py b/satella/coding/ctxt_managers.py new file mode 100644 index 0000000000000000000000000000000000000000..cf2d20e345c91eb99b4fe218dc3418778a67abad --- /dev/null +++ b/satella/coding/ctxt_managers.py @@ -0,0 +1,19 @@ +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 + + diff --git a/tests/test_coding/test_debug.py b/tests/test_coding/test_debug.py index 23d405091cf802040dffaa54f1dc885197c4fa98..d9ce597e45113bba0c80c547782946122579f2c7 100644 --- a/tests/test_coding/test_debug.py +++ b/tests/test_coding/test_debug.py @@ -1,13 +1,17 @@ import unittest 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.exceptions import PreconditionError class TestTypecheck(unittest.TestCase): + def test_empty_ctxt_manager(self): + with EmptyContextManager() as p: + pass + def test_except_exception(self): def expect_exception_1(): with expect_exception(KeyError, ValueError, 'Hello world!'):