Skip to content

Commit 5ae4bf4

Browse files
authored
Merge pull request #295 from IntelPython/use-warning-in-patch
Use `RuntimeWarning` when unpatching more than patching
2 parents 27583c9 + 546661a commit 5ae4bf4

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [dev] - YYYY-MM-DD
88

99
### Added
10-
* Added `mkl_fft` patching for NumPy, with `mkl_fft` context manager, `is_patched` query, and `patch_numpy_fft` and `restore_numpy_fft` calls to replace `numpy.fft` calls with calls from `mkl_fft.interfaces.numpy_fft` [gh-224](https://github.com/IntelPython/mkl_fft/pull/224)
10+
* Added `mkl_fft` patching for NumPy, with `mkl_fft` context manager, `is_patched` query, and `patch_numpy_fft` and `restore_numpy_fft` calls to replace `numpy.fft` calls with calls from `mkl_fft.interfaces.numpy_fft` [gh-224](https://github.com/IntelPython/mkl_fft/pull/224), [gh-295](https://github.com/IntelPython/mkl_fft/pull/295)
1111

1212
### Changed
1313
* In `mkl_fft.fftn` and `mkl_fft.ifftn`, improved checking of the shape argument `s` to use faster direct transforms more often. This makes performance more consistent between `mkl_fft.fftn/ifftn` and `mkl.interfaces`. [gh-283](https://github.com/IntelPython/mkl_fft/pull/283)

mkl_fft/_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_fft called more times than "
91-
"patch_numpy_fft in this thread."
92-
)
89+
warnings.warn(
90+
"restore_numpy_fft called more times than "
91+
"patch_numpy_fft in this thread.",
92+
RuntimeWarning,
93+
stacklevel=2,
94+
)
9395
return
9496
self._tls.local_count -= 1
9597
self._patch_count -= 1

mkl_fft/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_fft
2930
import mkl_fft.interfaces.numpy_fft as _nfft
@@ -78,3 +79,10 @@ def test_patch_reentrant():
7879

7980
assert not mkl_fft.is_patched()
8081
assert np.fft.fft.__module__ == old_module
82+
83+
84+
def test_patch_warning():
85+
if mkl_fft.is_patched():
86+
pytest.skip("This test should not be run with a pre-patched NumPy.")
87+
with pytest.warns(RuntimeWarning, match="restore_numpy_fft*"):
88+
mkl_fft.restore_numpy_fft()

0 commit comments

Comments
 (0)