Skip to content

Commit 582ecf9

Browse files
committed
Fix __setitem__ assignment in the case of slices with None for start values.
1 parent aaa172d commit 582ecf9

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

pshmem/shmem.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,31 @@ def __setitem__(self, key, value):
225225
offset = list()
226226
if isinstance(key, slice):
227227
# Just one dimension
228-
offset.append(key.start)
228+
if key.start is None:
229+
offset.append(0)
230+
else:
231+
offset.append(key.start)
229232
else:
230233
# Is it iterable?
231234
try:
232235
for k in key:
233236
if isinstance(k, slice):
234-
offset.append(k.start)
237+
if k.start is None:
238+
offset.append(0)
239+
else:
240+
offset.append(k.start)
235241
else:
236242
# Must be an index
243+
if not isinstance(k, (int, np.integer)):
244+
msg = "[] key elements must be a slice or integer"
245+
raise ValueError(msg)
237246
offset.append(k)
238247
except TypeError:
239248
# No- must be an index
240-
offset.append(k)
249+
if not isinstance(key, (int, np.integer)):
250+
msg = "[] key must be scalar or tuple of slices or integers"
251+
raise ValueError(msg)
252+
offset.append(key)
241253
self.set(value, offset=offset, fromrank=from_rank)
242254

243255
def __iter__(self):

pshmem/test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ def test_allocate(self):
102102
try:
103103
# Same as set(), but using __setitem__ with an
104104
# allreduce to find which process is setting.
105+
#
105106
# key as a tuple of offsets
106107
shm[setoffset] = setdata
108+
107109
# key as a tuple slices
108110
if setdata is None:
109111
shm[None] = setdata
@@ -160,6 +162,16 @@ def test_allocate(self):
160162
# This should be bitwise identical, even for floats
161163
nt.assert_equal(check[:, :, :], truth[:, :, :])
162164

165+
# Try full array assignment with slices containing None start
166+
# values
167+
if p != self.rank:
168+
shm[None] = None
169+
else:
170+
shm[:, :, :] = local
171+
172+
check[:, :, :] = shm[:, :, :]
173+
nt.assert_equal(check[:, :, :], truth[:, :, :])
174+
163175
# Ensure that we can reference the memory buffer from numpy without
164176
# a memory copy. The intention is that a slice of the shared memory
165177
# buffer should appear as a C-contiguous ndarray whenever we slice

0 commit comments

Comments
 (0)