Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
a1fbea79
Commit
a1fbea79
authored
Jul 07, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimise calls to slice()
parent
08c21802
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
94 additions
and
0 deletions
+94
-0
CHANGES.rst
CHANGES.rst
+2
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+18
-0
tests/run/builtin_slice.pyx
tests/run/builtin_slice.pyx
+74
-0
No files found.
CHANGES.rst
View file @
a1fbea79
...
...
@@ -9,6 +9,8 @@ Latest
Features added
--------------
* Calls to ``slice()`` are translated to a straight C-API call.
* Taking a ``char*`` from a temporary Python string object is safer
in more cases and can be done inside of non-trivial expressions,
including arguments of a function call. A compile time error
...
...
Cython/Compiler/Optimize.py
View file @
a1fbea79
...
...
@@ -1327,6 +1327,24 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
return
pos_args
[
0
]
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
):
def
__init__
(
self
):
Visitor
.
TreeVisitor
.
__init__
(
self
)
...
...
tests/run/builtin_slice.pyx
0 → 100644
View file @
a1fbea79
# 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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment