Anonymous avatar Anonymous committed 8a2e04f

pynames: better recursion handling

Comments (0)

Files changed (1)

rope/base/pynames.py

         return (self._get_pymodule().get_module(), 1)
 
 
+class prevent_recursion(object):
+
+    def __init__(self, default):
+        self.default = default
+
+    def __call__(self, func):
+        name = '_%s_cache' % func.__name__
+        def newfunc(host, *args, **kwds):
+            if getattr(host, name, False):
+                return self.default()
+            setattr(host, name, True)
+            try:
+                return func(host, *args, **kwds)
+            finally:
+                setattr(host, name, False)
+        return newfunc
+
+
 class ImportedName(PyName):
 
     def __init__(self, imported_module, imported_name):
             pass
         return UnboundName()
 
-    _getting_object = False
+    @prevent_recursion(rope.base.pyobjects.get_unknown)
     def get_object(self):
-        if not self._getting_object:
-            try:
-                self._getting_object = True
-                return self._get_imported_pyname().get_object()
-            finally:
-                self._getting_object = False
-        return rope.base.pyobjects.get_unknown()
+        return self._get_imported_pyname().get_object()
 
-    _getting_location = False
+    @prevent_recursion(lambda: (None, None))
     def get_definition_location(self):
-        if not self._getting_location:
-            try:
-                self._getting_location = True
-                return self._get_imported_pyname().get_definition_location()
-            finally:
-                self._getting_location = False
-        return (None, None)
+        return self._get_imported_pyname().get_definition_location()
 
 
 class StarImport(object):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.