Repository: spyder-ide/spyder

Issue:
Variable explorer crash when viewing numpy structured array
The following code creates the object `test_array` , and double-click on it in variable explorer results in error and immediate crash.

```
import numpy as np
test_array  = np.empty(2,dtype=[('X', 'f8', (2,10)), ('S', 'S10')])
```

I cannot capture the error message and there is no crash log file.

* Spyder Version: 3.1.4
* Python Version: 2.7.10 Anaconda
* Operating system: MacOS Sierra
* QT Version: 7.7.3
* PYQTVersion: 5.6.0





Discussion:
Thanks for reporting. This is a serious bug indeed!

We'll fix it in Spyder 3.2.4

Resolving patch:
diff --git a/spyder/widgets/variableexplorer/arrayeditor.py b/spyder/widgets/variableexplorer/arrayeditor.py
--- a/spyder/widgets/variableexplorer/arrayeditor.py
+++ b/spyder/widgets/variableexplorer/arrayeditor.py
@@ -264,17 +264,24 @@ def data(self, index, role=Qt.DisplayRole):
             if value is np.ma.masked:
                 return ''
             else:
-                return to_qvariant(self._format % value)
+                try:
+                    return to_qvariant(self._format % value)
+                except TypeError:
+                    self.readonly = True
+                    return repr(value)
         elif role == Qt.TextAlignmentRole:
             return to_qvariant(int(Qt.AlignCenter|Qt.AlignVCenter))
         elif role == Qt.BackgroundColorRole and self.bgcolor_enabled \
           and value is not np.ma.masked:
-            hue = self.hue0+\
-                  self.dhue*(float(self.vmax)-self.color_func(value)) \
-                  /(float(self.vmax)-self.vmin) #float convert to handle bool arrays
-            hue = float(np.abs(hue))
-            color = QColor.fromHsvF(hue, self.sat, self.val, self.alp)
-            return to_qvariant(color)
+            try:
+                hue = (self.hue0 +
+                       self.dhue * (float(self.vmax) - self.color_func(value))
+                       / (float(self.vmax) - self.vmin))
+                hue = float(np.abs(hue))
+                color = QColor.fromHsvF(hue, self.sat, self.val, self.alp)
+                return to_qvariant(color)
+            except TypeError:
+                return to_qvariant()
         elif role == Qt.FontRole:
             return to_qvariant(get_font(font_size_delta=DEFAULT_SMALL_DELTA))
         return to_qvariant()


Test patch:
diff --git a/spyder/widgets/variableexplorer/tests/test_arrayeditor.py b/spyder/widgets/variableexplorer/tests/test_arrayeditor.py
--- a/spyder/widgets/variableexplorer/tests/test_arrayeditor.py
+++ b/spyder/widgets/variableexplorer/tests/test_arrayeditor.py
@@ -38,6 +38,18 @@ def setup_arrayeditor(qbot, data, title="", xlabels=None, ylabels=None):
 
 # --- Tests
 # -----------------------------------------------------------------------------
+def test_type_errors(qtbot):
+    """Verify that we don't get a TypeError for certain structured arrays.
+
+    Fixes issue 5254.
+    """
+    arr = np.ones(2, dtype=[('X', 'f8', (2,10)), ('S', 'S10')])
+    dlg = setup_arrayeditor(qtbot, arr)
+    qtbot.keyClick(dlg.arraywidget.view, Qt.Key_Down, modifier=Qt.ShiftModifier)
+    contents = dlg.arraywidget.model.get_value(dlg.arraywidget.model.index(0, 0))
+    assert_array_equal(contents, np.ones(10))
+
+
 def test_arrayeditor_format(qtbot):
     """Changes the format of the array and validates its selected content."""
     arr = np.array([1, 2, 3], dtype=np.float32)