Repository: spyder-ide/spyder

Issue:
Setting Numpy threshold to nan in console makes the Variable Explorer really slow
Running this code, for example, in an IPython console

```python
import numpy as np
np.set_printoptions(threshold=np.nan)
x = np.random.rand(75000,5)
```

makes the Variable Explorer to take a really long time until it shows the `x` variable on it.


Discussion:

Resolving patch:
diff --git a/spyder/widgets/variableexplorer/utils.py b/spyder/widgets/variableexplorer/utils.py
--- a/spyder/widgets/variableexplorer/utils.py
+++ b/spyder/widgets/variableexplorer/utils.py
@@ -48,15 +48,14 @@ class FakeObject(object):
 #==============================================================================
 try:
     from numpy import (ndarray, array, matrix, recarray,
-                      int64, int32, float64, float32,
-                      complex64, complex128)
+                       int64, int32, float64, float32,
+                       complex64, complex128)
     from numpy.ma import MaskedArray
     from numpy import savetxt as np_savetxt
-    from numpy import set_printoptions as np_set_printoptions
+    from numpy import get_printoptions, set_printoptions
 except ImportError:
     ndarray = array = matrix = recarray = MaskedArray = np_savetxt = \
-    np_set_printoptions = int64 = int32 = float64 = float32 = \
-    complex64 = complex128 = FakeObject
+    int64 = int32 = float64 = float32 = complex64 = complex128 = FakeObject
 
 def get_numpy_dtype(obj):
     """Return NumPy data type associated to obj
@@ -160,9 +159,6 @@ def get_object_attrs(obj):
 CollectionsRepr.maxtuple = 10
 CollectionsRepr.maxset = 10
 
-if np_set_printoptions is not FakeObject:
-    np_set_printoptions(threshold=10)
-
 
 #==============================================================================
 # Date and datetime objects support
@@ -259,9 +255,17 @@ def unsorted_unique(lista):
 #==============================================================================
 def value_to_display(value, minmax=False):
     """Convert value for display purpose"""
+    # To save current Numpy threshold
+    np_threshold = FakeObject
     try:
         numeric_numpy_types = (int64, int32, float64, float32,
                                complex128, complex64)
+        if ndarray is not FakeObject:
+            # Save threshold
+            np_threshold = get_printoptions().get('threshold')
+            # Set max number of elements to show for Numpy arrays
+            # in our display
+            set_printoptions(threshold=10)
         if isinstance(value, recarray):
             fields = value.names
             display = 'Field names: ' + ', '.join(fields)
@@ -326,6 +330,10 @@ def value_to_display(value, minmax=False):
     if len(display) > 80:
         display = display[:80].rstrip() + ' ...'
 
+    # Restore Numpy threshold
+    if np_threshold is not FakeObject:
+        set_printoptions(threshold=np_threshold)
+
     return display
 
 


Test patch:
diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py
--- a/spyder/app/tests/test_mainwindow.py
+++ b/spyder/app/tests/test_mainwindow.py
@@ -104,6 +104,32 @@ def close_window():
 #==============================================================================
 # Tests
 #==============================================================================
+@flaky(max_runs=3)
+@pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
+def test_np_threshold(main_window, qtbot):
+    """Test that setting Numpy threshold doesn't make the Variable Explorer slow."""
+    # Set Numpy threshold
+    shell = main_window.ipyconsole.get_current_shellwidget()
+    qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)
+    with qtbot.waitSignal(shell.executed):
+        shell.execute('import numpy as np; np.set_printoptions(threshold=np.nan)')
+
+    # Create a big Numpy array
+    with qtbot.waitSignal(shell.executed):
+        shell.execute('x = np.random.rand(75000,5)')
+
+    # Wait a very small time to see the array in the Variable Explorer
+    main_window.variableexplorer.visibility_changed(True)
+    nsb = main_window.variableexplorer.get_focus_widget()
+    qtbot.waitUntil(lambda: nsb.editor.model.rowCount() == 1, timeout=500)
+
+    # Assert that NumPy threshold remains the same as the one
+    # set by the user
+    with qtbot.waitSignal(shell.executed):
+        shell.execute("t = np.get_printoptions()['threshold']")
+    assert np.isnan(shell.get_value('t'))
+
+
 @flaky(max_runs=3)
 @pytest.mark.skipif(os.name == 'nt', reason="It times out sometimes on Windows")
 def test_change_types_in_varexp(main_window, qtbot):