Skip to content

Commit 00c81ff

Browse files
committed
Reject fractional scalar indices
1 parent 9669ac0 commit 00c81ff

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎Changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
version 1.7.4.1 (tag v1.7.4.1rel)
22
=================================
3+
* Reject fractional scalar indices instead of silently truncating reads or writes (issue #1350).
34
* Keep a numpy pin for Windows ARM64 on Python 3.10 so lockers can resolve that environment (issue #1491).
45
* Preserve surviving dimensions and decoded types for empty orthogonal selections,
56
including empty integer indices and all-false masks (PR #1499).

‎src/netCDF4/utils.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ def _StartCountStride(elem, shape, dimensions=None, grp=None, datashape=None,\
266266
# integer scalar
267267
elif ea.dtype.kind == 'i':
268268
newElem.append(e)
269+
# Scalar floats retain compatibility when they represent an exact
270+
# integer, but non-integral values are invalid indices. Letting
271+
# int(e) handle both cases silently truncates values such as 0.8.
272+
elif ea.dtype.kind == 'f' and not np.iterable(ea):
273+
if not np.isfinite(e) or e != np.floor(e):
274+
raise IndexError(IndexErrorMsg)
275+
newElem.append(int(e))
269276
# slice or ellipsis object
270277
elif type(e) == slice or type(e) == type(Ellipsis):
271278
if not use_get_vars and type(e) == slice and e.step not in [None,-1,1] and\

‎test/test_fancyslicing.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ def tearDown(self):
7676
# Remove the temporary files
7777
os.remove(self.file)
7878

79+
def test_float_scalar_get(self):
80+
with Dataset(self.file) as f:
81+
variable = f.variables['data']
82+
for index in (0.8, -0.8, np.float32(0.8), np.float64(-0.8), np.array(0.8)):
83+
with self.subTest(index=repr(index)):
84+
with self.assertRaises(IndexError):
85+
variable[index]
86+
for index in (1.0, np.float32(1.0), np.float64(1.0), np.array(1.0)):
87+
assert_array_equal(variable[index], variable[1])
88+
89+
def test_float_scalar_set(self):
90+
with Dataset(self.file, 'a') as f:
91+
variable = f.variables['data']
92+
original = variable[:].copy()
93+
for index in (0.8, -0.8, np.float32(0.8), np.float64(-0.8), np.array(0.8)):
94+
with self.subTest(index=repr(index)):
95+
with self.assertRaises(IndexError):
96+
variable[index] = -999
97+
assert_array_equal(variable[:], original)
98+
7999
def test_get(self):
80100
"""testing 'fancy indexing'"""
81101
f = Dataset(self.file, 'r')

‎test/test_utils.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ def test_1d(self):
184184
assert_equal(count, 0)
185185
assert_equal(_out_array_shape(count), (0,))
186186

187+
def test_float_scalar_index(self):
188+
# Integral float scalars retain the historical scalar-index behavior.
189+
for elem in (1.0, np.float32(1.0), np.float64(1.0)):
190+
start, count, stride, put_ind = _StartCountStride(elem, (10,))
191+
assert_equal(start, 1)
192+
assert_equal(count, 1)
193+
assert_equal(stride, 1)
194+
assert_equal(put_ind, -1)
195+
196+
# Fractional float scalars must not be silently truncated to an
197+
# integer index.
198+
for elem in (0.8, -0.8, np.float32(0.8), np.float64(-0.8)):
199+
with self.assertRaises(IndexError):
200+
_StartCountStride(elem, (10,))
201+
187202
def test_ellipsis(self):
188203
elem=(Ellipsis, slice(1, 4))
189204
start, count, stride, put_ind = _StartCountStride(elem, (22,25,4))

0 commit comments

Comments
 (0)