10000 ENH: Make memmaps return arrays in ufuncs and fancy indexing by seberg · Pull Request #2932 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Make memmaps return arrays in ufuncs and fancy indexing #2932

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

Closed
wants to merge 2 commits into from
Closed
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
BUG: Tweak memmap __array_*__ calls to avoid possible regressions
Need to call the underlying __array_*__ function to look exactly
the same to a class subclassing memmap. To make sure that multiply
inheritence would keep working exactly the same, need to do the
try/excepts. However __array_finalize__ does not really support
multiple inheritences well in any case.
  • Loading branch information
seberg committed Jan 22, 2013
commit e33b66490a3dccb01c32d877a25860a97fd2d73c
30 changes: 18 additions & 12 deletions numpy/core/memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,30 @@ def __array_finalize__(self, obj):
self.mode = None

def __array_wrap__(self, arr, context=None):
# If the type is not memmap, then a user subclass may have some special
# functions that should be preserved to avoid a regression
if type(self) is not memmap:
return arr.view(type=type(self))
return arr
# If the type is not memmap, then a user subclass may do some more
# things that need to be preserved. Otherwise, return a base array
# since it should not p
if type(self) is memmap and not np.may_share_memory(self, arr):
return arr
try:
return super(memmap, self).__array_wrap__(arr, context=context)
except TypeError:
return super(memmap, self).__array_wrap__(arr)

def __array_prepare__(self, arr, context=None):
if type(self) is not memmap:
return arr.view(type=type(self))
# There is no use in making an array a memmap, since it does not own
# its memory.
return arr
# There is no need to make an array a memmap, but we still have to do
# it in case this is not of memmap type.
if type(self) is memmap and not np.may_share_memory(self, arr):
return arr
try:
return super(memmap, self).__array_prepare__(arr, context=context)
except TypeError:
return super(memmap, self).__array_prepare__(arr)

def __getitem__(self, index):
# return an array if the result does not point to self's memory
res = super(memmap, self).__getitem__(index)
if type(res) is memmap and res._mmap is None:
return res.view(ndarray)
return res.view(type=ndarray)
return res

def flush(self):
Expand Down
7 changes: 6 additions & 1 deletion numpy/core/tests/test_memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ def test_view(self):
assert(new_array.base is fp)

def test_to_array_conversions(self):
class MMSubclass(memmap):
class DummyInerhitance(np.ndarray):
def __array_wrap__(self, arr):
arr = super(DummyInerhitance, self).__array_wrap__(arr)
arr.info = 'set'
class MMSubclass(memmap, DummyInerhitance):
pass
m = memmap(self.tmpfp, dtype=self.dtype, shape=self.shape)
r = m + 1
Expand All @@ -128,6 +132,7 @@ class MMSubclass(memmap):
m = MMSubclass(self.tmpfp, dtype=self.dtype, shape=self.shape)
r = m + 1
assert_(isinstance(r, MMSubclass))
assert_(hasattr(r, 'info'))
assert_(isinstance(m[:,[0,1]], MMSubclass))

if __name__ == "__main__":
Expand Down
0