Skip to content

Commit 0a1ea75

Browse files
committed
FIX: Immutable buffers should not be set writeable
1 parent a960b92 commit 0a1ea75

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

nibabel/streamlines/tck.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,8 @@ def _read(cls, fileobj, header, buffer_size=4):
432432
for point_part in point_parts:
433433
# Read floats.
434434
pts = np.frombuffer(point_part, dtype=dtype)
435-
# Enforce ability to write to underlying bytes object
436-
pts.flags.writeable = True
437435
# Convert data to little-endian if needed.
438-
yield pts.astype('<f4', copy=False).reshape([-1, 3])
436+
yield pts.astype('<f4').reshape([-1, 3])
439437

440438
n_streams += len(point_parts)
441439

nibabel/streamlines/tests/test_trk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ def trk_with_bytes(self, trk_key='simple_trk_fname', endian='<'):
105105
with open(DATA[trk_key], 'rb') as fobj:
106106
trk_bytes = fobj.read()
107107
dt = trk_module.header_2_dtype.newbyteorder(endian)
108-
trk_struct = np.ndarray((1,), dt, buffer=trk_bytes)
109-
trk_struct.flags.writeable = True
108+
trk_struct = np.ndarray((1,), dt, buffer=trk_bytes).copy()
110109
return trk_struct, trk_bytes
111110

112111
def test_load_file_with_wrong_information(self):

nibabel/volumeutils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,13 @@ def array_from_file(shape, in_dtype, infile, offset=0, order='F', mmap=True):
538538
n_read,
539539
getattr(infile, 'name', 'object')))
540540
arr = np.ndarray(shape, in_dtype, buffer=data_bytes, order=order)
541+
if not needs_copy:
542+
try:
543+
arr.flags.writeable = True
544+
except ValueError:
545+
needs_copy = True
541546
if needs_copy:
542-
return arr.copy()
543-
arr.flags.writeable = True
547+
arr = arr.copy()
544548
return arr
545549

546550

0 commit comments

Comments
 (0)