Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
1798ea09
Commit
1798ea09
authored
Mar 25, 2015
by
Travis Hance
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
syntax error for del'ing a variable saved in the closure
parent
f69d1a99
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
2 deletions
+78
-2
src/analysis/scoping_analysis.cpp
src/analysis/scoping_analysis.cpp
+22
-2
test/tests/del_closure_var_syntax_error.py
test/tests/del_closure_var_syntax_error.py
+56
-0
No files found.
src/analysis/scoping_analysis.cpp
View file @
1798ea09
...
...
@@ -183,6 +183,7 @@ struct ScopingAnalysis::ScopeNameUsage {
StrSet
read
;
StrSet
written
;
StrSet
forced_globals
;
std
::
vector
<
AST_Name
*>
del_name_nodes
;
// Properties determined by looking at other scopes as well:
StrSet
referenced_from_nested
;
...
...
@@ -190,7 +191,7 @@ struct ScopingAnalysis::ScopeNameUsage {
StrSet
passthrough_accesses
;
// what names a child scope accesses a name from a parent scope
// `import *` and `exec` both force the scope to use the NAME lookup
// However, this is not allowed to happen (a SyntaxError) if the scope
// However, this is not allowed to happen (a SyntaxError) if the scope
has
// "free variables", variables read but not written (and not forced to be global)
// Furthermore, no child of the scope can have any free variables either
// (not even if the variables would refer to a closure in an in-between child).
...
...
@@ -341,6 +342,8 @@ public:
cur
->
read
.
insert
(
name
);
}
void
doDel
(
AST_Name
*
node
)
{
cur
->
del_name_nodes
.
push_back
(
node
);
}
void
doImportStar
(
AST_ImportFrom
*
node
)
{
if
(
cur
->
nameForcingNodeImportStar
==
NULL
)
cur
->
nameForcingNodeImportStar
=
node
;
...
...
@@ -358,9 +361,11 @@ public:
case
AST_TYPE
:
:
Load
:
doRead
(
node
->
id
);
break
;
case
AST_TYPE
:
:
Del
:
doDel
(
node
);
// fallthrough
case
AST_TYPE
:
:
Param
:
case
AST_TYPE
:
:
Store
:
case
AST_TYPE
:
:
Del
:
doWrite
(
node
->
id
);
break
;
default:
...
...
@@ -699,6 +704,21 @@ void ScopingAnalysis::processNameUsages(ScopingAnalysis::NameUsageMap* usages) {
else
if
(
usage
->
free
)
raiseNameForcingSyntaxError
(
"is a nested function"
,
usage
);
}
// Trying to `del` a varaible in the closure in a SyntaxError.
// NOTE(travis): I'm not sure why this is a syntax error;
// it doesn't seem like there is anything intrinisically difficult about supporting
// `del` for closure variables. But it is, so, there you go:
for
(
AST_Name
*
name_node
:
usage
->
del_name_nodes
)
{
InternedString
name
=
name_node
->
id
;
if
(
usage
->
referenced_from_nested
.
count
(
name
)
>
0
)
{
char
buf
[
1024
];
snprintf
(
buf
,
sizeof
(
buf
),
"can not delete variable '%s' referenced in nested scope"
,
name
.
c_str
());
assert
(
usage
->
node
->
type
==
AST_TYPE
::
FunctionDef
);
AST_FunctionDef
*
funcNode
=
static_cast
<
AST_FunctionDef
*>
(
usage
->
node
);
raiseSyntaxError
(
buf
,
name_node
->
lineno
,
0
,
""
/* file?? */
,
funcNode
->
name
.
str
());
}
}
}
std
::
vector
<
ScopeNameUsage
*>
sorted_usages
=
sortNameUsages
(
usages
);
...
...
test/tests/del_closure_var_syntax_error.py
0 → 100644
View file @
1798ea09
cases
=
[
"""
# should fail
def f():
a = 0
def g():
print a
del a
"""
,
"""
# should fail
def f():
def g():
print a
del a
"""
,
"""
def f():
global a
a = 0
def g():
print a
del a
"""
,
"""
def f():
a = 0
def g():
global a
print a
del a
"""
,
"""
def f():
a = 0
def g():
global a
def h():
print a
del a
"""
,
"""
def f():
class C(object):
a = 0
del a
def g():
print a
"""
]
#import traceback
for
case
in
cases
:
print
case
try
:
exec
case
except
SyntaxError
as
se
:
print
se
.
message
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