Skip to content

Commit

Permalink
len_slice: Catch more cases where length is unknown.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Dec 12, 2016
1 parent 7deb3e8 commit ab4ab22
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 12 additions & 5 deletions kenjutsu/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,21 @@ def len_slice(a_slice, a_length=None):

new_slice_size = 0
if isinstance(new_slice, slice):
if new_slice.stop is None:
if new_slice.step > 0:
if (new_slice.step > 0 and new_slice.start >= 0 and
(new_slice.stop is None or new_slice.stop < 0)):
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined end"
"Cannot determine slice length without a defined start"
" point. The reformatted slice was %s." % repr(new_slice)
)
else:
new_slice = slice(new_slice.start, -1, new_slice.step)
elif (new_slice.step < 0 and new_slice.start < 0 and
(new_slice.stop is None or new_slice.stop >= 0)):
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined start"
" point. The reformatted slice was %s." % repr(new_slice)
)

if new_slice.step < 0 and new_slice.stop is None:
new_slice = slice(new_slice.start, -1, new_slice.step)

new_slice_diff = float(new_slice.stop - new_slice.start)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def test_len_slice(self):
with self.assertRaises(measure.UnknownSliceLengthException):
measure.len_slice(slice(None))

with self.assertRaises(measure.UnknownSliceLengthException):
measure.len_slice(slice(None, None, -1))

for size in [10, 11, 12]:
excess = size + 3
each_range = range(size)
Expand Down

0 comments on commit ab4ab22

Please sign in to comment.