From 3325d6759288577d003df845ceda33512d6b6697 Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Thu, 18 Jul 2019 08:23:16 -0600 Subject: [PATCH 1/2] raise error when trying to set _FillValue using setncattr. --- netCDF4/_netCDF4.pyx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/netCDF4/_netCDF4.pyx b/netCDF4/_netCDF4.pyx index 1b3c0aa98..f95ac3dc1 100644 --- a/netCDF4/_netCDF4.pyx +++ b/netCDF4/_netCDF4.pyx @@ -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() From 10eb9447bca03fca9938dbe6621bdd58678e20f3 Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Thu, 18 Jul 2019 08:49:01 -0600 Subject: [PATCH 2/2] add test, Changelog entry --- Changelog | 1 + test/tst_atts.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/Changelog b/Changelog index df751ee54..2b39d0afe 100644 --- a/Changelog +++ b/Changelog @@ -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) ================================== diff --git a/test/tst_atts.py b/test/tst_atts.py index 9e3d65371..9d2c07fca 100644 --- a/test/tst_atts.py +++ b/test/tst_atts.py @@ -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')