Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions mkl_random/_patch_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions mkl_random/tests/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Loading