Commits

Ali Gholami Rudi  committed 8569a14

Cleaning up rope.base.oi.staticoi

staticoi module only collects static object info, now; the
object inference parts were moved to objectinfer module.

  • Participants
  • Parent commits ee90187

Comments (0)

Files changed (6)

File docs/done.txt

 ===========
 
 
+- Deprecated `Pycore.create_(module|package)` : November 29, 2007
+
+
 - Performing refactorings across multiple projects : November 29, 2007
 
 

File rope/base/oi/objectinfer.py

 from rope.base.oi import staticoi
 
 
+_ignore_inferred = staticoi._ignore_inferred
+
 class ObjectInfer(object):
     """A class for inferring objects
 
     """
 
     def __init__(self, pycore):
-        self.soi = staticoi.StaticObjectInference(pycore)
         self.object_info = pycore.object_info
 
-    def infer_returned_object(self, pyobject, args):
-        """Infer the `PyObject` this callable `PyObject` returns after calling"""
-        result = self.object_info.get_exact_returned(pyobject, args)
+    def infer_returned_object(self, pyfunction, args):
+        """Infer the `PyObject` this `PyFunction` returns after calling"""
+        result = self.object_info.get_exact_returned(pyfunction, args)
         if result is not None:
             return result
-        result = self.soi.infer_returned_object(pyobject, args)
+        result = _infer_returned(pyfunction, args)
         if result is not None:
-            if args and pyobject.get_module().get_resource() is not None:
+            if args and pyfunction.get_module().get_resource() is not None:
                 params = args.get_arguments(
-                    pyobject.get_param_names(special_args=False))
-                self.object_info.function_called(pyobject, params, result)
+                    pyfunction.get_param_names(special_args=False))
+                self.object_info.function_called(pyfunction, params, result)
             return result
-        return self.object_info.get_returned(pyobject, args)
+        return self.object_info.get_returned(pyfunction, args)
 
-    def infer_parameter_objects(self, pyobject):
-        """Infer the `PyObject`\s of parameters of this callable `PyObject`"""
-        result = self.object_info.get_parameter_objects(pyobject)
+    def infer_parameter_objects(self, pyfunction):
+        """Infer the `PyObject`\s of parameters of this `PyFunction`"""
+        result = self.object_info.get_parameter_objects(pyfunction)
         if result is None:
-            result = self.soi.infer_parameter_objects(pyobject)
-        self._handle_first_parameter(pyobject, result)
+            result = _parameter_objects(pyfunction)
+        self._handle_first_parameter(pyfunction, result)
         return result
 
     def _handle_first_parameter(self, pyobject, parameters):
         result = self.object_info.get_passed_objects(pyfunction,
                                                      parameter_index)
         if not result:
-            statically_inferred = self.soi.infer_parameter_objects(pyfunction)
+            statically_inferred = _parameter_objects(pyfunction)
             if len(statically_inferred) > parameter_index:
                 result.append(statically_inferred[parameter_index])
         return result
 
+    @_ignore_inferred
     def _infer_assignment(self, assignment, pymodule):
-        try:
-            pyobject = self._infer_pyobject_for_assign_node(
-                assignment.ast_node, pymodule)
-            if pyobject is None:
-                return None
-            return self._infer_assignment_object(assignment, pyobject)
-        except pyobjects.IsBeingInferredError:
-            pass
+        pyobject = self._infer_pyobject_for_assign_node(
+            assignment.ast_node, pymodule)
+        if pyobject is None:
+            return None
+        return self._infer_assignment_object(assignment, pyobject)
 
     def _infer_assignment_object(self, assignment, pyobject):
         for index in assignment.levels:
                 break
         return pyobject
 
+    @_ignore_inferred
     def _infer_pyobject_for_assign_node(self, assign_node, pymodule, lineno=None):
-        try:
-            if lineno is None:
-                lineno = self._get_lineno_for_node(assign_node)
-            holding_scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
-            primary, pyname = evaluate.get_primary_and_result(holding_scope,
-                                                              assign_node)
-            if pyname is not None:
-                result = pyname.get_object()
-                if isinstance(result.get_type(), builtins.Property) and \
-                   holding_scope.get_kind() == 'Class':
-                    arg = pynames.UnboundName(pyobjects.PyObject(
-                                              holding_scope.pyobject))
-                    return result.get_type().get_property_object(
-                        evaluate.ObjectArguments([arg]))
-                return result
-        except pyobjects.IsBeingInferredError:
-            pass
+        if lineno is None:
+            lineno = self._get_lineno_for_node(assign_node)
+        holding_scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
+        primary, pyname = evaluate.get_primary_and_result(holding_scope,
+                                                          assign_node)
+        if pyname is not None:
+            result = pyname.get_object()
+            if isinstance(result.get_type(), builtins.Property) and \
+               holding_scope.get_kind() == 'Class':
+                arg = pynames.UnboundName(pyobjects.PyObject(
+                                          holding_scope.pyobject))
+                return result.get_type().get_property_object(
+                    evaluate.ObjectArguments([arg]))
+            return result
 
+    @_ignore_inferred
     def _infer_pyname_for_assign_node(self, assign_node, pymodule, lineno=None):
-        try:
-            if lineno is None:
-                lineno = self._get_lineno_for_node(assign_node)
-            holding_scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
-            return evaluate.get_statement_result(holding_scope, assign_node)
-        except pyobjects.IsBeingInferredError:
-            pass
+        if lineno is None:
+            lineno = self._get_lineno_for_node(assign_node)
+        holding_scope = pymodule.get_scope().get_inner_scope_for_line(lineno)
+        return evaluate.get_statement_result(holding_scope, assign_node)
 
     def _get_lineno_for_node(self, assign_node):
         if hasattr(assign_node, 'lineno') and \
             return assign_node.lineno
         return 1
 
+    @_ignore_inferred
     def evaluate_object(self, evaluated):
         pyobject = self._infer_pyobject_for_assign_node(
             evaluated.assignment.ast_node, evaluated.module, evaluated.lineno)
     def _get_attribute(self, pyobject, name):
         if pyobject is not None and name in pyobject.get_attributes():
             return pyobject.get_attribute(name)
+
+
+def _infer_returned(pyobject, args):
+    if args:
+        # HACK: Setting parameter objects manually
+        # This is not thread safe and might cause problems if `args`
+        # does not come from a good call site
+        pyobject.get_scope().invalidate_data()
+        pyobject._set_parameter_pyobjects(
+            args.get_arguments(pyobject.get_param_names(special_args=False)))
+    scope = pyobject.get_scope()
+    if not scope._get_returned_asts():
+        return
+    for returned_node in reversed(scope._get_returned_asts()):
+        try:
+            resulting_pyname = evaluate.get_statement_result(scope,
+                                                             returned_node)
+            if resulting_pyname is None:
+                return
+            pyobject = resulting_pyname.get_object()
+            if pyobject == pyobjects.get_unknown():
+                return
+            if not scope._is_generator():
+                return pyobject
+            else:
+                return builtins.get_generator(pyobject)
+        except pyobjects.IsBeingInferredError:
+            pass
+
+def _parameter_objects(pyobject):
+    params = pyobject.get_param_names(special_args=False)
+    return [pyobjects.get_unknown()] * len(params)

File rope/base/oi/staticoi.py

-import rope.base
 from rope.base import ast, pyobjects, pynames, evaluate, builtins
 
 
-class StaticObjectInference(object):
-    """Performs static object inference
+def analyze_module(pycore, pymodule, should_analyze, search_subscopes):
+    """Analyze `pymodule` for static object inference
 
-    It actually performs two things:
-
-    * Analyzes scopes for collection object information
-      (`analyze_module` method)
-    * Analyzes function body for infering the object that is returned
-      from a function (`infer_returned_object` method)
+    Analyzes scopes for collecting object information.  The analysis
+    starts from inner scopes.
 
     """
-
-    def __init__(self, pycore):
-        self.pycore = pycore
-
-    def infer_returned_object(self, pyobject, args):
-        if args:
-            # HACK: Setting parameter objects manually
-            # This is not thread safe and might cause problems if `args`
-            # does not come from a good call site
-            pyobject.get_scope().invalidate_data()
-            pyobject._set_parameter_pyobjects(
-                args.get_arguments(pyobject.get_param_names(special_args=False)))
-        scope = pyobject.get_scope()
-        if not scope._get_returned_asts():
-            return
-        for returned_node in reversed(scope._get_returned_asts()):
-            try:
-                resulting_pyname = evaluate.get_statement_result(scope,
-                                                                 returned_node)
-                if resulting_pyname is None:
-                    return None
-                pyobject = resulting_pyname.get_object()
-                if pyobject == pyobjects.get_unknown():
-                    return
-                if not scope._is_generator():
-                    return resulting_pyname.get_object()
-                else:
-                    return builtins.get_generator(resulting_pyname.get_object())
-            except pyobjects.IsBeingInferredError:
-                pass
-
-    def infer_parameter_objects(self, pyobject):
-        params = pyobject.get_param_names(special_args=False)
-        return [pyobjects.get_unknown()] * len(params)
-
-    def analyze_module(self, pymodule, should_analyze, search_subscopes):
-        """Analyze `pymodule` for static object inference
-
-        The analysis starts from inner scopes first.
-
-        """
-        _analyze_node(self.pycore, pymodule, should_analyze, search_subscopes)
+    _analyze_node(pycore, pymodule, should_analyze, search_subscopes)
 
 
 def _analyze_node(pycore, pydefined, should_analyze, search_subscopes):
             ast.walk(child, visitor)
 
 
+def _ignore_inferred(func):
+    def newfunc(*args, **kwds):
+        try:
+            return func(*args, **kwds)
+        except pyobjects.IsBeingInferredError:
+            pass
+    return newfunc
+
+
 class SOIVisitor(object):
 
     def __init__(self, pycore, pydefined):
     def _ClassDef(self, node):
         pass
 
+    @_ignore_inferred
     def _Call(self, node):
         for child in ast.get_child_nodes(node):
             ast.walk(child, self)
         if isinstance(pyfunction, builtins.BuiltinFunction):
             pyfunction.get_returned_object(args)
 
+    @_ignore_inferred
     def _Assign(self, node):
         for child in ast.get_child_nodes(node):
             ast.walk(child, self)

File rope/base/pycore.py

 import rope.base.oi.objectinfer
 import rope.base.oi.objectinfo
 import rope.base.project
-from rope.base import ast, exceptions, taskhandle, codeanalyze
+from rope.base import ast, exceptions, taskhandle
 from rope.base.exceptions import ModuleNotFoundError
-from rope.base.oi import dynamicoi
+from rope.base.oi import dynamicoi, staticoi
 from rope.base.pyobjects import PyModule, PyPackage, PyClass
 
 
             result.extend(self._find_source_folders(resource))
         return result
 
-    def _get_object_infer(self):
-        return self.object_infer
-
     def run_module(self, resource, args=None, stdin=None, stdout=None):
         """Run `resource` module
 
         """
         pymodule = self.resource_to_pyobject(resource)
         self.module_cache.forget_all_data()
-        self.object_infer.soi.analyze_module(pymodule, should_analyze,
-                                             search_subscopes)
+        staticoi.analyze_module(self, pymodule,
+                                should_analyze, search_subscopes)
 
     def get_subclasses(self, pyclass, task_handle=taskhandle.NullTaskHandle()):
         classes = self.classes_cache.get_classes(task_handle)

File rope/base/pynames.py

 
     def _get_inferred(self):
         if self.module is not None:
-            object_infer = self.module.pycore._get_object_infer()
+            object_infer = self.module.pycore.object_infer
             return object_infer.infer_assigned_object(self)
 
     def get_object(self):
                                   _get_concluded_data(module))
 
     def _get_inferred(self):
-        object_infer = self.module.pycore._get_object_infer()
+        object_infer = self.module.pycore.object_infer
         return object_infer.evaluate_object(self)
 
     def get_object(self):
 
     def get_objects(self):
         """Returns the list of objects passed as this parameter"""
-        object_infer = self.pyfunction.pycore._get_object_infer()
+        object_infer = self.pyfunction.pycore.object_infer
         return object_infer.get_passed_objects(self.pyfunction, self.index)
 
     def get_definition_location(self):

File rope/base/pyobjects.py

         return rope.base.pyscopes.FunctionScope(self.pycore, self)
 
     def _infer_parameters(self):
-        object_infer = self.pycore._get_object_infer()
+        object_infer = self.pycore.object_infer
         pyobjects = object_infer.infer_parameter_objects(self)
         self._handle_special_args(pyobjects)
         return pyobjects
 
     def _infer_returned(self, args=None):
-        object_infer = self.pycore._get_object_infer()
+        object_infer = self.pycore.object_infer
         return object_infer.infer_returned_object(self, args)
 
     def _handle_special_args(self, pyobjects):