From 4824cc633ad4bd3b6477cb621b38790fb3ff75da Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Tue, 22 Apr 2014 10:13:17 +0300 Subject: Skip the same infered nodes as the base class in _is_metaclass. Closes issue #25. --HG-- branch : ancestors --- scoped_nodes.py | 2 ++ test/regrtest_data/recursion.py | 3 +++ test/unittest_regrtest.py | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 test/regrtest_data/recursion.py diff --git a/scoped_nodes.py b/scoped_nodes.py index bce2ca16..185944b1 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -681,6 +681,8 @@ def _is_metaclass(klass): return False if baseobj is YES: continue + if baseobj is klass: + continue if baseobj._type == 'metaclass': return True if _is_metaclass(baseobj): diff --git a/test/regrtest_data/recursion.py b/test/regrtest_data/recursion.py new file mode 100644 index 00000000..a34dad32 --- /dev/null +++ b/test/regrtest_data/recursion.py @@ -0,0 +1,3 @@ +""" For issue #25 """ +class Base(object): + pass \ No newline at end of file diff --git a/test/unittest_regrtest.py b/test/unittest_regrtest.py index a1719939..d4f937ab 100644 --- a/test/unittest_regrtest.py +++ b/test/unittest_regrtest.py @@ -156,6 +156,27 @@ with open('a.txt') as stream, open('b.txt'): # context manager didn't use an assignment name. list(astroid.nodes_of_class(nodes.CallFunc))[-1].infered() + def test_recursion_regression_issue25(self): + builder = AstroidBuilder() + data = """ +import recursion as base + +_real_Base = base.Base + +class Derived(_real_Base): + pass + +def run(): + base.Base = Derived +""" + astroid = builder.string_build(data, __name__, __file__) + # Used to crash in _is_metaclass, due to wrong + # ancestors chain + classes = astroid.nodes_of_class(nodes.Class) + for klass in classes: + # triggers the _is_metaclass call + klass.type + class Whatever(object): a = property(lambda x: x, lambda x: x) -- cgit v1.2.1 From f1ed6e4d50b3e3fef31a214f534b2611fe1f141d Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Wed, 30 Apr 2014 11:37:54 +0300 Subject: Simplify the lookup for the metaclass; add comments for the YES node tests. --HG-- branch : ancestors --- scoped_nodes.py | 10 ++++------ test/unittest_python3.py | 1 + test/unittest_scoped_nodes.py | 1 + 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/scoped_nodes.py b/scoped_nodes.py index 067f01fd..20bb664f 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -1055,12 +1055,10 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin): if self._metaclass: # Expects this from Py3k TreeRebuilder try: - infered = next(self._metaclass.infer()) - except InferenceError: - return - if infered is YES: # don't expose it - return None - return infered + return next(node for node in self._metaclass.infer() + if node is not YES) + except (InferenceError, StopIteration): + return try: meta = self.getattr('__metaclass__')[0] diff --git a/test/unittest_python3.py b/test/unittest_python3.py index 9e804068..114e3d37 100644 --- a/test/unittest_python3.py +++ b/test/unittest_python3.py @@ -133,6 +133,7 @@ class Python3TC(TestCase): @require_version('3.0') def test_metaclass_yes_leak(self): astroid = self.builder.string_build(dedent(""" + # notice `ab` instead of `abc` from ab import ABCMeta class Meta(metaclass=ABCMeta): pass diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py index 3d053189..6c56c55f 100644 --- a/test/unittest_scoped_nodes.py +++ b/test/unittest_scoped_nodes.py @@ -718,6 +718,7 @@ def g2(): def test_metaclass_yes_leak(self): astroid = abuilder.string_build(dedent(""" + # notice `ab` instead of `abc` from ab import ABCMeta class Meta(object): -- cgit v1.2.1