From 54fa129691f7d3db45d2f2b1d176fb7afb498364 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Wed, 18 Mar 2026 13:44:18 -0700 Subject: [PATCH 1/2] Use warning when calling restore more than patch in a thread --- mkl_random/_patch_numpy.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mkl_random/_patch_numpy.py b/mkl_random/_patch_numpy.py index 7d518e7..9879a5b 100644 --- a/mkl_random/_patch_numpy.py +++ b/mkl_random/_patch_numpy.py @@ -25,6 +25,7 @@ """Define functions for patching NumPy with MKL-based NumPy interface.""" +import warnings from contextlib import ContextDecorator from threading import Lock, local @@ -85,11 +86,12 @@ def do_restore(self, verbose=False): with self._lock: local_count = getattr(self._tls, "local_count", 0) if local_count <= 0: - if verbose: - print( - "Warning: restore_numpy_random called more times than " - "patch_numpy_random in this thread." - ) + warnings.warn( + "restore_numpy_random called more times than " + "patch_numpy_random in this thread.", + RuntimeWarning, + stacklevel=2, + ) return self._tls.local_count -= 1 self._patch_count -= 1 From b8ea8752f7c3344e3f008d0b4b1a211644586e34 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Thu, 19 Mar 2026 08:02:12 -0700 Subject: [PATCH 2/2] add test for unpatching more times than patching raising warning --- mkl_random/tests/test_patch.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mkl_random/tests/test_patch.py b/mkl_random/tests/test_patch.py index beffc1c..3fa1cf3 100644 --- a/mkl_random/tests/test_patch.py +++ b/mkl_random/tests/test_patch.py @@ -24,6 +24,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import numpy as np +import pytest import mkl_random import mkl_random.interfaces.numpy_random as _nrand @@ -103,3 +104,10 @@ def test_patch_reentrant(): finally: while mkl_random.is_patched(): mkl_random.restore_numpy_random() + + +def test_patch_warning(): + if mkl_random.is_patched(): + pytest.skip("This test should not be run with a pre-patched NumPy.") + with pytest.warns(RuntimeWarning, match="restore_numpy_random*"): + mkl_random.restore_numpy_random()