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 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()