Skip to content

Commit 416b3e2

Browse files
authored
Merge pull request #104 from IntelPython/use-warning-in-patch
Use `RuntimeWarning` when unpatching more than patching
2 parents f048c07 + b8ea875 commit 416b3e2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

mkl_random/_patch_numpy.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
"""Define functions for patching NumPy with MKL-based NumPy interface."""
2727

28+
import warnings
2829
from contextlib import ContextDecorator
2930
from threading import Lock, local
3031

@@ -85,11 +86,12 @@ def do_restore(self, verbose=False):
8586
with self._lock:
8687
local_count = getattr(self._tls, "local_count", 0)
8788
if local_count <= 0:
88-
if verbose:
89-
print(
90-
"Warning: restore_numpy_random called more times than "
91-
"patch_numpy_random in this thread."
92-
)
89+
warnings.warn(
90+
"restore_numpy_random called more times than "
91+
"patch_numpy_random in this thread.",
92+
RuntimeWarning,
93+
stacklevel=2,
94+
)
9395
return
9496
self._tls.local_count -= 1
9597
self._patch_count -= 1

mkl_random/tests/test_patch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2525

2626
import numpy as np
27+
import pytest
2728

2829
import mkl_random
2930
import mkl_random.interfaces.numpy_random as _nrand
@@ -103,3 +104,10 @@ def test_patch_reentrant():
103104
finally:
104105
while mkl_random.is_patched():
105106
mkl_random.restore_numpy_random()
107+
108+
109+
def test_patch_warning():
110+
if mkl_random.is_patched():
111+
pytest.skip("This test should not be run with a pre-patched NumPy.")
112+
with pytest.warns(RuntimeWarning, match="restore_numpy_random*"):
113+
mkl_random.restore_numpy_random()

0 commit comments

Comments
 (0)