Skip to content

Commit

Permalink
Revert "Remove the restore_path() method"
Browse files Browse the repository at this point in the history
This reverts commit 8151853 which was added while investigating
the slowdown caused by context's path copying. Turns out this change doesn't have a negative
impact on the performance but it has a positive one on the inference.
  • Loading branch information
PCManticore committed Jan 17, 2019
1 parent 7fa547b commit 0d4f73d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
8 changes: 7 additions & 1 deletion astroid/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER

"""Various context related utilities, including inference and call contexts."""

import contextlib
import pprint
from typing import Optional

Expand Down Expand Up @@ -116,6 +116,12 @@ def cache_generator(self, key, generator):

self.inferred[key] = tuple(results)

@contextlib.contextmanager
def restore_path(self):
path = set(self.path)
yield
self.path = path

def __str__(self):
state = (
"%s=%s"
Expand Down
47 changes: 24 additions & 23 deletions astroid/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2190,30 +2190,31 @@ def ancestors(self, recurs=True, context=None):
return

for stmt in self.bases:
try:
for baseobj in stmt.infer(context):
if not isinstance(baseobj, ClassDef):
if isinstance(baseobj, bases.Instance):
baseobj = baseobj._proxied
else:
continue
if not baseobj.hide:
if baseobj in yielded:
continue
yielded.add(baseobj)
yield baseobj
if not recurs:
continue
for grandpa in baseobj.ancestors(recurs=True, context=context):
if grandpa is self:
# This class is the ancestor of itself.
break
if grandpa in yielded:
with context.restore_path():
try:
for baseobj in stmt.infer(context):
if not isinstance(baseobj, ClassDef):
if isinstance(baseobj, bases.Instance):
baseobj = baseobj._proxied
else:
continue
if not baseobj.hide:
if baseobj in yielded:
continue
yielded.add(baseobj)
yield baseobj
if not recurs:
continue
yielded.add(grandpa)
yield grandpa
except exceptions.InferenceError:
continue
for grandpa in baseobj.ancestors(recurs=True, context=context):
if grandpa is self:
# This class is the ancestor of itself.
break
if grandpa in yielded:
continue
yielded.add(grandpa)
yield grandpa
except exceptions.InferenceError:
continue

def local_attr_ancestors(self, name, context=None):
"""Iterate over the parents that define the given name.
Expand Down

0 comments on commit 0d4f73d

Please sign in to comment.