def getattr(self, name, context=None): """this method doesn't look in the instance_attrs dictionary since it's done by an Instance proxy at inference time. It may return a YES object if the attribute has not been actually found but a __getattr__ or __getattribute__ method is defined """ values = self.locals.get(name, []) if name in self.special_attributes: if name == '__module__': return [cf(self.root().qname())] + values if name == '__bases__': return [ cf(tuple(self.ancestors(recurs=False, context=context))) ] + values # XXX need proper meta class handling + MRO implementation if name == '__mro__' and self.newstyle: # XXX mro is read-only but that's not our job to detect that return [ cf(tuple(self.ancestors(recurs=True, context=context))) ] + values return std_special_attributes(self, name) # don't modify the list in self.locals! values = list(values) for classnode in self.ancestors(recurs=False, context=context): try: values += classnode.getattr(name, context) except NotFoundError: continue if not values: raise NotFoundError(name) return values
def real_name(self, asname): """get name from 'as' name""" for index in range(len(self.names)): name, _asname = self.names[index] if name == '*': return asname if not _asname: name = name.split('.', 1)[0] _asname = name if asname == _asname: return name raise NotFoundError(asname)
def std_special_attributes(self, name, add_locals=True): if add_locals: locals = self.locals else: locals = {} if name == '__name__': return [cf(self.name)] + locals.get(name, []) if name == '__doc__': return [cf(self.doc)] + locals.get(name, []) if name == '__dict__': return [Dict()] + locals.get(name, []) raise NotFoundError(name)
def instance_attr(self, name, context=None): """return the astng nodes associated to name in this class instance attributes dictionary and in its parents :raises `NotFoundError`: if no attribute with this name has been find in this class or its parent classes """ values = self.instance_attrs.get(name, []) # get if from the first parent implementing it if any for class_node in self.instance_attr_ancestors(name, context): values += class_node.instance_attrs[name] if not values: raise NotFoundError(name) return values
def local_attr(self, name, context=None): """return the list of assign node associated to name in this class locals or in its parents :raises `NotFoundError`: if no attribute with this name has been find in this class or its parent classes """ try: return self.locals[name] except KeyError: # get if from the first parent implementing it if any for class_node in self.local_attr_ancestors(name, context): return class_node.locals[name] raise NotFoundError(name)
def getattr(self, name, context=None): if not name in self.special_attributes: try: return self.locals[name] except KeyError: pass else: if name == '__file__': return [cf(self.file)] + self.locals.get(name, []) if name == '__path__': if self.package: return [List()] + self.locals.get(name, []) return std_special_attributes(self, name) if self.package: try: return [self.import_module(name, relative_only=True)] except (KeyboardInterrupt, SystemExit): raise except: pass raise NotFoundError(name)
def wrapper(*args, **kwargs): nodes = [n for n in func(*args, **kwargs) if not isinstance(n, cls)] if not nodes: raise NotFoundError() return nodes