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
ae0b93d9
Commit
ae0b93d9
authored
Nov 09, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix compiler crash for generator expressions with a constant False condition
parent
42a7ea89
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
0 deletions
+103
-0
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+19
-0
tests/run/generator_expressions.pyx
tests/run/generator_expressions.pyx
+10
-0
tests/run/generator_expressions_nested.pyx
tests/run/generator_expressions_nested.pyx
+74
-0
No files found.
Cython/Compiler/Optimize.py
View file @
ae0b93d9
...
@@ -3117,6 +3117,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
...
@@ -3117,6 +3117,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
break
break
else
:
else
:
assert
condition_result
==
False
assert
condition_result
==
False
# prevent killing generators, but simplify them as much as possible
yield_expr
=
self
.
_find_genexpr_yield
(
if_clause
.
body
)
if
yield_expr
is
not
None
:
if_clause
.
condition
=
ExprNodes
.
BoolNode
(
if_clause
.
condition
.
pos
,
value
=
False
)
yield_expr
.
arg
=
ExprNodes
.
NoneNode
(
yield_expr
.
arg
.
pos
)
if_clauses
.
append
(
if_clause
)
else
:
# False clauses outside of generators can safely be deleted
pass
if
not
if_clauses
:
if
not
if_clauses
:
return
node
.
else_clause
return
node
.
else_clause
node
.
if_clauses
=
if_clauses
node
.
if_clauses
=
if_clauses
...
@@ -3131,6 +3140,16 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
...
@@ -3131,6 +3140,16 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
sequence
.
pos
,
args
=
sequence
.
args
,
mult_factor
=
sequence
.
mult_factor
)
sequence
.
pos
,
args
=
sequence
.
args
,
mult_factor
=
sequence
.
mult_factor
)
return
node
return
node
def
_find_genexpr_yield
(
self
,
node
):
body_node_types
=
(
Nodes
.
ForInStatNode
,
Nodes
.
IfStatNode
)
while
isinstance
(
node
,
body_node_types
):
node
=
node
.
body
if
isinstance
(
node
,
Nodes
.
ExprStatNode
):
node
=
node
.
expr
if
isinstance
(
node
,
ExprNodes
.
YieldExprNode
):
return
node
return
None
# in the future, other nodes can have their own handler method here
# in the future, other nodes can have their own handler method here
# that can replace them with a constant result node
# that can replace them with a constant result node
...
...
tests/run/generator_expressions.pyx
View file @
ae0b93d9
...
@@ -21,6 +21,16 @@ def genexpr_if():
...
@@ -21,6 +21,16 @@ def genexpr_if():
assert
x
==
'abc'
# don't leak
assert
x
==
'abc'
# don't leak
return
result
return
result
def
genexpr_if_false
():
"""
>>> genexpr_if_false()
[]
"""
x
=
'abc'
result
=
list
(
x
*
2
for
x
in
range
(
5
)
if
False
)
assert
x
==
'abc'
# don't leak
return
result
def
genexpr_with_lambda
():
def
genexpr_with_lambda
():
"""
"""
>>> genexpr_with_lambda()
>>> genexpr_with_lambda()
...
...
tests/run/generator_expressions_nested.pyx
0 → 100644
View file @
ae0b93d9
# mode: run
# cython: language_level=3
"""
Adapted from CPython's test_grammar.py
"""
def
genexpr_simple
():
"""
>>> sum([ x**2 for x in range(10) ])
285
>>> sum(genexpr_simple())
285
"""
return
(
x
**
2
for
x
in
range
(
10
))
def
genexpr_conditional
():
"""
>>> sum([ x*x for x in range(10) if x%2 ])
165
>>> sum(genexpr_conditional())
165
"""
return
(
x
*
x
for
x
in
range
(
10
)
if
x
%
2
)
def
genexpr_nested2
():
"""
>>> sum([x for x in range(10)])
45
>>> sum(genexpr_nested2())
45
"""
return
(
x
for
x
in
(
y
for
y
in
range
(
10
)))
def
genexpr_nested3
():
"""
>>> sum([x for x in range(10)])
45
>>> sum(genexpr_nested3())
45
"""
return
(
x
for
x
in
(
y
for
y
in
(
z
for
z
in
range
(
10
))))
def
genexpr_nested_listcomp
():
"""
>>> sum([x for x in range(10)])
45
>>> sum(genexpr_nested_listcomp())
45
"""
return
(
x
for
x
in
[
y
for
y
in
(
z
for
z
in
range
(
10
))])
def
genexpr_nested_conditional
():
"""
>>> sum([ x for x in [y for y in [z for z in range(10) if True]] if True ])
45
>>> sum(genexpr_nested_conditional())
45
"""
return
(
x
for
x
in
(
y
for
y
in
(
z
for
z
in
range
(
10
)
if
True
))
if
True
)
def
genexpr_nested2_conditional_empty
():
"""
>>> sum(genexpr_nested2_conditional_empty())
0
"""
return
(
y
for
y
in
(
z
for
z
in
range
(
10
)
if
True
)
if
False
)
def
genexpr_nested3_conditional_empty
():
"""
>>> sum(genexpr_nested3_conditional_empty())
0
"""
return
(
x
for
x
in
(
y
for
y
in
(
z
for
z
in
range
(
10
)
if
True
)
if
False
)
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