Repository: spyder-ide/spyder

Issue:
Error when trying to view the contents of a binary string in the Variable Explorer for Python 2
If you evaluate this code in Python 2

```python
import string
dig_its = string.digits;
translate_digits = string.maketrans(dig_its,len(dig_its)*' ')
```

and then do a double-click on the variable called `translate_digits` in the Variable Explorer, you get this error in the internal console

```python-traceback
Traceback (most recent call last):
  File "/home/carlos/Projects/spyder/github-repo/spyder/widgets/variableexplorer/collectionseditor.py", line 483, in createEditor
    editor = TextEditor(value, key)
  File "/home/carlos/Projects/spyder/github-repo/spyder/widgets/variableexplorer/texteditor.py", line 44, in __init__
    text = to_text_string(text, 'utf8')
  File "/home/carlos/Projects/spyder/github-repo/spyder/py3compat.py", line 137, in to_text_string
    return unicode(obj, encoding)
  File "/home/carlos/.virtualenvs/test-ipy-stable/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 128: invalid start byte
```

i think the solution is if we fail to initialize `TextEditor`, then we shouldn't show it.

@mariacamilaremolinagutierrez, `DataFrameEditor` has a method called `setup_and_check`, for you to look at. `TextEditor` should have a similar method to use it as `DataFrameEditor.setup_and_check` is also used.


Discussion:
@mariacamilaremolinagutierrez, this is your last issue for 3.1, so please try to finish it this week.

Then you can start to work on your project.

Resolving patch:
diff --git a/spyder/widgets/variableexplorer/collectionseditor.py b/spyder/widgets/variableexplorer/collectionseditor.py
--- a/spyder/widgets/variableexplorer/collectionseditor.py
+++ b/spyder/widgets/variableexplorer/collectionseditor.py
@@ -494,9 +494,12 @@ def createEditor(self, parent, option, index):
             return editor
         #---editor = TextEditor
         elif is_text_string(value) and len(value) > 40:
-            editor = TextEditor(value, key)
-            self.create_dialog(editor, dict(model=index.model(), editor=editor,
-                                            key=key, readonly=readonly))
+            te = TextEditor(None)
+            if te.setup_and_check(value):
+                editor = TextEditor(value, key)
+                self.create_dialog(editor, dict(model=index.model(),
+                                                editor=editor, key=key,
+                                                readonly=readonly))
             return None
         #---editor = QLineEdit
         elif is_editable_type(value):
diff --git a/spyder/widgets/variableexplorer/texteditor.py b/spyder/widgets/variableexplorer/texteditor.py
--- a/spyder/widgets/variableexplorer/texteditor.py
+++ b/spyder/widgets/variableexplorer/texteditor.py
@@ -89,6 +89,14 @@ def get_value(self):
         # already been destroyed, due to the Qt.WA_DeleteOnClose attribute
         return self.text
 
+    def setup_and_check(self, value):
+        """Verify if TextEditor is able to display strings passed to it."""
+        try:
+            to_text_string(value, 'utf8')
+            return True
+        except:
+            return False
+
 
 #==============================================================================
 # Tests


Test patch:
diff --git a/spyder/widgets/variableexplorer/tests/test_texteditor.py b/spyder/widgets/variableexplorer/tests/test_texteditor.py
--- a/spyder/widgets/variableexplorer/tests/test_texteditor.py
+++ b/spyder/widgets/variableexplorer/tests/test_texteditor.py
@@ -12,6 +12,7 @@
 import pytest
 
 # Local imports
+from spyder.py3compat import PY2
 from spyder.widgets.variableexplorer.texteditor import TextEditor
 
 @pytest.fixture
@@ -31,6 +32,16 @@ def test_texteditor(qtbot):
     dlg_text = texteditor.get_value()
     assert text == dlg_text
 
+def test_texteditor_setup_and_check():
+    if PY2:
+        import string
+        dig_its = string.digits;
+        translate_digits = string.maketrans(dig_its,len(dig_its)*' ')
+        editor = TextEditor(None)
+        assert not editor.setup_and_check(translate_digits)
+    else:
+        assert True
+
 
 if __name__ == "__main__":
     pytest.main()