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
Boxiang Sun
cython
Commits
d8813f05
Commit
d8813f05
authored
Dec 17, 2010
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
manual merge of Haoyu's nonlocal implementation
parent
7d1a5f54
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
105 additions
and
2 deletions
+105
-2
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+18
-0
Cython/Compiler/Parsing.pxd
Cython/Compiler/Parsing.pxd
+1
-0
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+8
-0
Cython/Compiler/Scanning.py
Cython/Compiler/Scanning.py
+1
-1
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+9
-0
Cython/Compiler/TypeInference.py
Cython/Compiler/TypeInference.py
+2
-1
tests/run/nonlocal_T490.pyx
tests/run/nonlocal_T490.pyx
+66
-0
No files found.
Cython/Compiler/Nodes.py
View file @
d8813f05
...
...
@@ -3434,6 +3434,24 @@ class GlobalNode(StatNode):
pass
class
NonlocalNode
(
StatNode
):
# Nonlocal variable declaration via the 'nonlocal' keyword.
#
# names [string]
child_attrs
=
[]
def
analyse_declarations
(
self
,
env
):
for
name
in
self
.
names
:
env
.
declare_nonlocal
(
name
,
self
.
pos
)
def
analyse_expressions
(
self
,
env
):
pass
def
generate_execution_code
(
self
,
code
):
pass
class
ExprStatNode
(
StatNode
):
# Expression used as a statement.
#
...
...
Cython/Compiler/Parsing.pxd
View file @
d8813f05
...
...
@@ -84,6 +84,7 @@ cdef p_genexp(PyrexScanner s, expr)
#-------------------------------------------------------
cdef
p_global_statement
(
PyrexScanner
s
)
cdef
p_nonlocal_statement
(
PyrexScanner
s
)
cdef
p_expression_or_assignment
(
PyrexScanner
s
)
cdef
p_print_statement
(
PyrexScanner
s
)
cdef
p_exec_statement
(
PyrexScanner
s
)
...
...
Cython/Compiler/Parsing.py
View file @
d8813f05
...
...
@@ -1047,6 +1047,12 @@ def p_global_statement(s):
names
=
p_ident_list
(
s
)
return
Nodes
.
GlobalNode
(
pos
,
names
=
names
)
def
p_nonlocal_statement
(
s
):
pos
=
s
.
position
()
s
.
next
()
names
=
p_ident_list
(
s
)
return
Nodes
.
NonlocalNode
(
pos
,
names
=
names
)
def
p_expression_or_assignment
(
s
):
expr_list
=
[
p_testlist_star_expr
(
s
)]
while
s
.
sy
==
'='
:
...
...
@@ -1600,6 +1606,8 @@ def p_simple_statement(s, first_statement = 0):
#print "p_simple_statement:", s.sy, s.systring ###
if
s
.
sy
==
'global'
:
node
=
p_global_statement
(
s
)
elif
s
.
sy
==
'nonlocal'
:
node
=
p_nonlocal_statement
(
s
)
elif
s
.
sy
==
'print'
:
node
=
p_print_statement
(
s
)
elif
s
.
sy
==
'exec'
:
...
...
Cython/Compiler/Scanning.py
View file @
d8813f05
...
...
@@ -36,7 +36,7 @@ def get_lexicon():
#------------------------------------------------------------------
py_reserved_words
=
[
"global"
,
"def"
,
"class"
,
"print"
,
"del"
,
"pass"
,
"break"
,
"global"
,
"
nonlocal"
,
"
def"
,
"class"
,
"print"
,
"del"
,
"pass"
,
"break"
,
"continue"
,
"return"
,
"raise"
,
"import"
,
"exec"
,
"try"
,
"except"
,
"finally"
,
"while"
,
"if"
,
"elif"
,
"else"
,
"for"
,
"in"
,
"assert"
,
"and"
,
"or"
,
"not"
,
"is"
,
"in"
,
"lambda"
,
...
...
Cython/Compiler/Symtab.py
View file @
d8813f05
...
...
@@ -1271,6 +1271,15 @@ class LocalScope(Scope):
else:
entry = self.global_scope().lookup_target(name)
self.entries[name] = entry
def declare_nonlocal(self, name, pos):
# Pull entry from outer scope into local scope
if self.lookup_here(name):
warning(pos, "'
%
s
' redeclared" % name, 0)
else:
entry = self.lookup(name)
if entry is None or not entry.from_closure:
error(pos, "no binding for nonlocal '
%
s
' found" % name)
def lookup(self, name):
# Look up name in this scope or an enclosing one.
...
...
Cython/Compiler/TypeInference.py
View file @
d8813f05
...
...
@@ -215,7 +215,8 @@ class SimpleAssignmentTypeInferer(object):
# TODO: Implement a real type inference algorithm.
# (Something more powerful than just extending this one...)
def
infer_types
(
self
,
scope
):
enabled
=
not
scope
.
is_closure_scope
and
scope
.
directives
[
'infer_types'
]
closure_or_inner
=
scope
.
is_closure_scope
or
(
scope
.
outer_scope
and
scope
.
outer_scope
.
is_closure_scope
)
enabled
=
not
closure_or_inner
and
scope
.
directives
[
'infer_types'
]
verbose
=
scope
.
directives
[
'infer_types.verbose'
]
if
enabled
==
True
:
spanning_type
=
aggressive_spanning_type
...
...
tests/run/nonlocal_T490.pyx
0 → 100644
View file @
d8813f05
def
simple
():
"""
>>> simple()
1
2
"""
x
=
1
y
=
2
def
f
():
nonlocal
x
nonlocal
x
,
y
print
(
x
)
print
(
y
)
f
()
def
assign
():
"""
>>> assign()
1
"""
xx
=
0
def
ff
():
nonlocal
xx
xx
+=
1
print
(
xx
)
ff
()
def
nested
():
"""
>>> nested()
1
"""
x
=
0
def
fx
():
def
gx
():
nonlocal
x
x
=
1
print
(
x
)
return
gx
fx
()()
def
arg
(
x
):
"""
>>> arg('x')
xyy
"""
def
appendy
():
nonlocal
x
x
+=
'y'
x
+=
'y'
appendy
()
print
x
return
def
argtype
(
int
n
):
"""
>>> argtype(0)
1
"""
def
inc
():
nonlocal
n
n
+=
1
inc
()
print
n
return
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