From 6edf6e29bbabcec4455f373d5fa903d24ea0f1e5 Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Fri, 3 May 2019 11:18:00 -0600 Subject: [PATCH 1/4] fix for issue922 --- netCDF4/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/netCDF4/utils.py b/netCDF4/utils.py index 261328c55..2e3999c03 100644 --- a/netCDF4/utils.py +++ b/netCDF4/utils.py @@ -365,7 +365,9 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\ datashape = broadcasted_shape(shape, datashape) # pad datashape with zeros for dimensions not being sliced (issue #906) - if datashape: + # only used when there is a slice over only one dimension + if datashape and len(datashape) != len(elem) and\ + len(datashape) == sum(1 for e in elem if type(e) == slice): datashapenew = (); i=0 for e in elem: if type(e) != slice: From 46eb4d064eb148f92d264dec1a9f3ea0e91dbe9f Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Fri, 3 May 2019 13:39:44 -0600 Subject: [PATCH 2/4] fix to issue #922 --- Changelog | 4 ++++ netCDF4/utils.py | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Changelog b/Changelog index 46a5112a4..60e1b9f89 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,7 @@ + since version 1.5.1.1 +============================== + * fix another slicing bug introduced by the fix to issue #906 (issue #922). + version 1.5.1.1 (tag v1.5.1.1rel) ================================== * fixed __version__ attribute (was set incorrectly in 1.5.1 release). diff --git a/netCDF4/utils.py b/netCDF4/utils.py index 2e3999c03..f3332c4c5 100644 --- a/netCDF4/utils.py +++ b/netCDF4/utils.py @@ -409,10 +409,13 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\ if unlim and e.stop is not None and e.stop > shape[i]: length = e.stop elif unlim and e.stop is None and datashape != (): - if e.start is None: - length = datashape[i] - else: - length = e.start+datashape[i] + try: + if e.start is None: + length = datashape[i] + else: + length = e.start+datashape[i] + except IndexError: + raise IndexError("Illegal slice") else: if unlim and datashape == () and len(dim) == 0: # writing scalar along unlimited dimension using slicing From 56eded44a8bce6a52d87f3495c00199adb4445e7 Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Fri, 3 May 2019 13:42:05 -0600 Subject: [PATCH 3/4] update --- netCDF4/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netCDF4/utils.py b/netCDF4/utils.py index f3332c4c5..86fa75799 100644 --- a/netCDF4/utils.py +++ b/netCDF4/utils.py @@ -415,7 +415,7 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\ else: length = e.start+datashape[i] except IndexError: - raise IndexError("Illegal slice") + raise IndexError("shape of data does not conform to slice") else: if unlim and datashape == () and len(dim) == 0: # writing scalar along unlimited dimension using slicing From 5004e700f5d1e38aa4c233009ea9f914359ab917 Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Fri, 3 May 2019 14:09:20 -0600 Subject: [PATCH 4/4] update --- netCDF4/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/netCDF4/utils.py b/netCDF4/utils.py index 86fa75799..fea3a3c05 100644 --- a/netCDF4/utils.py +++ b/netCDF4/utils.py @@ -365,14 +365,14 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\ datashape = broadcasted_shape(shape, datashape) # pad datashape with zeros for dimensions not being sliced (issue #906) - # only used when there is a slice over only one dimension + # only used when data covers slice over subset of dimensions if datashape and len(datashape) != len(elem) and\ len(datashape) == sum(1 for e in elem if type(e) == slice): datashapenew = (); i=0 for e in elem: - if type(e) != slice: + if type(e) != slice and not np.iterable(e): # scalar integer slice datashapenew = datashapenew + (0,) - else: + else: # slice object datashapenew = datashapenew + (datashape[i],) i+=1 datashape = datashapenew