Commits

Anonymous committed b0680ff

Using _ast in rope.base and rope.ide

Comments (0)

Files changed (12)

docs/dev/workingon.txt

 Using `_ast` instead of `compiler
 =================================
 
-- Using `_ast` in `patchedast`
-- Using `_ast` in `similarfinder`
-
+* Handle tuple parameters
 * Handling strings in following lines in `patchedast`
 * Using `PyModule.ast` in `similarfinder`
 
 
 
 def walk(node, walker):
+    """Walk the syntax tree"""
     method_name = '_' + node.__class__.__name__
     method = getattr(walker, method_name, None)
     if method is not None:
 
 
 def get_child_nodes(node):
+    if isinstance(node, _ast.Module):
+        return node.body
     result = []
     if node._fields is not None:
         for name in node._fields:

rope/base/builtins.py

 
     def get_returned_object(self, args):
         result = evaluate.get_statement_result(
-            self.scope, self.node.code)
+            self.scope, self.node.body)
         if result is not None:
             return result.get_object()
         else:

rope/base/codeanalyze.py

-import compiler
 import re
 import tokenize
 import token
 import rope.base.pynames
 from rope.base import pyobjects, pynames
 import rope.base.exceptions
-from rope.base import builtins
-from rope.base import evaluate
+from rope.base import ast, builtins, evaluate
 
 
 class WordRangeFinder(object):
     # XXX: This might belong to `rope.base.evaluate` module
     @staticmethod
     def get_primary_and_pyname_in_scope(holding_scope, name):
-        #ast = compiler.parse(name)
         try:
             # parenthesizing for handling cases like 'a_var.\nattr'
-            ast = compiler.parse('(%s)' % name)
+            node = ast.parse('(%s)' % name)
         except SyntaxError:
             raise BadIdentifierError('Not a python identifier selected.')
-        return evaluate.get_primary_and_result(holding_scope, ast)
+        return evaluate.get_primary_and_result(holding_scope, node)
 
 
 class BadIdentifierError(rope.base.exceptions.RopeError):

rope/base/evaluate.py

-import compiler
-
 import rope.base.pyobjects
 import rope.base.pynames
-from rope.base import exceptions
+from rope.base import ast, exceptions
 
 
 class StatementEvaluator(object):
         self.result = None
         self.old_result = None
 
-    def visitName(self, node):
-        self.result = self.scope.lookup(node.name)
+    def _Name(self, node):
+        self.result = self.scope.lookup(node.id)
 
-    def visitGetattr(self, node):
-        pyname = get_statement_result(self.scope, node.expr)
+    def _Attribute(self, node):
+        pyname = get_statement_result(self.scope, node.value)
         if pyname is None:
             pyname = rope.base.pynames.UnboundName()
         self.old_result = pyname
         if pyname.get_object() != rope.base.pyobjects.get_unknown():
             try:
-                self.result = pyname.get_object().get_attribute(node.attrname)
+                self.result = pyname.get_object().get_attribute(node.attr)
             except exceptions.AttributeNotFoundError:
                 self.result = None
 
-    def visitCallFunc(self, node):
-        primary, pyobject = self._get_primary_and_object_for_node(node.node)
+    def _Call(self, node):
+        primary, pyobject = self._get_primary_and_object_for_node(node.func)
         if pyobject is None:
             return
         def _get_returned(pyobject):
             self.result = rope.base.pynames.UnboundName(
                 pyobject=_get_returned(pyfunction))
 
-    def visitConst(self, node):
-        if isinstance(node.value, (str, unicode)):
-            self.result = rope.base.pynames.UnboundName(
-                pyobject=rope.base.builtins.get_str())
+    def _Str(self, node):
+        self.result = rope.base.pynames.UnboundName(
+            pyobject=rope.base.builtins.get_str())
 
-    def visitAdd(self, node):
+    def _Num(self, node):
         pass
 
-    def visitAnd(self, node):
+    def _BinOp(self, node):
         pass
 
-    def visitBackquote(self, node):
+    def _BoolOp(self, node):
         pass
 
-    def visitBitand(self, node):
+    def _Repr(self, node):
         pass
 
-    def visitBitor(self, node):
+    def _UnaryOp(self, node):
         pass
 
-    def visitXor(self, node):
+    def _Compare(self, node):
         pass
 
-    def visitCompare(self, node):
-        pass
-
-    def visitDict(self, node):
+    def _Dict(self, node):
         keys = None
         values = None
-        if node.items:
-            item = node.items[0]
-            keys = self._get_object_for_node(item[0])
-            values = self._get_object_for_node(item[1])
+        if node.keys:
+            keys = self._get_object_for_node(node.keys[0])
+            values = self._get_object_for_node(node.values[0])
         self.result = rope.base.pynames.UnboundName(
             pyobject=rope.base.builtins.get_dict(keys, values))
 
-    def visitFloorDiv(self, node):
-        pass
-
-    def visitList(self, node):
+    def _List(self, node):
         holding = None
-        if node.nodes:
-            holding = self._get_object_for_node(node.nodes[0])
+        if node.elts:
+            holding = self._get_object_for_node(node.elts[0])
         self.result = rope.base.pynames.UnboundName(
             pyobject=rope.base.builtins.get_list(holding))
 
-    def visitListComp(self, node):
+    def _ListComp(self, node):
         self.result = rope.base.pynames.UnboundName(
             pyobject=rope.base.builtins.get_list())
 
-    def visitGenExpr(self, node):
+    def _GeneratorExp(self, node):
         self.result = rope.base.pynames.UnboundName(
             pyobject=rope.base.builtins.get_iterator())
 
-    def visitMul(self, node):
-        pass
-
-    def visitNot(self, node):
-        pass
-
-    def visitOr(self, node):
-        pass
-
-    def visitPower(self, node):
-        pass
-
-    def visitRightShift(self, node):
-        pass
-
-    def visitLeftShift(self, node):
-        pass
-
-    def visitSlice(self, node):
-        self._call_function(node.expr, '__getslice__')
-
-    def visitSliceobj(self, node):
-        pass
-
-    def visitTuple(self, node):
+    def _Tuple(self, node):
         objects = []
-        if len(node.nodes) < 4:
-            for stmt in node.nodes:
+        if len(node.elts) < 4:
+            for stmt in node.elts:
                 pyobject = self._get_object_for_node(stmt)
                 objects.append(pyobject)
         else:
             pyobject = pyname.get_object()
         return primary, pyobject
 
-    def visitSubscript(self, node):
-        self._call_function(node.expr, '__getitem__', node.subs)
+    def _Subscript(self, node):
+        if isinstance(node.slice, ast.Index):
+            self._call_function(node.value, '__getitem__',
+                                [node.slice.value])
+        elif isinstance(node.slice, ast.Slice):
+            self._call_function(node.value, '__getslice__')
 
     def _call_function(self, node, function_name, other_args=None):
         pyname = get_statement_result(self.scope, node)
             self.result = rope.base.pynames.UnboundName(
                 pyobject=call_function.get_returned_object(arguments))
 
-    def visitLambda(self, node):
+    def _Lambda(self, node):
         self.result = rope.base.pynames.UnboundName(
             pyobject=rope.base.builtins.Lambda(node, self.scope))
 
 
 def get_statement_result(scope, node):
-    """Evaluate a `compiler.ast` node and return a PyName
+    """Evaluate a `ast.AST` node and return a PyName
 
     Returns `None` if the expression cannot be evaluated.
 
 
 def get_primary_and_result(scope, node):
     evaluator = StatementEvaluator(scope)
-    compiler.walk(node, evaluator)
+    ast.walk(node, evaluator)
     return evaluator.old_result, evaluator.result
 
 
 def get_string_result(scope, string):
     evaluator = StatementEvaluator(scope)
-    node = compiler.parse(string)
-    compiler.walk(node, evaluator)
+    node = ast.parse(string)
+    ast.walk(node, evaluator)
     return evaluator.result
 
 
 class Arguments(object):
     """A class for evaluating parameters passed to a function
 
-    You can use the `create_argument` factory.  It handles when the
+    You can use the `create_arguments` factory.  It handles when the
     first argument is implicit
 
     """
     def __init__(self, args, scope):
         self.args = args
         self.scope = scope
+        self.instance = None
 
     def get_arguments(self, parameters):
         result = []
     def get_pynames(self, parameters):
         result = [None] * max(len(parameters), len(self.args))
         for index, arg in enumerate(self.args):
-            if isinstance(arg, compiler.ast.Keyword) and arg.name in parameters:
-                result[parameters.index(arg.name)] = self._evaluate(arg.expr)
+            if isinstance(arg, ast.keyword) and arg.arg in parameters:
+                result[parameters.index(arg.arg)] = self._evaluate(arg.value)
             else:
                 result[index] = self._evaluate(arg)
         return result
 
 class MixedArguments(object):
 
-    def __init__(self, pyname, args, scope):
+    def __init__(self, pyname, arguments, scope):
+        """`argumens` is an instance of `Arguments`"""
         self.pyname = pyname
-        self.args = Arguments(args, scope)
+        self.args = arguments
 
     def get_pynames(self, parameters):
         return [self.pyname] + self.args.get_pynames(parameters[1:])
         return self.pyname
 
 
-def create_arguments(primary, pyfunction, call_func_node, scope):
+def create_arguments(primary, pyfunction, call_node, scope):
     """A factory for creating `Arguments`'"""
-    args = call_func_node.args
-    called = call_func_node.node
+    # TODO: handle tuple parameters
+    args = list(call_node.args)
+    args.extend(call_node.keywords)
+    called = call_node.func
     # XXX: Handle constructors
     if _is_method_call(primary, pyfunction):
-        args = list(args)
-        args.insert(0, called.expr)
+        args.insert(0, called.value)
     return Arguments(args, scope)
 
 

rope/base/oi/staticoi.py

-import compiler.ast
-import compiler.consts
-
 import rope.base
-from rope.base import pyobjects, pynames, evaluate, builtins
+from rope.base import ast, pyobjects, pynames, evaluate, builtins
 
 
 class StaticObjectInference(object):
            isinstance(pyobject.parent, pyobjects.PyClass):
             if not pyobject.decorators:
                 objects.append(pyobjects.PyObject(pyobject.parent))
-            elif self._is_staticmethod_decorator(pyobject.decorators.nodes[0]):
+            elif self._is_staticmethod_decorator(pyobject.decorators[0]):
                 objects.append(pyobjects.get_unknown())
-            elif self._is_classmethod_decorator(pyobject.decorators.nodes[0]):
+            elif self._is_classmethod_decorator(pyobject.decorators[0]):
                 objects.append(pyobject.parent)
             elif pyobject.get_param_names()[0] == 'self':
                 objects.append(pyobjects.PyObject(pyobject.parent))
         return objects
 
     def _is_staticmethod_decorator(self, node):
-        return isinstance(node, compiler.ast.Name) and node.name == 'staticmethod'
+        return isinstance(node, ast.Name) and node.id == 'staticmethod'
 
     def _is_classmethod_decorator(self, node):
-        return isinstance(node, compiler.ast.Name) and node.name == 'classmethod'
+        return isinstance(node, ast.Name) and node.id == 'classmethod'
 
     def analyze_module(self, pymodule, should_analyze=None):
         """Analyze `pymodule` for static object inference"""
     #    if hasattr(pydefined, 'get_name'):
     #        print pydefined.get_name()
     visitor = SOIVisitor(pycore, pydefined, should_analyze)
-    for child in pydefined.get_ast().getChildNodes():
-        compiler.walk(child, visitor)
+    for child in ast.get_child_nodes(pydefined.get_ast()):
+        ast.walk(child, visitor)
 
 
 class SOIVisitor(object):
         self.scope = pydefined.get_scope()
         self.should_analyze = should_analyze
 
-    def visitFunction(self, node):
+    def _FunctionDef(self, node):
         self._analyze_child(node)
 
-    def visitClass(self, node):
+    def _ClassDef(self, node):
         self._analyze_child(node)
 
     def _analyze_child(self, node):
         pydefined = self.scope.get_name(node.name).get_object()
         _analyze_node(self.pycore, pydefined, self.should_analyze)
 
-    def visitCallFunc(self, node):
-        for child in node.getChildNodes():
-            compiler.walk(child, self)
-        primary, pyname = evaluate.get_primary_and_result(self.scope, node.node)
+    def _Call(self, node):
+        for child in ast.get_child_nodes(node):
+            ast.walk(child, self)
+        primary, pyname = evaluate.get_primary_and_result(self.scope,
+                                                          node.func)
         if pyname is None:
             return
         pyfunction = pyname.get_object()
            '__call__' in pyfunction.get_attributes():
             pyfunction = pyfunction.get_attribute('__call__')
         if isinstance(pyfunction, pyobjects.AbstractFunction):
-            args = evaluate.create_arguments(primary, pyfunction, node, self.scope)
+            args = evaluate.create_arguments(primary, pyfunction,
+                                             node, self.scope)
         elif isinstance(pyfunction, pyobjects.PyClass):
             pyclass = pyfunction
             if '__init__' in pyfunction.get_attributes():
                 pyfunction = pyfunction.get_attribute('__init__').get_object()
             pyname = pynames.UnboundName(pyobjects.PyObject(pyclass))
-            args = evaluate.MixedArguments(pyname, node.args, self.scope)
+            base_args = evaluate.create_arguments(primary, pyfunction,
+                                                  node, self.scope)
+            args = evaluate.MixedArguments(pyname, base_args, self.scope)
         else:
             return
         self._call(pyfunction, args)
         if isinstance(pyfunction, builtins.BuiltinFunction):
             pyfunction.get_returned_object(args)
 
-    def visitAssign(self, node):
-        for child in node.getChildNodes():
-            compiler.walk(child, self)
+    def _Assign(self, node):
+        for child in ast.get_child_nodes(node):
+            ast.walk(child, self)
         visitor = _SOIAssignVisitor()
         nodes = []
-        for child in node.nodes:
-            compiler.walk(child, visitor)
+        for child in node.targets:
+            ast.walk(child, visitor)
             nodes.extend(visitor.nodes)
-        for assigned, levels in nodes:
-            instance = evaluate.get_statement_result(self.scope, assigned.expr)
+        for subscript, levels in nodes:
+            instance = evaluate.get_statement_result(self.scope, subscript.value)
             args_pynames = []
-            for ast in assigned.subs:
-                args_pynames.append(evaluate.get_statement_result(self.scope, ast))
+            args_pynames.append(evaluate.get_statement_result(
+                                self.scope, subscript.slice.value))
             value = self.pycore.object_infer._infer_assignment(
-                pynames._Assigned(node.expr, levels), self.pymodule)
+                pynames._Assigned(node.value, levels), self.pymodule)
             args_pynames.append(pynames.UnboundName(value))
             if instance is not None and value is not None:
                 pyobject = instance.get_object()
         self.nodes = []
 
     def _added(self, node, levels):
-        if isinstance(node, compiler.ast.Subscript):
+        if isinstance(node, ast.Subscript) and \
+           isinstance(node.slice, ast.Index):
             self.nodes.append((node, levels))

rope/base/pycore.py

-import compiler
 import difflib
 import sys
 
 import rope.base.oi.objectinfer
 import rope.base.oi.objectinfo
 import rope.base.project
-from rope.base import taskhandle
+from rope.base import ast, taskhandle
 from rope.base.exceptions import ModuleNotFoundError
 from rope.base.oi import dynamicoi
 from rope.base.pyobjects import PyModule, PyPackage, PyClass
         def __init__(self):
             self.class_lines = []
 
-        def visitClass(self, node):
+        def _ClassDef(self, node):
             self.class_lines.append(node.lineno)
             self.find_class_lines(node)
 
         def find_class_lines(self, node):
-            for child in node.getChildNodes():
-                compiler.walk(child, self)
+            for child in ast.get_child_nodes(node):
+                ast.walk(child, self)
             return self.class_lines
 
 

rope/base/pyobjects.py

-import compiler.consts
-
 import rope.base.evaluate
 import rope.base.pyscopes
-from rope.base import pynames
+from rope.base import ast, pynames
 from rope.base.exceptions import RopeError, AttributeNotFoundError
 
 
         return current_object
 
     def get_doc(self):
-        return self.get_ast().doc
+        if len(self.get_ast().body) > 0:
+            expr = self.get_ast().body[0]
+            if isinstance(expr, ast.Expr) and \
+               isinstance(expr.value, ast.Str):
+                return expr.value.s
 
     def _create_structural_attributes(self):
         return {}
     def __init__(self, pycore, ast_node, parent):
         AbstractFunction.__init__(self)
         PyDefinedObject.__init__(self, pycore, ast_node, parent)
-        self.parameters = self.ast_node.argnames
+        self.arguments = self.ast_node.args
         self.decorators = self.ast_node.decorators
         self.parameter_pyobjects = pynames._Inferred(
             self._infer_parameters, self.get_module()._get_concluded_data())
         return object_infer.infer_returned_object(self, args)
 
     def _handle_special_args(self, pyobjects):
-        if len(pyobjects) < len(self.parameters):
-            if self.get_ast().flags & compiler.consts.CO_VARARGS:
+        if len(pyobjects) == len(self.arguments.args):
+            if self.arguments.vararg:
                 pyobjects.append(rope.base.builtins.get_list())
-            if self.get_ast().flags & compiler.consts.CO_VARKEYWORDS:
+            if self.arguments.kwarg:
                 pyobjects.append(rope.base.builtins.get_dict())
 
     def _set_parameter_pyobjects(self, pyobjects):
     def get_parameters(self):
         if self.parameter_pynames is None:
             result = {}
-            for index, name in enumerate(self.parameters):
+            for index, name in enumerate(self.get_param_names()):
+                # TODO: handle tuple parameters
                 result[name] = pynames.ParameterName(self, index)
             self.parameter_pynames = result
         return self.parameter_pynames
         return self.get_ast().name
 
     def get_param_names(self, special_args=True):
-        result = list(self.parameters)
-        if not special_args:
-            node = self.get_ast()
-            if node.flags & compiler.consts.CO_VARKEYWORDS:
-                del result[-1]
-            if node.flags & compiler.consts.CO_VARARGS:
-                del result[-1]
+        # TODO: handle tuple parameters
+        result = [node.id for node in self.arguments.args
+                  if isinstance(node, ast.Name)]
+        if special_args:
+            if self.arguments.vararg:
+                result.append(self.arguments.vararg)
+            if self.arguments.kwarg:
+                result.append(self.arguments.kwarg)
         return result
 
 
 
     def _create_structural_attributes(self):
         new_visitor = _ClassVisitor(self.pycore, self)
-        for n in self.ast_node.getChildNodes():
-            compiler.walk(n, new_visitor)
+        for child in ast.get_child_nodes(self.ast_node):
+            ast.walk(child, new_visitor)
         return new_visitor.names
 
     def _create_concluded_attributes(self):
             source_code = source_code.encode('utf-8')
         self.source_code = source_code
         self._lines = None
-        ast_node = compiler.parse(source_code.rstrip(' \t'))
+        ast_node = ast.parse(source_code.rstrip(' \t'))
         self.star_imports = []
         super(PyModule, self).__init__(pycore, ast_node, resource)
 
 
     def _create_structural_attributes(self):
         visitor = _GlobalVisitor(self.pycore, self)
-        compiler.walk(self.ast_node, visitor)
+        ast.walk(self.ast_node, visitor)
         return visitor.names
 
     def _create_scope(self):
             ast_node = pycore.resource_to_pyobject(
                 resource.get_child('__init__.py')).get_ast()
         else:
-            ast_node = compiler.parse('\n')
+            ast_node = ast.parse('\n')
         super(PyPackage, self).__init__(pycore, ast_node, resource)
 
     def _create_structural_attributes(self):
         self.scope_visitor = scope_visitor
         self.assigned_ast = None
 
-    def visitAssign(self, node):
-        self.assigned_ast = node.expr
-        for child_node in node.nodes:
-            compiler.walk(child_node, self)
+    def _Assign(self, node):
+        self.assigned_ast = node.value
+        for child_node in node.targets:
+            ast.walk(child_node, self)
 
     def _assigned(self, name, assignment=None):
         old_pyname = self.scope_visitor.names.get(name, None)
         if assignment is not None:
             self.scope_visitor.names[name].assignments.append(assignment)
 
-    def visitAssName(self, node):
+    def _Name(self, node):
         assignment = None
         if self.assigned_ast is not None:
             assignment = pynames._Assigned(self.assigned_ast)
-        self._assigned(node.name, assignment)
+        self._assigned(node.id, assignment)
 
-    def visitAssTuple(self, node):
+    def _Tuple(self, node):
         names = _NodeNameCollector.get_assigned_names(node)
         for name, levels in names:
             assignment = None
                 assignment = pynames._Assigned(self.assigned_ast, levels)
             self._assigned(name, assignment)
 
-    def visitAssAttr(self, node):
+    def _Attribute(self, node):
         pass
 
-    def visitSubscript(self, node):
+    def _Subscript(self, node):
         pass
 
-    def visitSlice(self, node):
+    def _Slice(self, node):
         pass
 
 
         self._added(node, new_levels)
 
     def _added(self, node, levels):
-        if hasattr(node, 'name'):
-            self.names.append((node.name, levels))
+        if hasattr(node, 'id'):
+            self.names.append((node.id, levels))
 
-    def visitAssName(self, node):
+    def _Name(self, node):
         self._add_node(node)
 
-    def visitName(self, node):
-        self._add_node(node)
-
-    def visitTuple(self, node):
+    def _Tuple(self, node):
         new_levels = []
         if self.levels is not None:
             new_levels = list(self.levels)
             new_levels.append(self.index)
         self.index += 1
         visitor = _NodeNameCollector(new_levels)
-        for child in node.getChildNodes():
-            compiler.walk(child, visitor)
+        for child in ast.get_child_nodes(node):
+            ast.walk(child, visitor)
         self.names.extend(visitor.names)
 
-    def visitAssTuple(self, node):
-        self.visitTuple(node)
-
-    def visitAssAttr(self, node):
-        self._add_node(node)
-
-    def visitSubscript(self, node):
-        self._add_node(node)
-
-    def visitSlice(self, node):
+    def _Subscript(self, node):
         self._add_node(node)
 
     @staticmethod
     def get_assigned_names(node):
         visitor = _NodeNameCollector()
-        compiler.walk(node, visitor)
+        ast.walk(node, visitor)
         return visitor.names
 
 
         else:
             return None
 
-    def visitClass(self, node):
+    def _ClassDef(self, node):
         self.names[node.name] = pynames.DefinedName(
             PyClass(self.pycore, node, self.owner_object))
 
-    def visitFunction(self, node):
+    def _FunctionDef(self, node):
         pyobject = PyFunction(self.pycore, node, self.owner_object)
         self.names[node.name] = pynames.DefinedName(pyobject)
 
-    def visitAssign(self, node):
-        compiler.walk(node, _AssignVisitor(self))
+    def _Assign(self, node):
+        ast.walk(node, _AssignVisitor(self))
 
     def _assign_evaluated_object(self, assigned_vars, assigned,
                                  evaluation, lineno):
                 assignment=assignment, module=self.get_module(),
                 lineno=lineno, evaluation=evaluation)
 
-    def visitFor(self, node):
+    def _For(self, node):
         self._assign_evaluated_object(
-            node.assign, node.list, '.__iter__().next()', node.lineno)
-        compiler.walk(node.body, self)
+            node.target, node.iter, '.__iter__().next()', node.lineno)
+        for child in node.body:
+            ast.walk(child, self)
 
-    def visitWith(self, node):
+    def _With(self, node):
+        # ???: What if there are no optional vars?
         self._assign_evaluated_object(
-            node.vars, node.expr, '.__enter__()', node.lineno)
-        compiler.walk(node.body, self)
+            node.optional_vars, node.context_expr,
+            '.__enter__()', node.lineno)
+        for child in node.body:
+            ast.walk(child, self)
 
-    def visitImport(self, node):
+    def _Import(self, node):
         for import_pair in node.names:
-            module_name, alias = import_pair
+            module_name = import_pair.name
+            alias = import_pair.asname
             first_package = module_name.split('.')[0]
             if alias is not None:
-                self.names[alias] = pynames.ImportedModule(self.get_module(),
-                                                   module_name)
+                self.names[alias] = \
+                    pynames.ImportedModule(self.get_module(), module_name)
             else:
-                self.names[first_package] = pynames.ImportedModule(self.get_module(),
-                                                           first_package)
+                self.names[first_package] = \
+                    pynames.ImportedModule(self.get_module(), first_package)
 
-    def visitFrom(self, node):
+    def _ImportFrom(self, node):
         level = 0
-        if hasattr(node, 'level'):
+        if node.level:
             level = node.level
         imported_module = pynames.ImportedModule(self.get_module(),
-                                                 node.modname, level)
-        if node.names[0][0] == '*':
+                                                 node.module, level)
+        if len(node.names) == 1 and node.names[0].name == '*':
             self.owner_object.star_imports.append(
                 pynames.StarImport(imported_module))
         else:
-            for (name, alias) in node.names:
-                imported = name
+            for imported_name in node.names:
+                imported = imported_name.name
+                alias = imported_name.asname
                 if alias is not None:
                     imported = alias
                 self.names[imported] = pynames.ImportedName(imported_module,
-                                                            name)
+                                                            imported_name.name)
 
-    def visitGlobal(self, node):
+    def _Global(self, node):
         module = self.get_module()
         for name in node.names:
             if module is not None:
     def __init__(self, pycore, owner_object):
         super(_ClassVisitor, self).__init__(pycore, owner_object)
 
-    def visitFunction(self, node):
+    def _FunctionDef(self, node):
         pyobject = PyFunction(self.pycore, node, self.owner_object)
         self.names[node.name] = pynames.DefinedName(pyobject)
-        if len(node.argnames) > 0:
-            new_visitor = _ClassInitVisitor(self, node.argnames[0])
-            for child in node.getChildNodes():
-                compiler.walk(child, new_visitor)
+        if len(node.args.args) > 0:
+            first = node.args.args[0]
+            if isinstance(first, ast.Name):
+                new_visitor = _ClassInitVisitor(self, first.id)
+                for child in ast.get_child_nodes(node):
+                    ast.walk(child, new_visitor)
 
-    def visitClass(self, node):
+    def _ClassDef(self, node):
         self.names[node.name] = pynames.DefinedName(
             PyClass(self.pycore, node, self.owner_object))
 
         self.returned_asts = []
         self.generator = False
 
-    def visitReturn(self, node):
+    def _Return(self, node):
         self.returned_asts.append(node.value)
 
-    def visitYield(self, node):
+    def _Yield(self, node):
         self.returned_asts.append(node.value)
         self.generator = True
 
         super(_ClassInitVisitor, self).__init__(scope_visitor)
         self.self_name = self_name
 
-    def visitAssAttr(self, node):
-        if isinstance(node.expr, compiler.ast.Name) and \
-           node.expr.name == self.self_name:
-            if node.attrname not in self.scope_visitor.names:
-                self.scope_visitor.names[node.attrname] = pynames.AssignedName(
+    def _Attribute(self, node):
+        if not isinstance(node.ctx, ast.Store):
+            return
+        if isinstance(node.value, ast.Name) and \
+           node.value.id == self.self_name:
+            if node.attr not in self.scope_visitor.names:
+                self.scope_visitor.names[node.attr] = pynames.AssignedName(
                     lineno=node.lineno, module=self.scope_visitor.get_module())
-            self.scope_visitor.names[node.attrname].assignments.append(
+            self.scope_visitor.names[node.attr].assignments.append(
                 pynames._Assigned(self.assigned_ast))
 
-    def visitAssTuple(self, node):
-        for child in node.getChildNodes():
-            compiler.walk(child, self)
+    def _Tuple(self, node):
+        if not isinstance(node.ctx, ast.Store):
+            return
+        for child in ast.get_child_nodes(node):
+            ast.walk(child, self)
 
-    def visitAssName(self, node):
+    def _Name(self, node):
         pass
 
-    def visitFunction(self, node):
+    def _FunctionDef(self, node):
         pass
 
-    def visitClass(self, node):
+    def _ClassDef(self, node):
         pass
 
-    def visitFor(self, node):
+    def _For(self, node):
         pass
 
-    def visitWith(self, node):
+    def _With(self, node):
         pass
 
 

rope/base/pyscopes.py

-import compiler
-
 import rope.base.pynames
 import rope.base.pyobjects
-from rope.base import exceptions
+from rope.base import ast, exceptions
 
 
 class Scope(object):
         if self.names is None:
             new_visitor = rope.base.pyobjects._FunctionVisitor(self.pycore,
                                                                self.pyobject)
-            for n in self.pyobject.get_ast().getChildNodes():
-                compiler.walk(n, new_visitor)
+            for n in ast.get_child_nodes(self.pyobject.get_ast()):
+                ast.walk(n, new_visitor)
             self.names = self.pyobject.get_parameters()
             self.names.update(new_visitor.names)
             self.returned_asts = new_visitor.returned_asts

rope/refactor/importutils/module_imports.py

-import compiler
-
 import rope.base.pynames
+from rope.base import ast
 from rope.refactor.importutils import importinfo
 from rope.refactor.importutils import actions
 
 
     def _get_unbound_names(self, defined_pyobject):
         visitor = _GlobalUnboundNameFinder(self.pymodule, defined_pyobject)
-        compiler.walk(self.pymodule.get_ast(), visitor)
+        ast.walk(self.pymodule.get_ast(), visitor)
         return visitor.unbound
 
     def remove_unused_imports(self):
         last_index = 1
         # Getting the line of the first import fails when the first
         # import is not in the first non doc line of module
-        nodes = self.pymodule.get_ast().node
-        if nodes.getChildNodes():
-            last_index = nodes.getChildNodes()[0].lineno
+        nodes = self.pymodule.get_ast().body
+        if nodes:
+            last_index = nodes[0].lineno
         return last_index
 
     def _compare_imports(self, stmt1, stmt2):
                    get_inner_scope_for_line(node.lineno).pyobject
         visitor = _LocalUnboundNameFinder(pyobject, self)
         for child in node.getChildNodes():
-            compiler.walk(child, visitor)
+            ast.walk(child, visitor)
 
-    def visitFunction(self, node):
+    def _FunctionDef(self, node):
         self._visit_child_scope(node)
 
-    def visitClass(self, node):
+    def _ClassDef(self, node):
         self._visit_child_scope(node)
 
-    def visitName(self, node):
+    def _Name(self, node):
         if self._get_root()._is_node_interesting(node) and \
            not self.is_bound(node.name):
             self.add_unbound(node.name)
 
-    def visitGetattr(self, node):
+    def _Attribute(self, node):
         result = []
-        while isinstance(node, compiler.ast.Getattr):
+        while isinstance(node, ast.Attribute):
             result.append(node.attrname)
             node = node.expr
-        if isinstance(node, compiler.ast.Name):
+        if isinstance(node, ast.Name):
             result.append(node.name)
             primary = '.'.join(reversed(result))
             if self._get_root()._is_node_interesting(node) and \
                not self.is_bound(primary):
                 self.add_unbound(primary)
         else:
-            compiler.walk(node, self)
+            ast.walk(node, self)
 
     def _get_root(self):
         pass
                             blank_lines=self._count_empty_lines_before(start_line)))
 
     def find_import_statements(self):
-        nodes = self.pymodule.get_ast().node.nodes
+        nodes = self.pymodule.get_ast().body
         for index, node in enumerate(nodes):
-            if isinstance(node, (compiler.ast.Import, compiler.ast.From)):
+            if isinstance(node, (ast.Import, ast.ImportFrom)):
                 end_line = self.lines.length() + 1
                 if index + 1 < len(nodes):
                     end_line = nodes[index + 1].lineno
                 while self.lines.get_line(end_line - 1).strip() == '':
                     end_line -= 1
-            if isinstance(node, compiler.ast.Import):
+            if isinstance(node, ast.Import):
                 self.visit_import(node, end_line)
-            if isinstance(node, compiler.ast.From):
+            if isinstance(node, ast.ImportFrom):
                 self.visit_from(node, end_line)
         return self.imports

ropetest/advanced_oi_test.py

     def test_arguments_with_keywords(self):
         mod = self.pycore.create_module(self.project.root, 'mod')
         code = 'class C1(object):\n    pass\nclass C2(object):\n    pass\n' \
-               'def a_func(arg):\n    return arg\n' \
+               'def a_func(arg):\n    return eval("arg")\n' \
                'a = a_func(arg=C1())\nb = a_func(arg=C2())\n'
         mod.write(code)
         self.pycore.run_module(mod).wait_process()

ropetest/ide/codeassisttest.py

         code = 'class AClass(object):\n    ' + \
                '@staticmethod\n    def a_method():\n        pass\nAClass.a_method()'
         result = self.assist.get_definition_location(code, len(code) - 3)
-        self.assertEquals((None, 3), result)
+        self.assertEquals((None, 2), result)
 
     def test_get_definition_location_dotted_module_names(self):
         module_resource = self.project.get_pycore().\
         self.assertEquals((None, None), result)
 
     def test_get_definition_location_dot_spaces(self):
-        code = 'class AClass(object):\n    ' + \
-               '@staticmethod\n    def a_method():\n        pass\nAClass.\\\n     a_method()'
+        code = 'class AClass(object):\n    ' \
+               '@staticmethod\n    def a_method():\n' \
+               '        pass\nAClass.\\\n     a_method()'
         result = self.assist.get_definition_location(code, len(code) - 3)
-        self.assertEquals((None, 3), result)
+        self.assertEquals((None, 2), result)
 
     def test_get_definition_location_dot_line_break_inside_parens(self):
         code = 'class A(object):\n    def a_method(self):\n        pass\n' + \