9d4605a821bdceedae6300b9b36e104b6cece669
"""Module symbol-table generator"""
from compiler
.consts
import SC_LOCAL
, SC_GLOBAL
, SC_FREE
, SC_CELL
, SC_UNKNOWN
from compiler
.misc
import mangle
# XXX how much information do I need about each name?
def __init__(self
, name
, module
, klass
=None):
# nested is true if the class could contain free variables,
# i.e. if it is nested within another function.
for i
in range(len(klass
)):
return "<%s: %s>" % (self
.__class
__.__name
__, self
.name
)
return mangle(name
, self
.klass
)
self
.defs
[self
.mangle(name
)] = 1
self
.uses
[self
.mangle(name
)] = 1
def add_global(self
, name
):
if self
.uses
.has_key(name
) or self
.defs
.has_key(name
):
pass # XXX warn about global following def/use
if self
.params
.has_key(name
):
raise SyntaxError, "%s in %s is global and parameter" % \
self
.module
.add_def(name
)
def add_param(self
, name
):
def add_child(self
, child
):
self
.children
.append(child
)
print >> sys
.stderr
, self
.name
, self
.nested
and "nested" or ""
print >> sys
.stderr
, "\tglobals: ", self
.globals
print >> sys
.stderr
, "\tcells: ", self
.cells
print >> sys
.stderr
, "\tdefs: ", self
.defs
print >> sys
.stderr
, "\tuses: ", self
.uses
print >> sys
.stderr
, "\tfrees:", self
.frees
def check_name(self
, name
):
The scope of a name could be LOCAL, GLOBAL, FREE, or CELL.
if self
.globals.has_key(name
):
if self
.cells
.has_key(name
):
if self
.defs
.has_key(name
):
if self
.nested
and (self
.frees
.has_key(name
) or
self
.uses
.has_key(name
)):
for name
in self
.uses
.keys():
if not (self
.defs
.has_key(name
) or
self
.globals.has_key(name
)):
def handle_children(self
):
for child
in self
.children
:
frees
= child
.get_free_vars()
globals = self
.add_frees(frees
)
def force_global(self
, name
):
"""Force name to be global in scope.
Some child of the current node had a free reference to name.
When the child was processed, it was labelled a free
variable. Now that all its enclosing scope have been
processed, the name is known to be a global or builtin. So
walk back down the child chain and set the name to be global
Be careful to stop if a child does not think the name is
if self
.frees
.has_key(name
):
for child
in self
.children
:
if child
.check_name(name
) == SC_FREE
:
def add_frees(self
, names
):
"""Process list of free vars from nested scope.
Returns a list of names that are either 1) declared global in the
parent or 2) undefined in a top-level parent. In either case,
the nested scope should treat them as globals.
sc
= self
.check_name(name
)
if sc
== SC_UNKNOWN
or sc
== SC_FREE \
or isinstance(self
, ClassScope
):
child_globals
.append(name
)
elif isinstance(self
, FunctionScope
) and sc
== SC_LOCAL
:
child_globals
.append(name
)
child_globals
.append(name
)
class ModuleScope(Scope
):
__super_init
= Scope
.__init
__
self
.__super
_init
("global", self
)
class FunctionScope(Scope
):
class GenExprScope(Scope
):
__super_init
= Scope
.__init
__
def __init__(self
, module
, klass
=None):
self
.__super
_init
("generator expression<%d>"%i, module
, klass
)
self
.add_param('[outmost-iterable]')
class LambdaScope(FunctionScope
):
__super_init
= Scope
.__init
__
def __init__(self
, module
, klass
=None):
self
.__super
_init
("lambda.%d" % i
, module
, klass
)
__super_init
= Scope
.__init
__
def __init__(self
, name
, module
):
self
.__super
_init
(name
, module
, name
)
# node that define new scopes
def visitModule(self
, node
):
scope
= self
.module
= self
.scopes
[node
] = ModuleScope()
self
.visit(node
.node
, scope
)
visitExpression
= visitModule
def visitFunction(self
, node
, parent
):
self
.visit(node
.decorators
, parent
)
parent
.add_def(node
.name
)
scope
= FunctionScope(node
.name
, self
.module
, self
.klass
)
if parent
.nested
or isinstance(parent
, FunctionScope
):
self
.scopes
[node
] = scope
self
._do
_args
(scope
, node
.argnames
)
self
.visit(node
.code
, scope
)
self
.handle_free_vars(scope
, parent
)
def visitGenExpr(self
, node
, parent
):
scope
= GenExprScope(self
.module
, self
.klass
);
if parent
.nested
or isinstance(parent
, FunctionScope
) \
or isinstance(parent
, GenExprScope
):
self
.scopes
[node
] = scope
self
.visit(node
.code
, scope
)
self
.handle_free_vars(scope
, parent
)
def visitGenExprInner(self
, node
, scope
):
for genfor
in node
.quals
:
self
.visit(genfor
, scope
)
self
.visit(node
.expr
, scope
)
def visitGenExprFor(self
, node
, scope
):
self
.visit(node
.assign
, scope
, 1)
self
.visit(node
.iter, scope
)
def visitGenExprIf(self
, node
, scope
):
self
.visit(node
.test
, scope
)
def visitLambda(self
, node
, parent
, assign
=0):
# Lambda is an expression, so it could appear in an expression
# context where assign is passed. The transformer should catch
# any code that has a lambda on the left-hand side.
scope
= LambdaScope(self
.module
, self
.klass
)
if parent
.nested
or isinstance(parent
, FunctionScope
):
self
.scopes
[node
] = scope
self
._do
_args
(scope
, node
.argnames
)
self
.visit(node
.code
, scope
)
self
.handle_free_vars(scope
, parent
)
def _do_args(self
, scope
, args
):
if type(name
) == types
.TupleType
:
self
._do
_args
(scope
, name
)
def handle_free_vars(self
, scope
, parent
):
def visitClass(self
, node
, parent
):
parent
.add_def(node
.name
)
scope
= ClassScope(node
.name
, self
.module
)
if parent
.nested
or isinstance(parent
, FunctionScope
):
scope
.add_def('__module__')
self
.scopes
[node
] = scope
self
.visit(node
.code
, scope
)
self
.handle_free_vars(scope
, parent
)
# name can be a def or a use
# XXX a few calls and nodes expect a third "assign" arg that is
# true if the name is being used as an assignment. only
# expressions contained within statements may have the assign arg.
def visitName(self
, node
, scope
, assign
=0):
# operations that bind new names
def visitFor(self
, node
, scope
):
self
.visit(node
.assign
, scope
, 1)
self
.visit(node
.list, scope
)
self
.visit(node
.body
, scope
)
self
.visit(node
.else_
, scope
)
def visitFrom(self
, node
, scope
):
for name
, asname
in node
.names
:
scope
.add_def(asname
or name
)
def visitImport(self
, node
, scope
):
for name
, asname
in node
.names
:
scope
.add_def(asname
or name
)
def visitGlobal(self
, node
, scope
):
def visitAssign(self
, node
, scope
):
"""Propagate assignment flag down to child nodes.
The Assign node doesn't itself contains the variables being
assigned to. Instead, the children in node.nodes are visited
with the assign flag set to true. When the names occur in
those nodes, they are marked as defs.
Some names that occur in an assignment target are not bound by
the assignment, e.g. a name occurring inside a slice. The
visitor handles these nodes specially; they do not propagate
the assign flag to their children.
self
.visit(node
.expr
, scope
)
def visitAssName(self
, node
, scope
, assign
=1):
def visitAssAttr(self
, node
, scope
, assign
=0):
self
.visit(node
.expr
, scope
, 0)
def visitSubscript(self
, node
, scope
, assign
=0):
self
.visit(node
.expr
, scope
, 0)
def visitSlice(self
, node
, scope
, assign
=0):
self
.visit(node
.expr
, scope
, 0)
self
.visit(node
.lower
, scope
, 0)
self
.visit(node
.upper
, scope
, 0)
def visitAugAssign(self
, node
, scope
):
# If the LHS is a name, then this counts as assignment.
# Otherwise, it's just use.
self
.visit(node
.node
, scope
)
if isinstance(node
.node
, ast
.Name
):
self
.visit(node
.node
, scope
, 1) # XXX worry about this
self
.visit(node
.expr
, scope
)
# prune if statements if tests are false
_const_types
= types
.StringType
, types
.IntType
, types
.FloatType
def visitIf(self
, node
, scope
):
for test
, body
in node
.tests
:
if isinstance(test
, ast
.Const
):
if type(test
.value
) in self
._const
_types
:
self
.visit(node
.else_
, scope
)
# a yield statement signals a generator
def visitYield(self
, node
, scope
):
self
.visit(node
.value
, scope
)
return sort(l1
) == sort(l2
)
if __name__
== "__main__":
from compiler
import parseFile
, walk
return [s
for s
in [s
.get_name() for s
in syms
.get_symbols()]
if not (s
.startswith('_[') or s
.startswith('.'))]
for file in sys
.argv
[1:]:
syms
= symtable
.symtable(buf
, file, "exec")
mod_names
= get_names(syms
)
# compare module-level symbols
names2
= s
.scopes
[tree
].get_names()
if not list_eq(mod_names
, names2
):
for s
in syms
.get_symbols():
if sc
.name
== s
.get_name()]
print "skipping", s
.get_name()
if not list_eq(get_names(s
.get_namespace()),
print sort(get_names(s
.get_namespace()))
print sort(l
[0].get_names())