Skip to content

Commit 25aa812

Browse files
authored
Merge pull request #326 from abergeron/bool
Add proper conversion to bool.
2 parents 70bc75f + 68b0222 commit 25aa812

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pygpu/gpuarray.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,14 @@ cdef class GpuArray:
16571657
"""
16581658
return pygpu_as_ndarray(self)
16591659

1660+
def __bool__(self):
1661+
if self.size == 0:
1662+
return False
1663+
elif self.size == 1:
1664+
return bool(numpy.asarray(self))
1665+
else:
1666+
raise ValueError('The truth value of a multi-element array is ambiguous')
1667+
16601668
def _empty_like_me(self, dtype=None, order='C'):
16611669
"""
16621670
_empty_like_me(dtype=None, order='C')

pygpu/tests/test_gpu_ndarray.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def test_hash():
4747
assert exc is not None
4848

4949

50+
def test_bool():
51+
for data in [numpy.empty((0, 33)), [[1]], [[0]], [], [0], [1], 0, 1]:
52+
assert bool(pygpu.asarray(data, context=ctx)) == bool(numpy.asarray(data))
53+
54+
5055
def test_transfer():
5156
for shp in [(), (5,), (6, 7), (4, 8, 9), (1, 8, 9)]:
5257
for dtype in dtypes_all:

0 commit comments

Comments
 (0)