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
c400bbd5
Commit
c400bbd5
authored
Jul 06, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
do not let set/dict comprehensions leak in Py2, only list comprehensions
parent
be1cfc62
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
56 additions
and
8 deletions
+56
-8
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+4
-2
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+3
-1
tests/run/cython3.pyx
tests/run/cython3.pyx
+38
-0
tests/run/dictcomp.pyx
tests/run/dictcomp.pyx
+1
-1
tests/run/setcomp.pyx
tests/run/setcomp.pyx
+10
-4
No files found.
Cython/Compiler/ExprNodes.py
View file @
c400bbd5
...
...
@@ -3940,8 +3940,10 @@ class ComprehensionNode(ScopedExprNode):
subexprs
=
[
"target"
]
child_attrs
=
[
"loop"
,
"append"
]
# different behaviour in Py2 and Py3: leak loop variables or not?
has_local_scope
=
False
# Py2 behaviour as default
# leak loop variables or not? non-leaking Py3 behaviour is
# default, except for list comprehensions where the behaviour
# differs in Py2 and Py3 (see Parsing.py)
has_local_scope
=
True
def
infer_type
(
self
,
env
):
return
self
.
target
.
infer_type
(
env
)
...
...
Cython/Compiler/Parsing.py
View file @
c400bbd5
...
...
@@ -780,7 +780,9 @@ def p_list_maker(s):
loop
=
p_comp_for
(
s
,
append
)
s
.
expect
(
']'
)
return
ExprNodes
.
ComprehensionNode
(
pos
,
loop
=
loop
,
append
=
append
,
target
=
target
)
pos
,
loop
=
loop
,
append
=
append
,
target
=
target
,
# list comprehensions leak their loop variable in Py2
has_local_scope
=
s
.
context
.
language_level
>
2
)
else
:
if
s
.
sy
==
','
:
s
.
next
()
...
...
tests/run/cython3.pyx
View file @
c400bbd5
# cython: language_level=3
try
:
sorted
except
NameError
:
def
sorted
(
seq
):
seq
=
list
(
seq
)
seq
.
sort
()
return
seq
def
print_function
(
*
args
):
"""
>>> print_function(1,2,3)
...
...
@@ -17,3 +25,33 @@ def unicode_literals():
"""
print
(
isinstance
(
ustring
,
unicode
)
or
type
(
ustring
))
return
ustring
def
list_comp
():
"""
>>> list_comp()
[0, 4, 8]
"""
x
=
'abc'
result
=
[
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
]
assert
x
==
'abc'
# don't leak in Py3 code
return
result
def
set_comp
():
"""
>>> sorted(set_comp())
[0, 4, 8]
"""
x
=
'abc'
result
=
{
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
}
assert
x
==
'abc'
# don't leak
return
result
def
dict_comp
():
"""
>>> sorted(dict_comp().items())
[(0, 0), (2, 4), (4, 8)]
"""
x
=
'abc'
result
=
{
x
:
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
}
assert
x
==
'abc'
# don't leak
return
result
tests/run/dictcomp.pyx
View file @
c400bbd5
...
...
@@ -12,7 +12,7 @@ def dictcomp():
result
=
{
x
+
2
:
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
}
assert
x
!=
'abc'
assert
x
==
'abc'
# do not leak!
return
result
@
cython
.
test_fail_if_path_exists
(
...
...
tests/run/setcomp.pyx
View file @
c400bbd5
...
...
@@ -13,9 +13,12 @@ def setcomp():
>>> sorted(setcomp())
[0, 4, 8]
"""
return
{
x
*
2
x
=
'abc'
result
=
{
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
}
assert
x
==
'abc'
# do not leak
return
result
@
cython
.
test_fail_if_path_exists
(
"//GeneratorExpressionNode"
,
...
...
@@ -30,9 +33,12 @@ def genexp_set():
>>> sorted(genexp_set())
[0, 4, 8]
"""
return
set
(
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
)
x
=
'abc'
result
=
set
(
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
)
assert
x
==
'abc'
# do not leak
return
result
cdef
class
A
:
def
__repr__
(
self
):
return
u"A"
...
...
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