1.1 --- a/lib/__builtins__/sequence.py Thu Dec 01 23:06:26 2016 +0100
1.2 +++ b/lib/__builtins__/sequence.py Thu Dec 01 23:27:23 2016 +0100
1.3 @@ -49,7 +49,7 @@
1.4 # Handle slices separately.
1.5
1.6 elif _isinstance(index, slice):
1.7 - return self.__getslice__(index.start, index.end)
1.8 + return self.__getslice__(index.start, index.end, index.step)
1.9
1.10 # No other kinds of objects are supported as indexes.
1.11
1.12 @@ -77,9 +77,16 @@
1.13 else:
1.14 raise TypeError()
1.15
1.16 - def __getslice__(self, start, end=None):
1.17 + def __getslice__(self, start, end=None, step=1):
1.18
1.19 - "Return a slice starting from 'start', with the optional 'end'."
1.20 + """
1.21 + Return a slice of the sequence starting from the 'start' index, ending
1.22 + before the optional 'end' (or at the end of the sequence), and providing
1.23 + items at the frequency given by 'step' (with a default step of 1).
1.24 + """
1.25 +
1.26 + if step == 0:
1.27 + raise ValueError(step)
1.28
1.29 length = self.__len__()
1.30
1.31 @@ -101,9 +108,9 @@
1.32
1.33 result = []
1.34
1.35 - while start < end:
1.36 + while step > 0 and start < end or step < 0 and start > end:
1.37 result.append(self.__get_single_item__(start))
1.38 - start += 1
1.39 + start += step
1.40
1.41 return result
1.42
2.1 --- a/lib/operator/__init__.py Thu Dec 01 23:06:26 2016 +0100
2.2 +++ b/lib/operator/__init__.py Thu Dec 01 23:27:23 2016 +0100
2.3 @@ -80,4 +80,6 @@
2.4 pos,
2.5 )
2.6
2.7 +from __builtins__.span import slice # for Sliceobj instantiation
2.8 +
2.9 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/slice.py Thu Dec 01 23:27:23 2016 +0100
3.3 @@ -0,0 +1,12 @@
3.4 +l = range(0, 10)
3.5 +print l # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.6 +
3.7 +s = slice(2, 5)
3.8 +print s # __builtins__.span.slice(2, 5, 1)
3.9 +
3.10 +print l[s] # [2, 3, 4]
3.11 +print l[2:5] # [2, 3, 4]
3.12 +print l[2:5:-1] # []
3.13 +print l[5:2:-1] # [5, 4, 3]
3.14 +print l[1:9:2] # [1, 3, 5, 7]
3.15 +print l[9:1:-2] # [9, 7, 5, 3]