Skip to content

Commit

Permalink
2to3: Apply nonzero fixer.
Browse files Browse the repository at this point in the history
In Python 3 the `__nonzero__` class method is replaced by `__bool__`.
This only affects the MaskedArray class in numpy/oldnumeric/ma.py file
and the simplest solution is to provide both methods. I have my doubts
that the fixed up Python 3 version was correct or even tested, but I
think the current solution should work for as long as oldnumeric stays
in numpy.

Closes numpy#3073.
  • Loading branch information
charris committed Apr 13, 2013
1 parent 688bc60 commit 54ca3f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions numpy/oldnumeric/ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,18 @@ def __nonzero__(self):
return bool(m is not nomask and m.any()
or d is not nomask and d.any())

def __bool__(self):
"""returns true if any element is non-zero or masked
"""
# XXX: This changes bool conversion logic from MA.
# XXX: In MA bool(a) == len(a) != 0, but in numpy
# XXX: scalars do not have len
m = self._mask
d = self._data
return bool(m is not nomask and m.any()
or d is not nomask and d.any())

def __len__ (self):
"""Return length of first dimension. This is weird but Python's
slicing behavior depends on it."""
Expand Down Expand Up @@ -2134,10 +2146,13 @@ def asarray(data, dtype=None):
# XXX: I is better to to change the masked_*_operation adaptors
# XXX: to wrap ndarray methods directly to create ma.array methods.
from types import MethodType

def _m(f):
return MethodType(f, None, array)

def not_implemented(*args, **kwds):
raise NotImplementedError("not yet implemented for numpy.ma arrays")

array.all = _m(alltrue)
array.any = _m(sometrue)
array.argmax = _m(argmax)
Expand Down
2 changes: 1 addition & 1 deletion tools/py3tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
'methodattrs',
'ne',
# 'next',
# 'nonzero',
'nonzero',
'numliterals',
'operator',
'paren',
Expand Down

0 comments on commit 54ca3f2

Please sign in to comment.