Skip to content

Commit

Permalink
Merge pull request #961 from Unidata/issue959
Browse files Browse the repository at this point in the history
have Variable.setncattr fail when setting _FillValue
  • Loading branch information
jswhit authored Jul 18, 2019
2 parents 0bc37d8 + 10eb944 commit 5bf0184
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* remove underline ANSI in Dataset string representation (pull request #956).
* remove newlines from string representation (pull request #960).
* fix for issue #957 (size of scalar var is a float since numpy.prod(())=1.0).
* make sure Variable.setncattr fails to set _FillValue (issue #959).

version 1.5.1.2 (tag v1.5.1.2rel)
==================================
Expand Down
7 changes: 7 additions & 0 deletions netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4112,6 +4112,13 @@ netCDF attribute with the same name as one of the reserved python
attributes."""
cdef nc_type xtype
xtype=-99
# issue #959 - trying to set _FillValue results in mysterious
# error when close method is called so catch it here. It is
# already caught in __setattr__.
if name == '_FillValue':
msg='_FillValue attribute must be set when variable is '+\
'created (using fill_value keyword to createVariable)'
raise AttributeError(msg)
if self._grp.data_model != 'NETCDF4': self._grp._redef()
_set_att(self._grp, self._varid, name, value, xtype=xtype, force_ncstring=self._ncstring_attrs__)
if self._grp.data_model != 'NETCDF4': self._grp._enddef()
Expand Down
13 changes: 13 additions & 0 deletions test/tst_atts.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def setUp(self):
v1.seqatt = SEQATT
v1.stringseqatt = STRINGSEQATT
v1.setncattr_string('stringseqatt_array',STRINGSEQATT) # array of NC_STRING
# issue #959: should not be able to set _FillValue after var creation
try:
v1._FillValue(-999.)
except AttributeError:
pass
else:
raise ValueError('This test should have failed.')
try:
v1.setncattr('_FillValue',-999.)
except AttributeError:
pass
else:
raise ValueError('This test should have failed.')
# issue #485 (triggers segfault in C lib
# with version 1.2.1 without pull request #486)
f.foo = NP.array('bar','S')
Expand Down

0 comments on commit 5bf0184

Please sign in to comment.