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
0a4d4766
Commit
0a4d4766
authored
Dec 31, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix with-statement when context manager comes from an inlined def-function call
parent
9953cc9a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
2 deletions
+58
-2
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+3
-2
Cython/Compiler/Visitor.py
Cython/Compiler/Visitor.py
+36
-0
tests/run/closure_inlining.pyx
tests/run/closure_inlining.pyx
+19
-0
No files found.
Cython/Compiler/Optimize.py
View file @
0a4d4766
...
...
@@ -1668,7 +1668,8 @@ class EarlyReplaceBuiltinCalls(Visitor.EnvTransform):
return
node
return
kwargs
class
InlineDefNodeCalls
(
Visitor
.
EnvTransform
):
class
InlineDefNodeCalls
(
Visitor
.
NodeRefCleanupMixin
,
Visitor
.
EnvTransform
):
visit_Node
=
Visitor
.
VisitorTransform
.
recurse_to_children
def
get_constant_value_node
(
self
,
name_node
):
...
...
@@ -1696,7 +1697,7 @@ class InlineDefNodeCalls(Visitor.EnvTransform):
node
.
pos
,
function_name
=
function_name
,
function
=
function
,
args
=
node
.
args
)
if
inlined
.
can_be_inlined
():
return
inlined
return
self
.
replace
(
node
,
inlined
)
return
node
...
...
Cython/Compiler/Visitor.py
View file @
0a4d4766
...
...
@@ -374,6 +374,42 @@ class EnvTransform(CythonTransform):
return
node
class
NodeRefCleanupMixin
(
object
):
"""
Clean up references to nodes that were replaced.
NOTE: this implementation assumes that the replacement is
done first, before hitting any further references during
normal tree traversal. This needs to be arranged by calling
"self.visitchildren()" at a proper place in the transform
and by ordering the "child_attrs" of nodes appropriately.
"""
def
__init__
(
self
,
*
args
):
super
(
NodeRefCleanupMixin
,
self
).
__init__
(
*
args
)
self
.
_replacements
=
{}
def
visit_CloneNode
(
self
,
node
):
arg
=
node
.
arg
if
arg
not
in
self
.
_replacements
:
self
.
visitchildren
(
node
)
arg
=
node
.
arg
node
.
arg
=
self
.
_replacements
.
get
(
arg
,
arg
)
return
node
def
visit_ResultRefNode
(
self
,
node
):
expr
=
node
.
expression
if
expr
is
None
or
expr
not
in
self
.
_replacements
:
self
.
visitchildren
(
node
)
expr
=
node
.
expression
if
expr
is
not
None
:
node
.
expression
=
self
.
_replacements
.
get
(
expr
,
expr
)
return
node
def
replace
(
self
,
node
,
replacement
):
self
.
_replacements
[
node
]
=
replacement
return
replacement
class
MethodDispatcherTransform
(
EnvTransform
):
"""
Base class for transformations that want to intercept on specific
...
...
tests/run/closure_inlining.pyx
View file @
0a4d4766
...
...
@@ -128,3 +128,22 @@ def test_redef(redefine):
else
:
assert
inner
!=
inner2
return
inner
()
def
test_with_statement
():
"""
>>> test_with_statement()
enter
running
exit
"""
def
make_context_manager
():
class
CM
(
object
):
def
__enter__
(
self
):
print
"enter"
def
__exit__
(
self
,
*
args
):
print
"exit"
return
CM
()
with
make_context_manager
():
print
"running"
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