Commit 49e6b1f3 authored by cjgibson's avatar cjgibson

Reworking slicing unittests to include sliced type - added unsuccessful...

Reworking slicing unittests to include sliced type - added unsuccessful attempt to resolve issue #2508.
parent a729ca91
...@@ -4808,8 +4808,30 @@ class SliceIndexNode(ExprNode): ...@@ -4808,8 +4808,30 @@ class SliceIndexNode(ExprNode):
# if (__pyx_v_VARNAME == Py_None) { __pyx_t_TMPIDX = PY_SSIZE_T_MAX; } else { __pyx_t_TMPIDX = __Pyx_PyIndex_AsSsize_t(__pyx_v_VARNAME); } # if (__pyx_v_VARNAME == Py_None) { __pyx_t_TMPIDX = PY_SSIZE_T_MAX; } else { __pyx_t_TMPIDX = __Pyx_PyIndex_AsSsize_t(__pyx_v_VARNAME); }
c_int = PyrexTypes.c_py_ssize_t_type c_int = PyrexTypes.c_py_ssize_t_type
if self.start: if self.start:
# self.start = CondExprNode(
# self.start.pos,
# true_val = IntNode(self.start.pos, value = '0'),
# false_val = self.start,
# test = PrimaryCmpNode(
# self.start.pos,
# operand1 = self.start,
# operator = 'is',
# operand2 = NoneNode(self.pos)
# )
# )
self.start = self.start.coerce_to(c_int, env) self.start = self.start.coerce_to(c_int, env)
if self.stop: if self.stop:
# self.stop = CondExprNode(
# self.stop.pos,
# true_val = IntNode(self.start.pos, value = 'PY_SSIZE_T_MAX'),
# false_val = self.stop,
# test = PrimaryCmpNode(
# self.stop.pos,
# operand1 = self.stop,
# operator = 'is',
# operand2 = NoneNode(self.pos)
# )
# )
self.stop = self.stop.coerce_to(c_int, env) self.stop = self.stop.coerce_to(c_int, env)
self.is_temp = 1 self.is_temp = 1
return self return self
......
...@@ -204,6 +204,23 @@ def slice_charp_repeat(py_string_arg): ...@@ -204,6 +204,23 @@ def slice_charp_repeat(py_string_arg):
s = slice_val s = slice_val
return s[1:3].decode(u'ASCII') return s[1:3].decode(u'ASCII')
# Readers will find the common boilerplate in the tests below:
# >>> l = [1,2,3,4,5]
# >>> t = tuple(l)
# >>> b = ''.join(map(str, l)).encode('ASCII')
# >>> u = b.decode('ASCII')
# >>> o = (l, t, b, u)
# >>> n = ('list', 'tuple', 'bytes', 'unicode')
# >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
# >>> r = lambda i, *a: '%s[%s] -> %s' % (n[i], ':'.join(map(repr, a)), FUNCTION_NAME(o[i], *a))
# Originally, this was planned to be a basic iteration over
# the various object types contained within the slicable fused
# type, but Python2 -> Python3 semantics changed the class names
# and string representations used for raw bytes and unicode.
# As a result, we dynamically adjust the printed string output
# for each test in order to ensure consistent results when running
# both Python2 and Python3.
ctypedef fused slicable: ctypedef fused slicable:
list list
tuple tuple
...@@ -216,27 +233,38 @@ def slice_fused_type_start(slicable seq, start): ...@@ -216,27 +233,38 @@ def slice_fused_type_start(slicable seq, start):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, s: '%s[%r:] -> %s' % (n[i], s, p(slice_fused_type_start(o[i], s)))
... for s in (0, 4, 5, -5): >>> for i in range(len(o)):
... print(p(slice_fused_type_start(o, s))) ... for s in (0, len(l) - 1, len(l), -1, -len(l), None):
... print(r(i, s))
... ...
[1, 2, 3, 4, 5] list[0:] -> [1, 2, 3, 4, 5]
[5] list[4:] -> [5]
[] list[5:] -> []
[1, 2, 3, 4, 5] list[-1:] -> [5]
(1, 2, 3, 4, 5) list[-5:] -> [1, 2, 3, 4, 5]
(5,) list[None:] -> [1, 2, 3, 4, 5]
() tuple[0:] -> (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5) tuple[4:] -> (5,)
12345 tuple[5:] -> ()
5 tuple[-1:] -> (5,)
<BLANKLINE> tuple[-5:] -> (1, 2, 3, 4, 5)
12345 tuple[None:] -> (1, 2, 3, 4, 5)
12345 bytes[0:] -> 12345
5 bytes[4:] -> 5
<BLANKLINE> bytes[5:] ->
12345 bytes[-1:] -> 5
bytes[-5:] -> 12345
bytes[None:] -> 12345
unicode[0:] -> 12345
unicode[4:] -> 5
unicode[5:] ->
unicode[-1:] -> 5
unicode[-5:] -> 12345
unicode[None:] -> 12345
""" """
obj = seq[start:] obj = seq[start:]
return obj return obj
...@@ -247,31 +275,38 @@ def slice_fused_type_stop(slicable seq, stop): ...@@ -247,31 +275,38 @@ def slice_fused_type_stop(slicable seq, stop):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, s: '%s[:%r] -> %s' % (n[i], s, p(slice_fused_type_stop(o[i], s)))
... for s in (0, 4, 5, -5, None): >>> for i in range(len(o)):
... print(p(slice_fused_type_stop(o, s))) ... for s in (0, len(l) - 1, len(l), -1, -len(l), None):
... print(r(i, s))
... ...
[] list[:0] -> []
[1, 2, 3, 4] list[:4] -> [1, 2, 3, 4]
[1, 2, 3, 4, 5] list[:5] -> [1, 2, 3, 4, 5]
[] list[:-1] -> [1, 2, 3, 4]
[1, 2, 3, 4, 5] list[:-5] -> []
() list[:None] -> [1, 2, 3, 4, 5]
(1, 2, 3, 4) tuple[:0] -> ()
(1, 2, 3, 4, 5) tuple[:4] -> (1, 2, 3, 4)
() tuple[:5] -> (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5) tuple[:-1] -> (1, 2, 3, 4)
<BLANKLINE> tuple[:-5] -> ()
1234 tuple[:None] -> (1, 2, 3, 4, 5)
12345 bytes[:0] ->
<BLANKLINE> bytes[:4] -> 1234
12345 bytes[:5] -> 12345
<BLANKLINE> bytes[:-1] -> 1234
1234 bytes[:-5] ->
12345 bytes[:None] -> 12345
<BLANKLINE> unicode[:0] ->
12345 unicode[:4] -> 1234
unicode[:5] -> 12345
unicode[:-1] -> 1234
unicode[:-5] ->
unicode[:None] -> 12345
""" """
obj = seq[:stop] obj = seq[:stop]
return obj return obj
...@@ -282,156 +317,39 @@ def slice_fused_type_start_and_stop(slicable seq, start, stop): ...@@ -282,156 +317,39 @@ def slice_fused_type_start_and_stop(slicable seq, start, stop):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, t, s: '%s[%r:%r] -> %s' % (n[i], t, s, p(slice_fused_type_start_and_stop(o[i], t, s)))
... for start in (0, 1, 4, 5, -5, None): >>> for i in range(len(o)):
... for stop in (0, 4, 5, 10, -10, None): ... for start, stop in ((0, len(l)), (0, None), (None, len(l)),
... print(p(slice_fused_type_start_and_stop(o, start, stop))) ... (-len(l), 0), (1, 0), (0, 1)):
... print(r(i, start, stop))
... ...
[] list[0:5] -> [1, 2, 3, 4, 5]
[1, 2, 3, 4] list[0:None] -> [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] list[None:5] -> [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] list[-5:0] -> []
[] list[1:0] -> []
[1, 2, 3, 4, 5] list[0:1] -> [1]
[] tuple[0:5] -> (1, 2, 3, 4, 5)
[2, 3, 4] tuple[0:None] -> (1, 2, 3, 4, 5)
[2, 3, 4, 5] tuple[None:5] -> (1, 2, 3, 4, 5)
[2, 3, 4, 5] tuple[-5:0] -> ()
[] tuple[1:0] -> ()
[2, 3, 4, 5] tuple[0:1] -> (1,)
[] bytes[0:5] -> 12345
[] bytes[0:None] -> 12345
[5] bytes[None:5] -> 12345
[5] bytes[-5:0] ->
[] bytes[1:0] ->
[5] bytes[0:1] -> 1
[] unicode[0:5] -> 12345
[] unicode[0:None] -> 12345
[] unicode[None:5] -> 12345
[] unicode[-5:0] ->
[] unicode[1:0] ->
[] unicode[0:1] -> 1
[]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[]
[1, 2, 3, 4, 5]
[]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[]
[1, 2, 3, 4, 5]
()
(1, 2, 3, 4)
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
()
(1, 2, 3, 4, 5)
()
(2, 3, 4)
(2, 3, 4, 5)
(2, 3, 4, 5)
()
(2, 3, 4, 5)
()
()
(5,)
(5,)
()
(5,)
()
()
()
()
()
()
()
(1, 2, 3, 4)
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
()
(1, 2, 3, 4, 5)
()
(1, 2, 3, 4)
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
()
(1, 2, 3, 4, 5)
<BLANKLINE>
1234
12345
12345
<BLANKLINE>
12345
<BLANKLINE>
234
2345
2345
<BLANKLINE>
2345
<BLANKLINE>
<BLANKLINE>
5
5
<BLANKLINE>
5
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
1234
12345
12345
<BLANKLINE>
12345
<BLANKLINE>
1234
12345
12345
<BLANKLINE>
12345
<BLANKLINE>
1234
12345
12345
<BLANKLINE>
12345
<BLANKLINE>
234
2345
2345
<BLANKLINE>
2345
<BLANKLINE>
<BLANKLINE>
5
5
<BLANKLINE>
5
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
<BLANKLINE>
1234
12345
12345
<BLANKLINE>
12345
<BLANKLINE>
1234
12345
12345
<BLANKLINE>
12345
""" """
obj = seq[start:stop] obj = seq[start:stop]
return obj return obj
...@@ -442,42 +360,47 @@ def slice_fused_type_step(slicable seq, step): ...@@ -442,42 +360,47 @@ def slice_fused_type_step(slicable seq, step):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, s: '%s[::%r] -> %s' % (n[i], s, p(slice_fused_type_step(o[i], s)))
... for s in (1, -1, 2, -3, 10, -10, None): >>> for i in range(len(o)):
... print(p(slice_fused_type_step(o, s))) ... for s in (1, -1, 2, -3, 5, -5, None):
... print(r(i, s))
... ...
[1, 2, 3, 4, 5] list[::1] -> [1, 2, 3, 4, 5]
[5, 4, 3, 2, 1] list[::-1] -> [5, 4, 3, 2, 1]
[1, 3, 5] list[::2] -> [1, 3, 5]
[5, 2] list[::-3] -> [5, 2]
[1] list[::5] -> [1]
[5] list[::-5] -> [5]
[1, 2, 3, 4, 5] list[::None] -> [1, 2, 3, 4, 5]
(1, 2, 3, 4, 5) tuple[::1] -> (1, 2, 3, 4, 5)
(5, 4, 3, 2, 1) tuple[::-1] -> (5, 4, 3, 2, 1)
(1, 3, 5) tuple[::2] -> (1, 3, 5)
(5, 2) tuple[::-3] -> (5, 2)
(1,) tuple[::5] -> (1,)
(5,) tuple[::-5] -> (5,)
(1, 2, 3, 4, 5) tuple[::None] -> (1, 2, 3, 4, 5)
12345 bytes[::1] -> 12345
54321 bytes[::-1] -> 54321
135 bytes[::2] -> 135
52 bytes[::-3] -> 52
1 bytes[::5] -> 1
5 bytes[::-5] -> 5
12345 bytes[::None] -> 12345
12345 unicode[::1] -> 12345
54321 unicode[::-1] -> 54321
135 unicode[::2] -> 135
52 unicode[::-3] -> 52
1 unicode[::5] -> 1
5 unicode[::-5] -> 5
12345 unicode[::None] -> 12345
>>> for o in (l, t, b): >>> for v in o:
... try: slice_fused_type_step(o, 0) ... try: slice_fused_type_step(v, 0)
... except ValueError: pass ... except ValueError: pass
... try: slice_fused_type_step(v, v)
... except TypeError: pass
""" """
obj = seq[::step] obj = seq[::step]
return obj return obj
...@@ -488,112 +411,56 @@ def slice_fused_type_start_and_step(slicable seq, start, step): ...@@ -488,112 +411,56 @@ def slice_fused_type_start_and_step(slicable seq, start, step):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, s, t: '%s[%r::%r] -> %s' % (n[i], s, t, p(slice_fused_type_start_and_step(o[i], s, t)))
... for start in (0, 2, 5, -5, None): >>> for i in range(len(o)):
... for step in (1, -1, 2, -3, None): ... for start, step in ((0, 1), (0, -1), (1, 1), (1, -1),
... print(p(slice_fused_type_start_and_step(o, start, step))) ... (None, 1), (None, -1), (None, None),
... (1, 2), (len(l), -2), (len(l), len(l))):
... print(r(i, start, step))
... ...
[1, 2, 3, 4, 5] list[0::1] -> [1, 2, 3, 4, 5]
[1] list[0::-1] -> [1]
[1, 3, 5] list[1::1] -> [2, 3, 4, 5]
[1] list[1::-1] -> [2, 1]
[1, 2, 3, 4, 5] list[None::1] -> [1, 2, 3, 4, 5]
[3, 4, 5] list[None::-1] -> [5, 4, 3, 2, 1]
[3, 2, 1] list[None::None] -> [1, 2, 3, 4, 5]
[3, 5] list[1::2] -> [2, 4]
[3] list[5::-2] -> [5, 3, 1]
[3, 4, 5] list[5::5] -> []
[] tuple[0::1] -> (1, 2, 3, 4, 5)
[5, 4, 3, 2, 1] tuple[0::-1] -> (1,)
[] tuple[1::1] -> (2, 3, 4, 5)
[5, 2] tuple[1::-1] -> (2, 1)
[] tuple[None::1] -> (1, 2, 3, 4, 5)
[1, 2, 3, 4, 5] tuple[None::-1] -> (5, 4, 3, 2, 1)
[1] tuple[None::None] -> (1, 2, 3, 4, 5)
[1, 3, 5] tuple[1::2] -> (2, 4)
[1] tuple[5::-2] -> (5, 3, 1)
[1, 2, 3, 4, 5] tuple[5::5] -> ()
[1, 2, 3, 4, 5] bytes[0::1] -> 12345
[5, 4, 3, 2, 1] bytes[0::-1] -> 1
[1, 3, 5] bytes[1::1] -> 2345
[5, 2] bytes[1::-1] -> 21
[1, 2, 3, 4, 5] bytes[None::1] -> 12345
(1, 2, 3, 4, 5) bytes[None::-1] -> 54321
(1,) bytes[None::None] -> 12345
(1, 3, 5) bytes[1::2] -> 24
(1,) bytes[5::-2] -> 531
(1, 2, 3, 4, 5) bytes[5::5] ->
(3, 4, 5) unicode[0::1] -> 12345
(3, 2, 1) unicode[0::-1] -> 1
(3, 5) unicode[1::1] -> 2345
(3,) unicode[1::-1] -> 21
(3, 4, 5) unicode[None::1] -> 12345
() unicode[None::-1] -> 54321
(5, 4, 3, 2, 1) unicode[None::None] -> 12345
() unicode[1::2] -> 24
(5, 2) unicode[5::-2] -> 531
() unicode[5::5] ->
(1, 2, 3, 4, 5)
(1,)
(1, 3, 5)
(1,)
(1, 2, 3, 4, 5)
(1, 2, 3, 4, 5)
(5, 4, 3, 2, 1)
(1, 3, 5)
(5, 2)
(1, 2, 3, 4, 5)
12345
1
135
1
12345
345
321
35
3
345
<BLANKLINE>
54321
<BLANKLINE>
52
<BLANKLINE>
12345
1
135
1
12345
12345
54321
135
52
12345
12345
1
135
1
12345
345
321
35
3
345
<BLANKLINE>
54321
<BLANKLINE>
52
<BLANKLINE>
12345
1
135
1
12345
12345
54321
135
52
12345
>>> for o in (l, t, b): >>> for o in (l, t, b):
... try: slice_fused_type_start_and_step(o, 0, 0) ... try: slice_fused_type_start_and_step(o, 0, 0)
... except ValueError: pass ... except ValueError: pass
...@@ -607,115 +474,49 @@ def slice_fused_type_stop_and_step(slicable seq, stop, step): ...@@ -607,115 +474,49 @@ def slice_fused_type_stop_and_step(slicable seq, stop, step):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, s, t: '%s[:%r:%r] -> %s' % (n[i], s, t, p(slice_fused_type_stop_and_step(o[i], s, t)))
... for stop in (5, 10, 3, -10, None): >>> for i in range(len(o)):
... for step in (1, -1, 2, -3, None): ... for stop, step in ((len(l), 1), (len(l), None), (None, 1),
... print(p(slice_fused_type_stop_and_step(o, stop, step))) ... (len(l), -1), (len(l) - 1, 2), (len(l), -2),
... (len(l), len(l))):
... print(r(i, stop, step))
... ...
[1, 2, 3, 4, 5] list[:5:1] -> [1, 2, 3, 4, 5]
[] list[:5:None] -> [1, 2, 3, 4, 5]
[1, 3, 5] list[:None:1] -> [1, 2, 3, 4, 5]
[] list[:5:-1] -> []
[1, 2, 3, 4, 5] list[:4:2] -> [1, 3]
[1, 2, 3, 4, 5] list[:5:-2] -> []
[] list[:5:5] -> [1]
[1, 3, 5] tuple[:5:1] -> (1, 2, 3, 4, 5)
[] tuple[:5:None] -> (1, 2, 3, 4, 5)
[1, 2, 3, 4, 5] tuple[:None:1] -> (1, 2, 3, 4, 5)
[1, 2, 3] tuple[:5:-1] -> ()
[5] tuple[:4:2] -> (1, 3)
[1, 3] tuple[:5:-2] -> ()
[5] tuple[:5:5] -> (1,)
[1, 2, 3] bytes[:5:1] -> 12345
[] bytes[:5:None] -> 12345
[5, 4, 3, 2, 1] bytes[:None:1] -> 12345
[] bytes[:5:-1] ->
[5, 2] bytes[:4:2] -> 13
[] bytes[:5:-2] ->
[1, 2, 3, 4, 5] bytes[:5:5] -> 1
[5, 4, 3, 2, 1] unicode[:5:1] -> 12345
[1, 3, 5] unicode[:5:None] -> 12345
[5, 2] unicode[:None:1] -> 12345
[1, 2, 3, 4, 5] unicode[:5:-1] ->
(1, 2, 3, 4, 5) unicode[:4:2] -> 13
() unicode[:5:-2] ->
(1, 3, 5) unicode[:5:5] -> 1
() >>> for v in o:
(1, 2, 3, 4, 5) ... try: slice_fused_type_stop_and_step(v, len(l), 0)
(1, 2, 3, 4, 5)
()
(1, 3, 5)
()
(1, 2, 3, 4, 5)
(1, 2, 3)
(5,)
(1, 3)
(5,)
(1, 2, 3)
()
(5, 4, 3, 2, 1)
()
(5, 2)
()
(1, 2, 3, 4, 5)
(5, 4, 3, 2, 1)
(1, 3, 5)
(5, 2)
(1, 2, 3, 4, 5)
12345
<BLANKLINE>
135
<BLANKLINE>
12345
12345
<BLANKLINE>
135
<BLANKLINE>
12345
123
5
13
5
123
<BLANKLINE>
54321
<BLANKLINE>
52
<BLANKLINE>
12345
54321
135
52
12345
12345
<BLANKLINE>
135
<BLANKLINE>
12345
12345
<BLANKLINE>
135
<BLANKLINE>
12345
123
5
13
5
123
<BLANKLINE>
54321
<BLANKLINE>
52
<BLANKLINE>
12345
54321
135
52
12345
>>> for o in (l, t, b):
... try: slice_fused_type_stop_and_step(o, 5, 0)
... except ValueError: pass ... except ValueError: pass
... try: slice_fused_type_stop_and_step(v, len(l), v)
... except TypeError: pass
""" """
obj = seq[:stop:step] obj = seq[:stop:step]
return obj return obj
...@@ -726,44 +527,57 @@ def slice_fused_type_all(slicable seq, start, stop, step): ...@@ -726,44 +527,57 @@ def slice_fused_type_all(slicable seq, start, stop, step):
>>> t = tuple(l) >>> t = tuple(l)
>>> b = ''.join(map(str, l)).encode('ASCII') >>> b = ''.join(map(str, l)).encode('ASCII')
>>> u = b.decode('ASCII') >>> u = b.decode('ASCII')
>>> o = (l, t, b, u)
>>> n = ('list', 'tuple', 'bytes', 'unicode')
>>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o) >>> p = lambda o: o.decode() if isinstance(o, type(b)) else str(o)
>>> for o in (l, t, b, u): >>> r = lambda i, s, t, e: '%s[%r:%r:%r] -> %s' % (n[i], s, t, e, p(slice_fused_type_all(o[i], s, t, e)))
... for args in ((0, 5, 1), (5, 0, -1), (None, 5, 1), (5, None, -1), >>> for i in range(len(o)):
... (-100, 100, None), (None, None, None), (1, 3, 2), (5, 1, -3)): ... for args in ((0, len(l), 1), (len(l), 0, -1), (None, len(l), 1),
... print(p(slice_fused_type_all(o, *args))) ... (len(l), None, -1), (-len(l), len(l), None), (None, None, None),
... (1, 3, 2), (len(l), 1, -3), (len(l), 0, 1)):
... print(r(i, *args))
... ...
[1, 2, 3, 4, 5] list[0:5:1] -> [1, 2, 3, 4, 5]
[5, 4, 3, 2] list[5:0:-1] -> [5, 4, 3, 2]
[1, 2, 3, 4, 5] list[None:5:1] -> [1, 2, 3, 4, 5]
[5, 4, 3, 2, 1] list[5:None:-1] -> [5, 4, 3, 2, 1]
[1, 2, 3, 4, 5] list[-5:5:None] -> [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5] list[None:None:None] -> [1, 2, 3, 4, 5]
[2] list[1:3:2] -> [2]
[5] list[5:1:-3] -> [5]
(1, 2, 3, 4, 5) list[5:0:1] -> []
(5, 4, 3, 2) tuple[0:5:1] -> (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5) tuple[5:0:-1] -> (5, 4, 3, 2)
(5, 4, 3, 2, 1) tuple[None:5:1] -> (1, 2, 3, 4, 5)
(1, 2, 3, 4, 5) tuple[5:None:-1] -> (5, 4, 3, 2, 1)
(1, 2, 3, 4, 5) tuple[-5:5:None] -> (1, 2, 3, 4, 5)
(2,) tuple[None:None:None] -> (1, 2, 3, 4, 5)
(5,) tuple[1:3:2] -> (2,)
12345 tuple[5:1:-3] -> (5,)
5432 tuple[5:0:1] -> ()
12345 bytes[0:5:1] -> 12345
54321 bytes[5:0:-1] -> 5432
12345 bytes[None:5:1] -> 12345
12345 bytes[5:None:-1] -> 54321
2 bytes[-5:5:None] -> 12345
5 bytes[None:None:None] -> 12345
12345 bytes[1:3:2] -> 2
5432 bytes[5:1:-3] -> 5
12345 bytes[5:0:1] ->
54321 unicode[0:5:1] -> 12345
12345 unicode[5:0:-1] -> 5432
12345 unicode[None:5:1] -> 12345
2 unicode[5:None:-1] -> 54321
5 unicode[-5:5:None] -> 12345
unicode[None:None:None] -> 12345
unicode[1:3:2] -> 2
unicode[5:1:-3] -> 5
unicode[5:0:1] ->
>>> for v in o:
... try: slice_fused_type_stop_and_step(v, len(l), 0)
... except ValueError: pass
... try: slice_fused_type_stop_and_step(v, len(l), v)
... except TypeError: pass
""" """
obj = seq[start:stop:step] obj = seq[start:stop:step]
return obj return obj
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment