Skip to content
Snippets Groups Projects
Commit bef65e56 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: Change re-entrancy logic of disabled ctx manager so that, if

parent 750e46d8
No related branches found
No related tags found
No related merge requests found
...@@ -33,9 +33,8 @@ import fsl.utils.ensure as ensure ...@@ -33,9 +33,8 @@ import fsl.utils.ensure as ensure
import fsl.data.melodicanalysis as fslma import fsl.data.melodicanalysis as fslma
_DISABLE_ASSERTIONS = False _DISABLE_ASSERTIONS = 0
""" """Semaphore used by the :func:`disabled` context manager. """
"""
@contextlib.contextmanager @contextlib.contextmanager
...@@ -43,18 +42,23 @@ def disabled(disable=True): ...@@ -43,18 +42,23 @@ def disabled(disable=True):
"""Context manager which allows assertion checks to be temporarily """Context manager which allows assertion checks to be temporarily
disabled. disabled.
If calls to this function are nested, only one of the calls need to be made
with ``disable=True`` for assertions to be disabled; any other calls which
are part of the call stack which set ``disable=False`` will have no effect.
:arg disable: Set to ``True`` (the default) to disable assertions, :arg disable: Set to ``True`` (the default) to disable assertions,
or ``False`` to enable them. or ``False`` to enable them.
""" """
global _DISABLE_ASSERTIONS global _DISABLE_ASSERTIONS
oldval = _DISABLE_ASSERTIONS if disable:
_DISABLE_ASSERTIONS = disable _DISABLE_ASSERTIONS += 1
try: try:
yield yield
finally: finally:
_DISABLE_ASSERTIONS = oldval if disable:
_DISABLE_ASSERTIONS -= 1
def _canDisable(func): def _canDisable(func):
...@@ -62,7 +66,7 @@ def _canDisable(func): ...@@ -62,7 +66,7 @@ def _canDisable(func):
via the :func:`disabled` context manager. via the :func:`disabled` context manager.
""" """
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if not _DISABLE_ASSERTIONS: if _DISABLE_ASSERTIONS == 0:
return func(*args, **kwargs) return func(*args, **kwargs)
return wrapper return wrapper
......
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