Skip to content

Commit d63c5ee

Browse files
authored
Merge pull request #494 from abergeron/fix_==
Stop comparing to None or Ellipsis with '==' (or equivalent)
2 parents ab2a43a + 9c5811b commit d63c5ee

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

pygpu/gpuarray.pyx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ cdef bytes _s(s):
4040
return s
4141
raise TypeError("Expected a string")
4242

43+
cdef size_t countis(l, object val):
44+
cdef size_t count
45+
cdef size_t i
46+
count = 0
47+
for i in range(len(l)):
48+
if l[i] is val:
49+
count += 1
50+
return count
51+
4352
def cl_wrap_ctx(size_t ptr):
4453
"""
4554
cl_wrap_ctx(ptr)
@@ -244,7 +253,7 @@ cdef int strides_ok(GpuArray a, strides):
244253
if a.ga.dimensions[i] == 0:
245254
return 1
246255

247-
max_axis_offset = strides[i] * (a.ga.dimensions[i] - 1)
256+
max_axis_offset = <ssize_t>(strides[i]) * <ssize_t>(a.ga.dimensions[i] - 1)
248257
if max_axis_offset > 0:
249258
if upper + max_axis_offset > size:
250259
return 0
@@ -1951,7 +1960,7 @@ cdef class GpuArray:
19511960
key = tuple(key)
19521961

19531962
# Need to massage Ellipsis here, to avoid packing it into a tuple.
1954-
if key.count(Ellipsis) > 1:
1963+
if countis(key, Ellipsis) > 1:
19551964
raise IndexError, "cannot use more than one Ellipsis"
19561965

19571966
# The following code replaces an Ellipsis found in the key by
@@ -1966,7 +1975,7 @@ cdef class GpuArray:
19661975
else:
19671976
# Need number of axes minus missing dimensions extra slice(None)
19681977
# objects, not counting None entries and the Ellipsis itself
1969-
num_slcs = self.ga.nd - (len(key) - key.count(None) - 1)
1978+
num_slcs = self.ga.nd - (len(key) - countis(key, None) - 1)
19701979
fill_slices = (slice(None),) * num_slcs
19711980
key = key[:ell_idx] + fill_slices + key[ell_idx + 1:]
19721981

@@ -1983,7 +1992,7 @@ cdef class GpuArray:
19831992

19841993
# Slice into array, then reshape, accommodating for None entries in key
19851994
sliced = self.__cgetitem__(getitem_idcs)
1986-
if key.count(None) == 0:
1995+
if countis(key, None) == 0:
19871996
# Avoid unnecessary reshaping if there was no None
19881997
return sliced
19891998
else:
@@ -2085,7 +2094,7 @@ cdef class GpuArray:
20852094

20862095
idx = tuple(idx)
20872096

2088-
if idx.count(Ellipsis) > 1:
2097+
if countis(idx, Ellipsis) > 1:
20892098
raise IndexError, "cannot use more than one Ellipsis"
20902099

20912100
# Remove None entries, they should be ignored (as in Numpy)
@@ -2425,7 +2434,7 @@ cdef class GpuKernel:
24252434
"""
24262435
__call__(*args, n=None, gs=None, ls=None, shared=0)
24272436
"""
2428-
if n == None and (ls == None or gs == None):
2437+
if n is None and (ls is None or gs is None):
24292438
raise ValueError, "Must specify size (n) or both gs and ls"
24302439
self.do_call(n, gs, ls, args, shared)
24312440

0 commit comments

Comments
 (0)