From 3dc870eb9030c250181afc1573dbddb7e9a3eed5 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Sat, 16 Sep 2017 18:25:23 -0500 Subject: [PATCH 1/3] Variable Explorer: Handle TypeError's in the Array editor --- .../widgets/variableexplorer/arrayeditor.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/spyder/widgets/variableexplorer/arrayeditor.py b/spyder/widgets/variableexplorer/arrayeditor.py index c50cfe06930..ca0b28699c0 100644 --- 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() From e2707a42c7ff1d5b9b850b7c1bb1903910432ba7 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Sat, 16 Sep 2017 18:25:56 -0500 Subject: [PATCH 2/3] Testing: Add a test to verify that we don't get TypeError's for certain arrays --- .../variableexplorer/tests/test_arrayeditor.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spyder/widgets/variableexplorer/tests/test_arrayeditor.py b/spyder/widgets/variableexplorer/tests/test_arrayeditor.py index a2f19e7329c..a2793d6050a 100644 --- 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) From db5ddd498632a3d2539d231bfd2147ffdea300f0 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Sat, 16 Sep 2017 18:31:50 -0500 Subject: [PATCH 3/3] Fix style issue --- spyder/widgets/variableexplorer/arrayeditor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spyder/widgets/variableexplorer/arrayeditor.py b/spyder/widgets/variableexplorer/arrayeditor.py index ca0b28699c0..da2a1bdf283 100644 --- a/spyder/widgets/variableexplorer/arrayeditor.py +++ b/spyder/widgets/variableexplorer/arrayeditor.py @@ -276,7 +276,7 @@ def data(self, index, role=Qt.DisplayRole): try: hue = (self.hue0 + self.dhue * (float(self.vmax) - self.color_func(value)) - /(float(self.vmax) - self.vmin)) + / (float(self.vmax) - self.vmin)) hue = float(np.abs(hue)) color = QColor.fromHsvF(hue, self.sat, self.val, self.alp) return to_qvariant(color)