8000 ENH: Add `madvise` for `memmap` objects by IndifferentArea · Pull Request #29260 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add madvise for memmap objects #29260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
lint
  • Loading branch information
IndifferentArea committed Jun 23, 2025
commit fff4ad4c2fd8575333d05533701b397f83fc89d7
4 changes: 1 addition & 3 deletions numpy/_core/memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,7 @@ def madvise(self, option):
self._mmap.madvise(option)
except AttributeError:
# This can happen if mmap.madvise is not available on the system
raise NotImplementedError(
"mmap.madvise is not available on this system."
)
raise NotImplementedError("mmap.madvise is not available on this system.")
except OSError as e:
raise OSError(e.errno, e.strerror + ". Check if 'option' is valid.") from e

Expand Down
18 changes: 10 additions & 8 deletions numpy/_core/tests/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,27 @@ def test_shape_type(self):
@pytest.mark.skipif(not hasattr(mmap, "MADV_NORMAL") and hasattr(mmap, "madvise"),
reason="mmap.MADV_NORMAL is not available on this system.")
def test_madvise_normal(self):
fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
shape=self.shape)
fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape)
fp[:] = self.data[:]
# This should not raise an error
fp.madvise(mmap.MADV_NORMAL)

def test_madvise_no_mmap(self):
7AF4 fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
shape=self.shape)
fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape)
# Manually set _mmap to None to simulate a closed/unbacked memmap
fp._mmap = None
expected_err_msg = "madvise cannot be used: the memory map is not backed by any memory."
expected_err_msg = (
"madvise cannot be used: the memory map is not backed by any memory."
)
with pytest.raises(TypeError, match=expected_err_msg):
fp.madvise(mmap.MADV_NORMAL)

def test_madvise_on_view(self):
fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+',
shape=self.shape)
fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape)
view = fp[1:] # Create a view
expected_err_msg = "madvise cannot be used on a view of a memmap object. Please use it on the original memmap object."
expected_err_msg = (
"madvise cannot be used on a view of a memmap object. "
"Please use it on the original memmap object."
)
with pytest.raises(TypeError, match=expected_err_msg):
view.madvise(mmap.MADV_NORMAL)
Loading
0