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
Boxiang Sun
cython
Commits
469db401
Commit
469db401
authored
Mar 11, 2013
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strip down for-in-loops with empty iterables
parent
4cfaa9f6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
1 deletion
+93
-1
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+8
-1
tests/run/constant_folding.py
tests/run/constant_folding.py
+68
-0
tests/run/constant_folding_cy.pyx
tests/run/constant_folding_cy.pyx
+17
-0
No files found.
Cython/Compiler/Optimize.py
View file @
469db401
...
@@ -3235,8 +3235,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
...
@@ -3235,8 +3235,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
def
visit_ForInStatNode
(
self
,
node
):
def
visit_ForInStatNode
(
self
,
node
):
self
.
visitchildren
(
node
)
self
.
visitchildren
(
node
)
# iterating over a list literal? => tuples are more efficient
sequence
=
node
.
iterator
.
sequence
sequence
=
node
.
iterator
.
sequence
if
isinstance
(
sequence
,
ExprNodes
.
SequenceNode
):
if
not
sequence
.
args
:
if
node
.
else_clause
:
return
node
.
else_clause
else
:
# don't break list comprehensions
return
Nodes
.
StatListNode
(
node
.
pos
,
stats
=
[])
# iterating over a list literal? => tuples are more efficient
if
isinstance
(
sequence
,
ExprNodes
.
ListNode
):
if
isinstance
(
sequence
,
ExprNodes
.
ListNode
):
node
.
iterator
.
sequence
=
sequence
.
as_tuple
()
node
.
iterator
.
sequence
=
sequence
.
as_tuple
()
return
node
return
node
...
...
tests/run/constant_folding.py
View file @
469db401
...
@@ -185,3 +185,71 @@ def while_true():
...
@@ -185,3 +185,71 @@ def while_true():
return
True
return
True
else
:
else
:
print
(
"FAIL"
)
print
(
"FAIL"
)
@
cython
.
test_fail_if_path_exists
(
"//ForInStatNode"
,
)
def
for_in_empty
():
"""
>>> for_in_empty()
"""
for
i
in
[]:
print
(
"LOOP"
)
@
cython
.
test_fail_if_path_exists
(
"//ForInStatNode"
,
)
def
for_in_empty_else
():
"""
>>> for_in_empty_else()
True
"""
for
i
in
[]:
print
(
"LOOP"
)
else
:
return
True
@
cython
.
test_fail_if_path_exists
(
"//ForInStatNode"
,
)
@
cython
.
test_assert_path_exists
(
"//ComprehensionNode"
,
)
def
for_in_empty_listcomp
():
"""
>>> for_in_empty_listcomp()
[]
"""
return
[
i
for
i
in
[]]
@
cython
.
test_fail_if_path_exists
(
"//ForInStatNode"
,
)
@
cython
.
test_assert_path_exists
(
"//ComprehensionNode"
,
)
def
for_in_empty_nested_listcomp
():
"""
>>> for_in_empty_nested_listcomp()
[]
"""
return
[
x
for
_
in
[]
for
x
in
[
1
,
2
,
3
]]
@
cython
.
test_fail_if_path_exists
(
"//ForInStatNode//ForInStatNode"
,
)
@
cython
.
test_assert_path_exists
(
"//ForInStatNode"
,
"//ComprehensionNode"
,
)
def
for_in_nested_listcomp
():
"""
>>> for_in_nested_listcomp()
[]
"""
return
[
x
for
x
in
[
1
,
2
,
3
]
for
_
in
[]]
tests/run/constant_folding_cy.pyx
View file @
469db401
...
@@ -80,3 +80,20 @@ def unicode_slicing_safe_surrogates2():
...
@@ -80,3 +80,20 @@ def unicode_slicing_safe_surrogates2():
"""
"""
ustring
=
u'abc
\
U00010000
def'
[:
2
]
ustring
=
u'abc
\
U00010000
def'
[:
2
]
return
ustring
return
ustring
@
cython
.
test_fail_if_path_exists
(
"//ForInStatNode"
,
)
@
cython
.
test_assert_path_exists
(
"//ComprehensionNode"
,
)
def
for_in_empty_setcomp
():
"""
>>> s = for_in_empty_setcomp()
>>> isinstance(s, set)
True
>>> len(s)
0
"""
return
{
i
for
i
in
[]}
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