Commit a1fbea79 authored by Stefan Behnel's avatar Stefan Behnel

optimise calls to slice()

parent 08c21802
...@@ -9,6 +9,8 @@ Latest ...@@ -9,6 +9,8 @@ Latest
Features added Features added
-------------- --------------
* Calls to ``slice()`` are translated to a straight C-API call.
* Taking a ``char*`` from a temporary Python string object is safer * Taking a ``char*`` from a temporary Python string object is safer
in more cases and can be done inside of non-trivial expressions, in more cases and can be done inside of non-trivial expressions,
including arguments of a function call. A compile time error including arguments of a function call. A compile time error
......
...@@ -1327,6 +1327,24 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform): ...@@ -1327,6 +1327,24 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
return pos_args[0] return pos_args[0]
return node return node
def _handle_simple_function_slice(self, node, pos_args):
arg_count = len(pos_args)
start = step = None
if arg_count == 1:
stop, = pos_args
elif arg_count == 2:
start, stop = pos_args
elif arg_count == 3:
start, stop, step = pos_args
else:
self._error_wrong_arg_count('slice', node, pos_args)
return node
return ExprNodes.SliceNode(
node.pos,
start=start or ExprNodes.NoneNode(node.pos),
stop=stop,
step=step or ExprNodes.NoneNode(node.pos))
class YieldNodeCollector(Visitor.TreeVisitor): class YieldNodeCollector(Visitor.TreeVisitor):
def __init__(self): def __init__(self):
Visitor.TreeVisitor.__init__(self) Visitor.TreeVisitor.__init__(self)
......
# mode: run
def slice1(stop):
"""
>>> list(range(8))
[0, 1, 2, 3, 4, 5, 6, 7]
>>> list(range(10))[slice1(8)]
[0, 1, 2, 3, 4, 5, 6, 7]
>>> slice1(1)
slice(None, 1, None)
>>> slice1(10)
slice(None, 10, None)
>>> slice1(None)
slice(None, None, None)
>>> slice1(1) == slice(1)
True
>>> slice1(None) == slice(None)
True
"""
return slice(stop)
def slice1_const():
"""
>>> slice1_const() == slice(12)
True
"""
return slice(12)
def slice2(start, stop):
"""
>>> list(range(2, 8))
[2, 3, 4, 5, 6, 7]
>>> list(range(10))[slice2(2, 8)]
[2, 3, 4, 5, 6, 7]
>>> slice2(1, 10)
slice(1, 10, None)
>>> slice2(None, 10)
slice(None, 10, None)
>>> slice2(4, None)
slice(4, None, None)
"""
return slice(start, stop)
def slice2_const():
"""
>>> slice2_const() == slice(None, 12)
True
"""
return slice(None, 12)
def slice3(start, stop, step):
"""
>>> list(range(2, 8, 3))
[2, 5]
>>> list(range(10))[slice3(2, 8, 3)]
[2, 5]
>>> slice3(2, None, 3)
slice(2, None, 3)
>>> slice3(None, 3, 2)
slice(None, 3, 2)
"""
return slice(start, stop, step)
def slice3_const():
"""
>>> slice3_const() == slice(12, None, 34)
True
"""
return slice(12, None, 34)
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