Skip to content

Commit 7116b1d

Browse files
committed
Modified normalize_slice in cf/functions.py to fix #774
1 parent 7a09fef commit 7116b1d

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ version NEXTRELEASE
77
* Fix bug where `cf.example_fields` returned a `list`
88
of Fields rather than a `Fieldlist`
99
(https://github.com/NCAS-CMS/cf-python/issues/725)
10+
* Fix bug where `cf.functions.normalize_slice` doesn't correctly
11+
handle certain cyclic slices
12+
(https://github.com/NCAS-CMS/cf-python/issues/774)
1013

1114
----
1215

cf/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,8 +2132,8 @@ def normalize_slice(index, size, cyclic=False):
21322132
return slice(start, stop, step)
21332133

21342134
if not (
2135-
(step > 0 and start < 0 and stop > 0)
2136-
or (step < 0 and start > 0 and stop < 0)
2135+
(step > 0 and start < 0 and stop >= 0)
2136+
or (step < 0 and start >= 0 and stop < 0)
21372137
):
21382138
raise IndexError(
21392139
f"{index!r} is not a {'cyclic ' if cyclic else ''}slice"

cf/test/test_functions.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,24 @@ def test_normalize_slice(self):
389389
cf.normalize_slice(slice(2, 5, -2), 8, cyclic=True),
390390
slice(2, -3, -2),
391391
)
392-
392+
393+
self.assertEqual(
394+
cf.normalize_slice(slice(-8, 0, 1), 8, cyclic=True),
395+
slice(-8, 0, 1)
396+
)
397+
self.assertEqual(
398+
cf.normalize_slice(slice(0, 7, -1), 8, cyclic=True),
399+
slice(0, 7, -1)
400+
)
401+
self.assertEqual(
402+
cf.normalize_slice(slice(-1, -8, 1), 8, cyclic=True),
403+
slice(-1, 0, 1)
404+
)
405+
self.assertEqual(
406+
cf.normalize_slice(slice(-8, -1, -1), 8, cyclic=True),
407+
slice(0, -1, -1)
408+
)
409+
393410
with self.assertRaises(IndexError):
394411
cf.normalize_slice([1, 2], 8)
395412

0 commit comments

Comments
 (0)