Commit 346c350e authored by Kevin Modzelewski's avatar Kevin Modzelewski

Process del statements as part of definedness analysis

I'm surprised we got away so long without handling them!

Also, it looks like del statements go through full tuple unpacking --
so you can do something like
del (l[1], [c.b])
parent a4a6c7dc
......@@ -277,7 +277,6 @@ public:
virtual bool visit_assert(AST_Assert* node) { return true; }
virtual bool visit_branch(AST_Branch* node) { return true; }
virtual bool visit_delete(AST_Delete* node) { return true; }
virtual bool visit_expr(AST_Expr* node) { return true; }
virtual bool visit_global(AST_Global* node) { return true; }
virtual bool visit_invoke(AST_Invoke* node) { return false; }
......@@ -287,6 +286,21 @@ public:
virtual bool visit_raise(AST_Raise* node) { return true; }
virtual bool visit_return(AST_Return* node) { return true; }
virtual bool visit_delete(AST_Delete* node) {
for (auto t : node->targets) {
if (t->type == AST_TYPE::Name) {
AST_Name* name = ast_cast<AST_Name>(t);
state.erase(name->id);
} else {
// The CFG pass should reduce all deletes to the "basic" deletes on names/attributes/subscripts.
// If not, probably the best way to do this would be to just do a full AST traversal
// and look for AST_Name's with a ctx of Del
assert(t->type == AST_TYPE::Attribute || t->type == AST_TYPE::Subscript);
}
}
return true;
}
virtual bool visit_classdef(AST_ClassDef* node) {
_doSet(node->name);
return true;
......
# expected: fail
# - we don't support this functionality yet
def f4():
a = 1
b = 2
del [(a,), b]
if 0:
print a, b
print locals()
f4()
class C(object):
pass
def f5():
c = C()
c.a = 1
c.b = 2
c.c = 3
l = range(5)
del ([c.a], [c.b], l[3])
print hasattr(c, "a")
print hasattr(c, "b")
print hasattr(c, "c")
print l
f5()
......@@ -58,3 +58,11 @@ try:
assert 0
except NameError, e:
print e
def f3():
a = 1
del a
if 0:
pass
print locals()
f3()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment