Skip to content

Commit

Permalink
Merge pull request scipy#8633 from rgommers/netcdf-append-issue
Browse files Browse the repository at this point in the history
BUG: io: fix regression in `io.netcdf_file` with append mode.
  • Loading branch information
pv authored Apr 1, 2018
2 parents 47c0a24 + 5d0e3b0 commit fadc841
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scipy/io/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,8 @@ def _read_var_array(self):
else:
pos = self.fp.tell()
self.fp.seek(begin_)
data = frombuffer(self.fp.read(a_size), dtype=dtype_)
data = frombuffer(self.fp.read(a_size), dtype=dtype_
).copy()
data.shape = shape
self.fp.seek(pos)

Expand All @@ -723,7 +724,8 @@ def _read_var_array(self):
else:
pos = self.fp.tell()
self.fp.seek(begin)
rec_array = frombuffer(self.fp.read(self._recs*self._recsize), dtype=dtypes)
rec_array = frombuffer(self.fp.read(self._recs*self._recsize),
dtype=dtypes).copy()
rec_array.shape = (self._recs,)
self.fp.seek(pos)

Expand Down Expand Up @@ -766,7 +768,7 @@ def _read_att_values(self):
self.fp.read(-count % 4) # read padding

if typecode is not 'c':
values = frombuffer(values, dtype='>%s' % typecode)
values = frombuffer(values, dtype='>%s' % typecode).copy()
if values.shape == (1,):
values = values[0]
else:
Expand Down
15 changes: 15 additions & 0 deletions scipy/io/tests/test_netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ def test_itemset_no_segfault_on_readonly():
assert_raises(RuntimeError, time_var.assignValue, 42)


def test_appending_issue_gh_8625():
stream = BytesIO()

with make_simple(stream, mode='w') as f:
f.createDimension('x', 2)
f.createVariable('x', float, ('x',))
f.variables['x'][...] = 1
f.flush()
contents = stream.getvalue()

stream = BytesIO(contents)
with netcdf_file(stream, mode='a') as f:
f.variables['x'][...] = 2


def test_write_invalid_dtype():
dtypes = ['int64', 'uint64']
if np.dtype('int').itemsize == 8: # 64-bit machines
Expand Down

0 comments on commit fadc841

Please sign in to comment.