Repository: davidhalter/jedi

Issue:
Slow completion for existing object with long `__repr__` method
Hi,

For completion with existing objects it seems that the `__repr__()` method is evaluated for an error message within the inspect module which is afterward caught. This makes the completion as slow as `__repr__()` while this evaluation in unnecessary for the user. This method can bee rather slow with big pandas objects or custom class.

This as been seen here :
[qtconsole issue #90](https://github.com/jupyter/qtconsole/issues/90)

A minimal code to reproduce it :
```python
import jedi
class Bugger(object):
    def __init__(self, size=10000):
        """Create bigg data."""
        self.big_data = [list(range(i)) for i in range(size)]

    def easy_method(self):
        """Method that should be really fast."""
        return self.big_data[-1][-1]

    def __repr__(self):
        output = ""
        for nested in self.big_data:
            for elem in nested:
                output += str(elem)
            output += '\n'
        return output

test = Bugger()
jedi.Interpreter('test.ea', [locals()]).completions()
```

And a more convenient working version :
```python
import jedi, pdb, inspect
class Bugger(object):
    def __init__(self, size=10):
        """Create bigg data."""
        self.big_data = [list(range(i)) for i in range(size)]

    def easy_method(self):
        """Method that should be really fast."""
        return self.big_data[-1][-1]

    def __repr__(self):
        frames = inspect.getouterframes(inspect.currentframe())
        for frame in frames:
            print(frame)
        pdb.set_trace()
        output = ""
        for nested in self.big_data:
            for elem in nested:
                output += str(elem)
            output += '\n'
        return output

test = Bugger()
jedi.Interpreter('test.ea', [locals()]).completions()
```

Jedi seems to end here through jedi/evaluate/compiled/mixed.py, line 121.
Maybe making checks before the calling `inspect.getsourcefile()` could fix it, but not I don't know jedi enough to claim it.

Discussion:
davidhalter: I like! Thanks man. Good test.

Patch:
diff --git a/AUTHORS.txt b/AUTHORS.txt
index b5619dcb..52204a8b 100644
--- a/AUTHORS.txt
+++ b/AUTHORS.txt
@@ -43,6 +43,7 @@ bstaint (@bstaint)
 Mathias Rav (@Mortal) <rav@cs.au.dk>
 Daniel Fiterman (@dfit99) <fitermandaniel2@gmail.com>
 Simon Ruggier (@sruggier)
+Élie Gouzien (@ElieGouzien)
 
 
 Note: (@user) means a github user name.
diff --git a/jedi/evaluate/compiled/mixed.py b/jedi/evaluate/compiled/mixed.py
index dfb49657..1390ea47 100644
--- a/jedi/evaluate/compiled/mixed.py
+++ b/jedi/evaluate/compiled/mixed.py
@@ -116,8 +116,21 @@ def _load_module(evaluator, path, python_object):
     return module
 
 
+def source_findable(python_object):
+    """Check if inspect.getfile has a chance to find the source."""
+    return (inspect.ismodule(python_object) or
+            inspect.isclass(python_object) or
+            inspect.ismethod(python_object) or
+            inspect.isfunction(python_object) or
+            inspect.istraceback(python_object) or
+            inspect.isframe(python_object) or
+            inspect.iscode(python_object))
+
+
 def find_syntax_node_name(evaluator, python_object):
     try:
+        if not source_findable(python_object):
+            raise TypeError  # Prevents computation of `repr` within inspect.
         path = inspect.getsourcefile(python_object)
     except TypeError:
         # The type might not be known (e.g. class_with_dict.__weakref__)


Test patch:
diff --git a/test/test_speed.py b/test/test_speed.py
index dcb69840..8d5bc9a3 100644
--- a/test/test_speed.py
+++ b/test/test_speed.py
@@ -51,3 +51,19 @@ class TestSpeed(TestCase):
         with open('speed/precedence.py') as f:
             line = len(f.read().splitlines())
         assert jedi.Script(line=line, path='speed/precedence.py').goto_definitions()
+
+    @_check_speed(0.1)
+    def test_no_repr_computation(self):
+        """
+        For Interpreter completion aquisition of sourcefile can trigger
+        unwanted computation of repr(). Exemple : big pandas data.
+        See issue #919.
+        """
+        class SlowRepr():
+            "class to test what happens if __repr__ is very slow."
+            def some_method(self):
+                pass
+            def __repr__(self):
+                time.sleep(0.2)
+        test = SlowRepr()
+        jedi.Interpreter('test.som', [locals()]).completions()