def ancestors(self, recurs=True, context=None): """return an iterator on the node base classes in a prefixed depth first order :param recurs: boolean indicating if it should recurse or return direct ancestors only """ # FIXME: should be possible to choose the resolution order # XXX inference make infinite loops possible here (see BaseTransformer # manipulation in the builder module for instance) yielded = set([self]) if context is None: context = InferenceContext() for stmt in self.bases: with context.restore_path(): try: for baseobj in stmt.infer(context): if not isinstance(baseobj, Class): # duh ? continue if baseobj in yielded: continue # cf xxx above yielded.add(baseobj) yield baseobj if recurs: for grandpa in baseobj.ancestors(True, context): if grandpa in yielded: continue # cf xxx above yielded.add(grandpa) yield grandpa except InferenceError: # XXX log error ? continue
def ancestors(self, recurs=True, context=None): yielded = set([self]) if context is None: context = InferenceContext() for stmt in self.bases: path = set(context.path) try: for baseobj in stmt.infer(context): if not isinstance(baseobj, logilab.astng.scoped_nodes.Class): # duh ? continue if baseobj in yielded: continue # cf xxx above yielded.add(baseobj) yield baseobj if recurs: for grandpa in baseobj.ancestors(True, context): if grandpa in yielded: continue # cf xxx above yielded.add(grandpa) yield grandpa except InferenceError: # XXX log error ? pass context.path = path
def test_absolute_import(self): astng = abuilder.file_build(self.datapath('absimport.py')) ctx = InferenceContext() ctx.lookupname = 'message' # will fail if absolute import failed astng['message'].infer(ctx).next() ctx.lookupname = 'email' m = astng['email'].infer(ctx).next() self.assertFalse(m.file.startswith(self.datapath('email.py')))
def ilookup(self, name): """infered lookup return an iterator on infered values of the statements returned by the lookup method """ frame, stmts = self.lookup(name) context = InferenceContext() return _infer_stmts(stmts, context, frame)
def _newstyle_impl(self, context=None): if context is None: context = InferenceContext() if self._newstyle is not None: return self._newstyle for base in self.ancestors(recurs=False, context=context): if base._newstyle_impl(context): self._newstyle = True break if self._newstyle is None: self._newstyle = False return self._newstyle
def infer_name_module(self, name): context = InferenceContext() context.lookupname = name return self.infer(context, asname=False)