diff options
author | cpopa <devnull@localhost> | 2013-08-13 13:48:51 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2013-08-13 13:48:51 +0300 |
commit | 3a1e3fa1636032cdaad5b1debdd15bf115ad2c1a (patch) | |
tree | 76cfcefd5e139f620d762f65da7e4b6d1d3276d0 | |
parent | 9b41481e469b2829cf97a9dd282930a092d5d777 (diff) | |
download | astroid-3a1e3fa1636032cdaad5b1debdd15bf115ad2c1a.tar.gz |
Change metaclass discovery.
-rw-r--r-- | rebuilder.py | 14 | ||||
-rw-r--r-- | scoped_nodes.py | 25 |
2 files changed, 19 insertions, 20 deletions
diff --git a/rebuilder.py b/rebuilder.py index e3e1fc1..862de85 100644 --- a/rebuilder.py +++ b/rebuilder.py @@ -21,7 +21,7 @@ order to get a single Astroid representation import sys from warnings import warn -from _ast import (Expr as Discard, Str, Name, Attribute, +from _ast import (Expr as Discard, Str, # binary operators Add, Div, FloorDiv, Mod, Mult, Pow, Sub, BitAnd, BitOr, BitXor, LShift, RShift, @@ -99,13 +99,6 @@ def _init_set_doc(node, newnode): except IndexError: pass # ast built from scratch -def _infer_metaclass(node): - if isinstance(node, Name): - return node.id - elif isinstance(node, Attribute): - # TODO: should we retrieve the fully qualified name of the class? - return node.attr - def _lineno_parent(oldnode, newnode, parent): newnode.parent = parent if hasattr(oldnode, 'lineno'): @@ -251,7 +244,8 @@ class TreeRebuilder(object): except (AttributeError, KeyError): continue elif getattr(newnode.targets[0], 'name', None) == '__metaclass__': - self._metaclass[-1] = _infer_metaclass(node.value) or 'type' + # XXX check more... + self._metaclass[-1] = 'type' newnode.set_line_info(newnode.last_child()) return newnode @@ -944,7 +938,7 @@ class TreeRebuilder3k(TreeRebuilder): newnode = super(TreeRebuilder3k, self).visit_class(node, parent) for keyword in node.keywords: if keyword.arg == 'metaclass': - newnode._metaclass = _infer_metaclass(keyword.value) or 'type' + newnode._metaclass = self.visit(keyword, newnode).value break return newnode diff --git a/scoped_nodes.py b/scoped_nodes.py index 3f6e55a..939fc74 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -992,18 +992,23 @@ class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin): raise InferenceError() _metaclass = None - def metaclass(self): - """ return the metaclass of this class """ + def _metaclass_search(self): + """ Return the metaclass of this class """ if self._metaclass: - return self._metaclass + # Expects this from Py3k TreeRebuilder + try: + return self._metaclass.infered()[0] + except InferenceError: + return try: - meta = Instance(self).getattr('__metaclass__')[0] + meta = self.getattr('__metaclass__')[0] except NotFoundError: return - - # XXX: We could return the actual class instance, not the string - # representing its name, but I don't know how to handle this - # with Py3's metaclass keyword.. - return meta.infered()[0].name -
\ No newline at end of file + try: + infered = meta.infered()[0] + except InferenceError: + return + if infered is YES: # don't expose this + return None + return infered |