Commits

Anonymous committed cb073e0

arguments: added related classes

Comments (0)

Files changed (6)

rope/base/arguments.py

+import rope.base.evaluate
+from rope.base import ast
+
+
+class Arguments(object):
+    """A class for evaluating parameters passed to a function
+
+    You can use the `create_arguments` factory.  It handles implicit
+    first arguments.
+
+    """
+
+    def __init__(self, args, scope):
+        self.args = args
+        self.scope = scope
+        self.instance = None
+
+    def get_arguments(self, parameters):
+        result = []
+        for pyname in self.get_pynames(parameters):
+            if pyname is None:
+                result.append(None)
+            else:
+                result.append(pyname.get_object())
+        return result
+
+    def get_pynames(self, parameters):
+        result = [None] * max(len(parameters), len(self.args))
+        for index, arg in enumerate(self.args):
+            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
+
+    def get_instance_pyname(self):
+        if self.args:
+            return self._evaluate(self.args[0])
+
+    def _evaluate(self, ast_node):
+        return rope.base.evaluate.get_statement_result(self.scope, ast_node)
+
+
+def create_arguments(primary, pyfunction, call_node, scope):
+    """A factory for creating `Arguments`"""
+    args = list(call_node.args)
+    args.extend(call_node.keywords)
+    called = call_node.func
+    # XXX: Handle constructors
+    if _is_method_call(primary, pyfunction) and \
+       isinstance(called, ast.Attribute):
+        args.insert(0, called.value)
+    return Arguments(args, scope)
+
+
+class ObjectArguments(object):
+
+    def __init__(self, pynames):
+        self.pynames = pynames
+
+    def get_arguments(self, parameters):
+        result = []
+        for pyname in self.pynames:
+            if pyname is None:
+                result.append(None)
+            else:
+                result.append(pyname.get_object())
+        return result
+
+    def get_pynames(self, parameters):
+        return self.pynames
+
+    def get_instance_pyname(self):
+        return self.pynames[0]
+class MixedArguments(object):
+
+    def __init__(self, pyname, arguments, scope):
+        """`argumens` is an instance of `Arguments`"""
+        self.pyname = pyname
+        self.args = arguments
+
+    def get_pynames(self, parameters):
+        return [self.pyname] + self.args.get_pynames(parameters[1:])
+
+    def get_arguments(self, parameters):
+        result = []
+        for pyname in self.get_pynames(parameters):
+            if pyname is None:
+                result.append(None)
+            else:
+                result.append(pyname.get_object())
+        return result
+
+    def get_instance_pyname(self):
+        return self.pyname
+
+
+def _is_method_call(primary, pyfunction):
+    if primary is None:
+        return False
+    pyobject = primary.get_object()
+    if isinstance(pyobject.get_type(), rope.base.pyobjects.PyClass) and \
+       isinstance(pyfunction, rope.base.pyobjects.PyFunction) and \
+       isinstance(pyfunction.parent, rope.base.pyobjects.PyClass):
+        return True
+    if isinstance(pyobject.get_type(), rope.base.pyobjects.AbstractClass) and \
+       isinstance(pyfunction, rope.base.builtins.BuiltinFunction):
+        return True
+    return False

rope/base/builtins.py

 import inspect
 
 import rope.base.evaluate
-from rope.base import pynames, pyobjects
+from rope.base import pynames, pyobjects, arguments
 
 
 class BuiltinModule(pyobjects.AbstractModule):
             return
         new_dict = context.get_pynames(['self', 'd'])[1]
         if new_dict and isinstance(new_dict.get_object().get_type(), Dict):
-            args = rope.base.evaluate.ObjectArguments([new_dict])
+            args = arguments.ObjectArguments([new_dict])
             items = new_dict.get_object()['popitem'].\
                     get_object().get_returned_object(args)
             context.save_per_name(items)
     if pyname is None:
         return None
     seq = pyname.get_object()
-    args = rope.base.evaluate.ObjectArguments([pyname])
+    args = arguments.ObjectArguments([pyname])
     if '__iter__' in seq:
         iter = seq['__iter__'].get_object().\
                get_returned_object(args)

rope/base/evaluate.py

 import rope.base.builtins
 import rope.base.pynames
 import rope.base.pyobjects
-from rope.base import ast, astutils, exceptions, pyobjects
+from rope.base import ast, astutils, exceptions, pyobjects, arguments
 from rope.base.codeanalyze import WordRangeFinder
 
 
         if pyobject is None:
             return
         def _get_returned(pyobject):
-            args = create_arguments(primary, pyobject, node, self.scope)
+            args = arguments.create_arguments(primary, pyobject,
+                                              node, self.scope)
             return pyobject.get_returned_object(args)
         if isinstance(pyobject, rope.base.pyobjects.AbstractClass):
             result = None
             args = [node]
             if other_args:
                 args += other_args
-            arguments = Arguments(args, self.scope)
+            arguments_ = arguments.Arguments(args, self.scope)
             self.result = rope.base.pynames.UnboundName(
-                pyobject=call_function.get_returned_object(arguments))
+                pyobject=call_function.get_returned_object(arguments_))
 
     def _Lambda(self, node):
         self.result = rope.base.pynames.UnboundName(
             pyobject=rope.base.builtins.Lambda(node, self.scope))
 
 
-class Arguments(object):
-    """A class for evaluating parameters passed to a function
-
-    You can use the `create_arguments` factory.  It handles implicit
-    first arguments.
-
-    """
-
-    def __init__(self, args, scope):
-        self.args = args
-        self.scope = scope
-        self.instance = None
-
-    def get_arguments(self, parameters):
-        result = []
-        for pyname in self.get_pynames(parameters):
-            if pyname is None:
-                result.append(None)
-            else:
-                result.append(pyname.get_object())
-        return result
-
-    def get_pynames(self, parameters):
-        result = [None] * max(len(parameters), len(self.args))
-        for index, arg in enumerate(self.args):
-            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
-
-    def get_instance_pyname(self):
-        if self.args:
-            return self._evaluate(self.args[0])
-
-    def _evaluate(self, ast_node):
-        return get_statement_result(self.scope, ast_node)
-
-
-class ObjectArguments(object):
-
-    def __init__(self, pynames):
-        self.pynames = pynames
-
-    def get_arguments(self, parameters):
-        result = []
-        for pyname in self.pynames:
-            if pyname is None:
-                result.append(None)
-            else:
-                result.append(pyname.get_object())
-        return result
-
-    def get_pynames(self, parameters):
-        return self.pynames
-
-    def get_instance_pyname(self):
-        return self.pynames[0]
-
-
-class MixedArguments(object):
-
-    def __init__(self, pyname, arguments, scope):
-        """`argumens` is an instance of `Arguments`"""
-        self.pyname = pyname
-        self.args = arguments
-
-    def get_pynames(self, parameters):
-        return [self.pyname] + self.args.get_pynames(parameters[1:])
-
-    def get_arguments(self, parameters):
-        result = []
-        for pyname in self.get_pynames(parameters):
-            if pyname is None:
-                result.append(None)
-            else:
-                result.append(pyname.get_object())
-        return result
-
-    def get_instance_pyname(self):
-        return self.pyname
-
-
-def create_arguments(primary, pyfunction, call_node, scope):
-    """A factory for creating `Arguments`"""
-    args = list(call_node.args)
-    args.extend(call_node.keywords)
-    called = call_node.func
-    # XXX: Handle constructors
-    if _is_method_call(primary, pyfunction) and \
-       isinstance(called, ast.Attribute):
-        args.insert(0, called.value)
-    return Arguments(args, scope)
-
-
-def _is_method_call(primary, pyfunction):
-    if primary is None:
-        return False
-    pyobject = primary.get_object()
-    if isinstance(pyobject.get_type(), rope.base.pyobjects.PyClass) and \
-       isinstance(pyfunction, rope.base.pyobjects.PyFunction) and \
-       isinstance(pyfunction.parent, rope.base.pyobjects.PyClass):
-        return True
-    if isinstance(pyobject.get_type(), rope.base.pyobjects.AbstractClass) and \
-       isinstance(pyfunction, rope.base.builtins.BuiltinFunction):
-        return True
-    return False
-
-
 def _get_evaluated_names(targets, assigned, module, evaluation, lineno):
     result = {}
     for name, levels in astutils.get_name_levels(targets):

rope/base/oi/objectinfer.py

 import rope.base.builtins
 import rope.base.pynames
 import rope.base.pyobjects
-from rope.base import evaluate, utils
+from rope.base import evaluate, utils, arguments
 
 
 _ignore_inferred = utils.ignore_exception(
             arg = rope.base.pynames.UnboundName(
                 rope.base.pyobjects.PyObject(holding_scope.pyobject))
             return pyname, result.get_type().get_property_object(
-                evaluate.ObjectArguments([arg]))
+                arguments.ObjectArguments([arg]))
         return pyname, result
 
 @_ignore_inferred
                 pyobject = new_pyname.get_object()
         if pyobject is not None and call:
             if isinstance(pyobject, rope.base.pyobjects.AbstractFunction):
-                args = evaluate.ObjectArguments([pyname])
+                args = arguments.ObjectArguments([pyname])
                 pyobject = pyobject.get_returned_object(args)
             else:
                 pyobject = None

rope/base/oi/staticoi.py

 import rope.base.ast
 import rope.base.oi.objectinfer
 import rope.base.pynames
-from rope.base import pyobjects, evaluate, astutils
+from rope.base import pyobjects, evaluate, astutils, arguments
 
 
 def analyze_module(pycore, pymodule, should_analyze, search_subscopes):
             return
         pyfunction = pyname.get_object()
         if isinstance(pyfunction, pyobjects.AbstractFunction):
-            args = evaluate.create_arguments(primary, pyfunction,
-                                             node, self.scope)
+            args = arguments.create_arguments(primary, pyfunction,
+                                              node, self.scope)
         elif isinstance(pyfunction, pyobjects.PyClass):
             pyclass = pyfunction
             if '__init__' in pyfunction:
         self._call(pyfunction, args)
 
     def _args_with_self(self, primary, self_pyname, pyfunction, node):
-        base_args = evaluate.create_arguments(primary, pyfunction,
-                                              node, self.scope)
-        return evaluate.MixedArguments(self_pyname, base_args, self.scope)
+        base_args = arguments.create_arguments(primary, pyfunction,
+                                               node, self.scope)
+        return arguments.MixedArguments(self_pyname, base_args, self.scope)
 
     def _call(self, pyfunction, args):
         if isinstance(pyfunction, pyobjects.PyFunction):
                 pyobject = instance.get_object()
                 if '__setitem__' in pyobject:
                     pyfunction = pyobject['__setitem__'].get_object()
-                    args = evaluate.ObjectArguments([instance] + args_pynames)
+                    args = arguments.ObjectArguments([instance] + args_pynames)
                     self._call(pyfunction, args)
                 # IDEA: handle `__setslice__`, too
 

rope/base/pyobjectsdef.py

 import rope.base.builtins
 import rope.base.oi.objectinfer
 import rope.base.pyscopes
-from rope.base import (pynamesdef as pynames, exceptions,
-                       ast, astutils, pyobjects, fscommands)
+from rope.base import pynamesdef as pynames, exceptions, ast, astutils, pyobjects, fscommands, arguments
 from rope.base.pyobjects import *
 
 
                     arg = pynames.UnboundName(PyObject(self.owner_object))
                     def _eval(type_=type_, arg=arg):
                         return type_.get_property_object(
-                            rope.base.evaluate.ObjectArguments([arg]))
+                            arguments.ObjectArguments([arg]))
                     self.names[node.name] = pynames.EvaluatedName(
                         _eval, module=self.get_module(), lineno=node.lineno)
                     break