diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2014-01-26 22:10:12 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2014-01-26 22:10:12 +0200 |
commit | 92b1a3e50a82e475bd992c5b7674057160e0ce19 (patch) | |
tree | a631bcc6dd7c4f7d15d747dcaa4ebcba86097da5 /rebuilder.py | |
parent | fcd68800cbbb80049f21c01692bb02eb090e4c8e (diff) | |
parent | bb8f9f5fa4f4d442ef41825ea6e16d8549d82342 (diff) | |
download | astroid-git-92b1a3e50a82e475bd992c5b7674057160e0ce19.tar.gz |
Merge with default.
--HG--
branch : nameconstant_py34
Diffstat (limited to 'rebuilder.py')
-rw-r--r-- | rebuilder.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/rebuilder.py b/rebuilder.py index e04d186b..d4b46d33 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, +from _ast import (Expr as Discard, Str, Name, Attribute, # binary operators Add, Div, FloorDiv, Mod, Mult, Pow, Sub, BitAnd, BitOr, BitXor, LShift, RShift, @@ -87,6 +87,7 @@ REDIRECT = {'arguments': 'Arguments', 'keyword': 'Keyword', 'Repr': 'Backquote', } +PY3K = sys.version_info >= (3, 0) def _init_set_doc(node, newnode): newnode.doc = None @@ -114,7 +115,11 @@ def _set_infos(oldnode, newnode, parent): newnode.col_offset = oldnode.col_offset newnode.set_line_info(newnode.last_child()) # set_line_info accepts None - +def _infer_metaclass(node): + if isinstance(node, Name): + return node.id + elif isinstance(node, Attribute): + return node.attr class TreeRebuilder(object): @@ -245,7 +250,7 @@ class TreeRebuilder(object): continue elif getattr(newnode.targets[0], 'name', None) == '__metaclass__': # XXX check more... - self._metaclass[-1] = 'type' # XXX get the actual metaclass + self._metaclass[-1] = _infer_metaclass(node.value) newnode.set_line_info(newnode.last_child()) return newnode @@ -328,10 +333,13 @@ class TreeRebuilder(object): newnode.decorators = self.visit_decorators(node, newnode) newnode.set_line_info(newnode.last_child()) metaclass = self._metaclass.pop() - if not newnode.bases: - # no base classes, detect new / style old style according to - # current scope - newnode._newstyle = metaclass == 'type' + if PY3K: + newnode._newstyle = True + else: + if not newnode.bases: + # no base classes, detect new / style old style according to + # current scope + newnode._newstyle = metaclass in ('type', 'ABCMeta') newnode.parent.frame().set_local(newnode.name, newnode) return newnode @@ -940,6 +948,14 @@ class TreeRebuilder3k(TreeRebuilder): def visit_yieldfrom(self, node, parent): return self.visit_yield(node, parent) + def visit_class(self, node, parent): + newnode = super(TreeRebuilder3k, self).visit_class(node, parent) + for keyword in node.keywords: + if keyword.arg == 'metaclass': + newnode._metaclass = self.visit(keyword, newnode).value + break + return newnode + if sys.version_info >= (3, 0): TreeRebuilder = TreeRebuilder3k |