summaryrefslogtreecommitdiff
path: root/astroid/rebuilder.py
diff options
context:
space:
mode:
Diffstat (limited to 'astroid/rebuilder.py')
-rw-r--r--astroid/rebuilder.py86
1 files changed, 37 insertions, 49 deletions
diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py
index 8b2ef48f..6399ef01 100644
--- a/astroid/rebuilder.py
+++ b/astroid/rebuilder.py
@@ -18,62 +18,51 @@
"""this module contains utilities for rebuilding a _ast tree in
order to get a single Astroid representation
"""
-from astroid.as_string import dump
+import _ast
import sys
-from _ast import (
- Expr, Str,
- # binary operators
- Add, BinOp, Div, FloorDiv, Mod, Mult, Pow, Sub, BitAnd, BitOr, BitXor,
- LShift, RShift,
- # logical operators
- And, Or,
- # unary operators
- UAdd, USub, Not, Invert,
- # comparison operators
- Eq, Gt, GtE, In, Is, IsNot, Lt, LtE, NotEq, NotIn,
- )
-from astroid import nodes
from astroid import astpeephole
+from astroid import nodes
+
+
-_BIN_OP_CLASSES = {Add: '+',
- BitAnd: '&',
- BitOr: '|',
- BitXor: '^',
- Div: '/',
- FloorDiv: '//',
- Mod: '%',
- Mult: '*',
- Pow: '**',
- Sub: '-',
- LShift: '<<',
- RShift: '>>',
+_BIN_OP_CLASSES = {_ast.Add: '+',
+ _ast.BitAnd: '&',
+ _ast.BitOr: '|',
+ _ast.BitXor: '^',
+ _ast.Div: '/',
+ _ast.FloorDiv: '//',
+ _ast.Mod: '%',
+ _ast.Mult: '*',
+ _ast.Pow: '**',
+ _ast.Sub: '-',
+ _ast.LShift: '<<',
+ _ast.RShift: '>>',
}
if sys.version_info >= (3, 5):
- from _ast import MatMult
- _BIN_OP_CLASSES[MatMult] = '@'
+ _BIN_OP_CLASSES[_ast.MatMult] = '@'
-_BOOL_OP_CLASSES = {And: 'and',
- Or: 'or',
+_BOOL_OP_CLASSES = {_ast.And: 'and',
+ _ast.Or: 'or',
}
-_UNARY_OP_CLASSES = {UAdd: '+',
- USub: '-',
- Not: 'not',
- Invert: '~',
+_UNARY_OP_CLASSES = {_ast.UAdd: '+',
+ _ast.USub: '-',
+ _ast.Not: 'not',
+ _ast.Invert: '~',
}
-_CMP_OP_CLASSES = {Eq: '==',
- Gt: '>',
- GtE: '>=',
- In: 'in',
- Is: 'is',
- IsNot: 'is not',
- Lt: '<',
- LtE: '<=',
- NotEq: '!=',
- NotIn: 'not in',
+_CMP_OP_CLASSES = {_ast.Eq: '==',
+ _ast.Gt: '>',
+ _ast.GtE: '>=',
+ _ast.In: 'in',
+ _ast.Is: 'is',
+ _ast.IsNot: 'is not',
+ _ast.Lt: '<',
+ _ast.LtE: '<=',
+ _ast.NotEq: '!=',
+ _ast.NotIn: 'not in',
}
CONST_NAME_TRANSFORMS = {'None': None,
@@ -93,7 +82,7 @@ PY34 = sys.version_info >= (3, 4)
def _get_doc(node):
try:
- if isinstance(node.body[0], Expr) and isinstance(node.body[0].value, Str):
+ if isinstance(node.body[0], _ast.Expr) and isinstance(node.body[0].value, _ast.Str):
doc = node.body[0].value.s
node.body = node.body[1:]
return node, doc
@@ -111,7 +100,6 @@ class TreeRebuilder(object):
self._import_from_nodes = []
self._delayed_assattr = []
self._visit_meths = {}
- self._transform = manager.transform
self._peepholer = astpeephole.ASTPeepholeOptimizer()
def visit_module(self, node, modname, modpath, package):
@@ -131,7 +119,7 @@ class TreeRebuilder(object):
visit_name = 'visit_' + REDIRECT.get(cls_name, cls_name).lower()
visit_method = getattr(self, visit_name)
self._visit_meths[cls] = visit_method
- return self._transform(visit_method(node, parent, assign_ctx))
+ return visit_method(node, parent, assign_ctx)
def _save_assignment(self, node, name=None):
"""save assignement situation since node.parent is not available yet"""
@@ -259,7 +247,7 @@ class TreeRebuilder(object):
def visit_binop(self, node, parent, assign_ctx=None):
"""visit a BinOp node by returning a fresh instance of it"""
- if isinstance(node.left, BinOp) and self._manager.optimize_ast:
+ if isinstance(node.left, _ast.BinOp) and self._manager.optimize_ast:
# Optimize BinOp operations in order to remove
# redundant recursion. For instance, if the
# following code is parsed in order to obtain
@@ -272,7 +260,7 @@ class TreeRebuilder(object):
# problem for the correctness of the program).
#
# ("a" + "b" + # one thousand more + "c")
- newnode = self._peepholer.optimize_binop(node, parent)
+ newnode = self._peepholer.optimize_binop(node)
if newnode:
return newnode