diff options
Diffstat (limited to 'lookup.py')
-rw-r--r-- | lookup.py | 139 |
1 files changed, 2 insertions, 137 deletions
@@ -33,8 +33,8 @@ import __builtin__ from logilab.astng import MANAGER, NotFoundError from logilab.astng import nodes # XXX this will not work ?? circular import from logilab.astng._nodes import BaseClass, NodeNG -from logilab.astng.infutils import are_exclusive, copy_context, _infer_stmts - +from logilab.astng.infutils import copy_context, _infer_stmts +from logilab.astng.node_classes import are_exclusive class LookupMixIn(BaseClass): """Mixin looking up a name in the right scope @@ -175,141 +175,6 @@ class LookupMixIn(BaseClass): _stmt_parents.append(stmt.parent) return _stmts - -def builtin_lookup(name): - """lookup a name into the builtin module - return the list of matching statements and the astng for the builtin - module - """ - builtinastng = MANAGER.astng_from_module(__builtin__) - if name == '__dict__': - return builtinastng, () - try: - stmts = builtinastng.locals[name] - except KeyError: - stmts = () - return builtinastng, stmts - -class LocalsDictNodeNG(LookupMixIn, NodeNG): - """ this class provides locals handling common to Module, Function - and Class nodes, including a dict like interface for direct access - to locals information - """ - - # attributes below are set by the builder module or by raw factories - - # dictionary of locals with name as key and node defining the local as - # value - locals = None - - def qname(self): - """return the 'qualified' name of the node, eg module.name, - module.class.name ... - """ - if self.parent is None: - return self.name - return '%s.%s' % (self.parent.frame().qname(), self.name) - - def frame(self): - """return the first parent frame node (i.e. Module, Function or Class) - """ - return self - - def scope(self): - """return the first node defining a new scope (i.e. Module, - Function, Class, Lambda but also GenExpr) - """ - return self - - - def _scope_lookup(self, node, name, offset=0): - """XXX method for interfacing the scope lookup""" - try: - stmts = node._filter_stmts(self.locals[name], self, offset) - except KeyError: - stmts = () - if stmts: - return self, stmts - if self.parent: # i.e. not Module - # nested scope: if parent scope is a function, that's fine - # else jump to the module - pscope = self.parent.scope() - if not isinstance(pscope, nodes.Function): - pscope = pscope.root() - return pscope.scope_lookup(node, name) - return builtin_lookup(name) # Module - - - - def set_local(self, name, stmt): - """define <name> in locals (<stmt> is the node defining the name) - if the node is a Module node (i.e. has globals), add the name to - globals - - if the name is already defined, ignore it - """ - assert self.locals is not None, (self, id(self)) - #assert not stmt in self.locals.get(name, ()), (self, stmt) - self.locals.setdefault(name, []).append(stmt) - - __setitem__ = set_local - - def _append_node(self, child): - """append a child, linking it in the tree""" - self.body.append(child) - child.parent = self - - def add_local_node(self, child_node, name=None): - """append a child which should alter locals to the given node""" - if name != '__class__': - # add __class__ node as a child will cause infinite recursion later! - self._append_node(child_node) - self.set_local(name or child_node.name, child_node) - - - def __getitem__(self, item): - """method from the `dict` interface returning the first node - associated with the given name in the locals dictionary - - :type item: str - :param item: the name of the locally defined object - :raises KeyError: if the name is not defined - """ - return self.locals[item][0] - - def __iter__(self): - """method from the `dict` interface returning an iterator on - `self.keys()` - """ - return iter(self.keys()) - - def keys(self): - """method from the `dict` interface returning a tuple containing - locally defined names - """ - return self.locals.keys() - - def values(self): - """method from the `dict` interface returning a tuple containing - locally defined nodes which are instance of `Function` or `Class` - """ - return [self[key] for key in self.keys()] - - def items(self): - """method from the `dict` interface returning a list of tuple - containing each locally defined name with its associated node, - which is an instance of `Function` or `Class` - """ - return zip(self.keys(), self.values()) - - def has_key(self, name): - """method from the `dict` interface returning True if the given - name is defined in the locals dictionary - """ - return self.locals.has_key(name) - - __contains__ = has_key - # maybe import at the end ? from logilab.astng import nodes |