Skip to content

Commit 03d8cc7

Browse files
authored
Merge branch 'master' into v1.7.5rel
2 parents 314255d + af5a34b commit 03d8cc7

5 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎.github/workflows/cibuildwheel.yml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
if: ${{ github.event_name }} == "pull_request"
8686

8787
- name: "Building ${{ matrix.os }} (${{ matrix.arch }}) wheels"
88-
uses: pypa/cibuildwheel@v4.3.0
88+
uses: pypa/cibuildwheel@v4.3.1
8989
env:
9090
CIBW_ARCHS: ${{ matrix.arch }}
9191

@@ -128,7 +128,7 @@ jobs:
128128
python=${{ matrix.python-version }} libnetcdf=4.10.1 --channel conda-forge
129129
130130
- name: Build wheels for Windows/Mac
131-
uses: pypa/cibuildwheel@v4.3.0
131+
uses: pypa/cibuildwheel@v4.3.1
132132
env:
133133
CIBW_ARCHS: ${{ matrix.arch }}
134134

@@ -167,7 +167,7 @@ jobs:
167167
echo "NETCDF4_DIR=C:\vcpkg\installed\arm64-windows" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
168168
169169
- name: Build wheels for Windows ARM64
170-
uses: pypa/cibuildwheel@v4.3.0
170+
uses: pypa/cibuildwheel@v4.3.1
171171
env:
172172
CIBW_ARCHS: ARM64
173173
CIBW_SKIP: "cp310-*"

‎Changelog‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
12
version 1.7.5 (tag v1.7.5rel)
23
=============================
4+
* Reject fractional scalar indices instead of silently truncating reads or writes (issue #1350).
35
* Keep a numpy pin for Windows ARM64 on Python 3.10 so lockers can resolve that environment (issue #1491).
46
* Preserve surviving dimensions and decoded types for empty orthogonal selections,
57
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)