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
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
6a65da7a
Commit
6a65da7a
authored
Dec 09, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify and generalise comprehension/genexpr handling fix for if-const optimisation
parent
ff095903
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
18 deletions
+49
-18
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+7
-18
tests/run/generators_py.py
tests/run/generators_py.py
+22
-0
tests/run/listcomp.pyx
tests/run/listcomp.pyx
+20
-0
No files found.
Cython/Compiler/Optimize.py
View file @
6a65da7a
...
...
@@ -3119,26 +3119,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
node
.
else_clause
=
if_clause
.
body
break
else
:
# False clauses can safely be deleted
assert
condition_result
==
False
# prevent killing generator expressions,
# but simplify them as much as possible
yield_expr_stat
=
self
.
_find_genexpr_yield_stat
(
if_clause
.
body
)
if
yield_expr_stat
is
not
None
:
# => if False: yield None
yield_expr
=
yield_expr_stat
.
expr
if_clause
.
condition
=
ExprNodes
.
BoolNode
(
if_clause
.
condition
.
pos
,
value
=
False
,
constant_result
=
False
)
yield_expr
.
arg
=
ExprNodes
.
NoneNode
(
yield_expr
.
arg
.
pos
)
if_clause
.
body
=
yield_expr_stat
if_clauses
.
append
(
if_clause
)
else
:
# False clauses outside of generators can safely be deleted
pass
if
not
if_clauses
:
if
if_clauses
:
node
.
if_clauses
=
if_clauses
return
node
elif
node
.
else_clause
:
return
node
.
else_clause
node
.
if_clauses
=
if_clauses
return
node
else
:
return
Nodes
.
StatListNode
(
node
.
pos
,
stats
=
[])
def
visit_ForInStatNode
(
self
,
node
):
self
.
visitchildren
(
node
)
...
...
tests/run/generators_py.py
View file @
6a65da7a
# mode: run
# tag: generators
import
cython
try
:
from
builtins
import
next
# Py3k
except
ImportError
:
...
...
@@ -362,3 +364,23 @@ def test_del_in_generator():
del
x
yield
a
del
a
@
cython
.
test_fail_if_path_exists
(
"//IfStatNode"
,
"//PrintStatNode"
)
def
test_yield_in_const_conditional_false
():
"""
>>> list(test_yield_in_const_conditional_false())
[]
"""
if
False
:
print
(
yield
1
)
@
cython
.
test_fail_if_path_exists
(
"//IfStatNode"
)
@
cython
.
test_assert_path_exists
(
"//PrintStatNode"
)
def
test_yield_in_const_conditional_true
():
"""
>>> list(test_yield_in_const_conditional_true())
None
[1]
"""
if
True
:
print
(
yield
1
)
tests/run/listcomp.pyx
View file @
6a65da7a
...
...
@@ -87,3 +87,23 @@ def sorted_listcomp(sequence):
[3, 4, 5]
"""
return
sorted
([
n
+
1
for
n
in
sequence
])
@
cython
.
test_fail_if_path_exists
(
"//IfStatNode"
,
"//ComprehensionAppendNode"
)
@
cython
.
test_assert_path_exists
(
"//ComprehensionNode"
)
def
listcomp_const_condition_false
():
"""
>>> listcomp_const_condition_false()
[]
"""
return
[
x
*
2
for
x
in
range
(
3
)
if
False
]
@
cython
.
test_fail_if_path_exists
(
"//IfStatNode"
)
@
cython
.
test_assert_path_exists
(
"//ComprehensionNode"
,
"//ComprehensionAppendNode"
)
def
listcomp_const_condition_true
():
"""
>>> listcomp_const_condition_true()
[0, 2, 4]
"""
return
[
x
*
2
for
x
in
range
(
3
)
if
True
]
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