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
Kirill Smelkov
cython
Commits
e4a910d0
Commit
e4a910d0
authored
Apr 21, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support for lambda inside of generator expressions
parent
0c813e12
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
2 deletions
+58
-2
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+7
-2
tests/run/cython3.pyx
tests/run/cython3.pyx
+12
-0
tests/run/generator_expressions.pyx
tests/run/generator_expressions.pyx
+39
-0
No files found.
Cython/Compiler/Symtab.py
View file @
e4a910d0
...
...
@@ -558,8 +558,7 @@ class Scope(object):
def declare_lambda_function(self, func_cname, pos):
# Add an entry for an anonymous Python function.
entry = self.declare_var(None, py_object_type, pos,
cname=func_cname, visibility='private')
entry = self.declare(None, func_cname, py_object_type, pos, 'private')
entry.name = EncodedString(func_cname)
entry.func_cname = func_cname
entry.signature = pyfunction_signature
...
...
@@ -1401,6 +1400,12 @@ class GeneratorExpressionScope(Scope):
self
.
entries
[
name
]
=
entry
return
entry
def
declare_lambda_function
(
self
,
func_cname
,
pos
):
return
self
.
outer_scope
.
declare_lambda_function
(
func_cname
,
pos
)
def
add_lambda_def
(
self
,
def_node
):
return
self
.
outer_scope
.
add_lambda_def
(
def_node
)
class
ClosureScope
(
LocalScope
):
...
...
tests/run/cython3.pyx
View file @
e4a910d0
# cython: language_level=3
# mode: run
# tag: generators, python3
cimport
cython
...
...
@@ -89,6 +91,16 @@ def list_comp():
assert
x
==
'abc'
# don't leak in Py3 code
return
result
def
list_comp_with_lambda
():
"""
>>> list_comp_with_lambda()
[0, 4, 8]
"""
x
=
'abc'
result
=
[
x
*
2
for
x
in
range
(
5
)
if
(
lambda
x
:
x
%
2
)(
x
)
==
0
]
assert
x
==
'abc'
# don't leak in Py3 code
return
result
module_level_lc
=
[
module_level_loopvar
*
2
for
module_level_loopvar
in
range
(
4
)
]
def
list_comp_module_level
():
"""
...
...
tests/run/generator_expressions.pyx
0 → 100644
View file @
e4a910d0
# mode: run
# tag: generators, lambda
def
genexpr
():
"""
>>> genexpr()
[0, 2, 4, 6, 8]
"""
x
=
'abc'
result
=
list
(
x
*
2
for
x
in
range
(
5
)
)
assert
x
==
'abc'
# don't leak
return
result
def
genexpr_if
():
"""
>>> genexpr_if()
[0, 4, 8]
"""
x
=
'abc'
result
=
list
(
x
*
2
for
x
in
range
(
5
)
if
x
%
2
==
0
)
assert
x
==
'abc'
# don't leak
return
result
def
genexpr_with_lambda
():
"""
>>> genexpr_with_lambda()
[0, 4, 8]
"""
x
=
'abc'
result
=
list
(
x
*
2
for
x
in
range
(
5
)
if
(
lambda
x
:
x
%
2
)(
x
)
==
0
)
assert
x
==
'abc'
# don't leak
return
result
def
genexpr_of_lambdas
(
int
N
):
"""
>>> [ (f(), g()) for f,g in genexpr_of_lambdas(5) ]
[(0, 0), (1, 2), (2, 4), (3, 6), (4, 8)]
"""
return
(
((
lambda
:
x
),
(
lambda
:
x
*
2
))
for
x
in
range
(
N
)
)
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