Commit bef65e56 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

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

parent 750e46d8
......@@ -33,9 +33,8 @@ import fsl.utils.ensure as ensure
import fsl.data.melodicanalysis as fslma
_DISABLE_ASSERTIONS = False
"""
"""
_DISABLE_ASSERTIONS = 0
"""Semaphore used by the :func:`disabled` context manager. """
@contextlib.contextmanager
......@@ -43,18 +42,23 @@ def disabled(disable=True):
"""Context manager which allows assertion checks to be temporarily
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,
or ``False`` to enable them.
"""
global _DISABLE_ASSERTIONS
oldval = _DISABLE_ASSERTIONS
_DISABLE_ASSERTIONS = disable
if disable:
_DISABLE_ASSERTIONS += 1
try:
yield
finally:
_DISABLE_ASSERTIONS = oldval
if disable:
_DISABLE_ASSERTIONS -= 1
def _canDisable(func):
......@@ -62,7 +66,7 @@ def _canDisable(func):
via the :func:`disabled` context manager.
"""
def wrapper(*args, **kwargs):
if not _DISABLE_ASSERTIONS:
if _DISABLE_ASSERTIONS == 0:
return func(*args, **kwargs)
return wrapper
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment