From bef65e569ec15a5401b22e282d3656d48843f2f6 Mon Sep 17 00:00:00 2001 From: Paul McCarthy <pauldmccarthy@gmail.com> Date: Tue, 20 Oct 2020 15:29:40 +0100 Subject: [PATCH] RF: Change re-entrancy logic of disabled ctx manager so that, if --- fsl/utils/assertions.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/fsl/utils/assertions.py b/fsl/utils/assertions.py index 63b004d1..5c7e90c9 100644 --- a/fsl/utils/assertions.py +++ b/fsl/utils/assertions.py @@ -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 -- GitLab