Repository: spyder-ide/spyder

Issue:
UnicodeEncodeError in TextEditor
 
<!---   *** BEFORE SUBMITTING: PASTE CLIPBOARD HERE TO COMPLETE YOUR REPORT ***   ---!>
<!--- **PLEASE READ:** Before submitting here, please carefully consult our *Troubleshooting Guide*: https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ and search the issues page for your error/problem, as most posted bugs are duplicates or easy fixes.

If you don't find anything, please provide a detailed step-by-step description (in English) of the problem and what led up to it below. Issue reports without a clear way to reproduce them will be closed. Thanks! --->

## Problem Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

i open spyder and just Execute code analysis nothing more.

### Traceback
```python-traceback
  File "D:\JRocha\jpgrjr\Anaconda3-1\envs\ArconDev\lib\site-packages\spyder_pylint\widgets\pylintgui.py", line 281, in show_log
    readonly=True, size=(700, 500)).exec_()
  File "D:\JRocha\jpgrjr\Anaconda3-1\envs\ArconDev\lib\site-packages\spyder\widgets\variableexplorer\texteditor.py", line 75, in __init__
    "%s" % (" - "+str(title) if str(title) else ""))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 2: ordinal not in range(128)
```

## Package Versions

* Spyder version: 3.2.8 
* Python version: 2.7.15
* Qt version: 5.6.2
* PyQt5 version: 5.6
* Operating system: Windows 10

### Dependencies

```
pyflakes >=0.5.0  :  1.6.0 (OK)
pycodestyle >=2.3 :  2.4.0 (OK)
pygments >=2.0    :  2.2.0 (OK)
pandas >=0.13.1   :  0.23.0 (OK)
numpy >=1.7       :  1.14.3 (OK)
sphinx >=0.6.6    :  1.7.4 (OK)
rope >=0.9.4      :  0.10.7 (OK)
jedi >=0.9.0      :  0.12.0 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
sympy >=0.7.3     :  1.1.1 (OK)
cython >=0.21     :  0.28.2 (OK)
qtconsole >=4.2.0 :  4.3.1 (OK)
IPython >=4.0;<6.0:  5.7.0 (OK)
pylint >=0.25     :  1.8.4 (OK)
```



Discussion:
Thanks for reporting. We'll take at this error in a future release.
@juanis2112, please work on this one. You need to go to this line:

https://github.com/spyder-ide/spyder/blob/master/spyder/plugins/variableexplorer/widgets/texteditor.py#L85

and

1. Try to convert the `title` arg to unicode with the function `to_text_string`. If that fails, then you need to set `title = u''`. (You can achieve that by enclosing the use of `to_text_string` in a `try/except Exception`).
2. Remove `str(title)` and use the converted `title` from step 1. instead.
3. Prefix all strings in that line with `u`, like this `u"%s"`, etc. That's to make things work in Python 2.

Resolving patch:
diff --git a/spyder/plugins/variableexplorer/widgets/collectionseditor.py b/spyder/plugins/variableexplorer/widgets/collectionseditor.py
--- a/spyder/plugins/variableexplorer/widgets/collectionseditor.py
+++ b/spyder/plugins/variableexplorer/widgets/collectionseditor.py
@@ -49,7 +49,7 @@
 from spyder.config.fonts import DEFAULT_SMALL_DELTA
 from spyder.config.gui import get_font
 from spyder.py3compat import (io, is_binary_string, is_text_string,
-                              PY3, to_text_string)
+                              PY3, to_text_string, is_type_text_string)
 from spyder.utils import icon_manager as ima
 from spyder.utils.misc import getcwd_or_home
 from spyder.utils.qthelpers import (add_actions, create_action,
@@ -332,7 +332,10 @@ def data(self, index, role=Qt.DisplayRole):
         if index.column() == 3:
             display = value_to_display(value, minmax=self.minmax)
         else:
-             display = to_text_string(value)
+            if is_type_text_string(value):
+                display = to_text_string(value, encoding="utf-8")
+            else:
+                display = to_text_string(value)
         if role == Qt.DisplayRole:
             return to_qvariant(display)
         elif role == Qt.EditRole:
diff --git a/spyder/plugins/variableexplorer/widgets/texteditor.py b/spyder/plugins/variableexplorer/widgets/texteditor.py
--- a/spyder/plugins/variableexplorer/widgets/texteditor.py
+++ b/spyder/plugins/variableexplorer/widgets/texteditor.py
@@ -20,7 +20,7 @@
 from spyder.config.base import _
 from spyder.config.gui import get_font
 from spyder.py3compat import (is_binary_string, to_binary_string,
-                              to_text_string)
+                              to_text_string, PY3, PY2)
 from spyder.utils import icon_manager as ima
 
 
@@ -81,8 +81,17 @@ def __init__(self, text, title='', font=None, parent=None,
         self.setWindowFlags(Qt.Window)
         
         self.setWindowIcon(ima.icon('edit'))
+        if title:
+            try:
+                unicode_title = to_text_string(title)
+            except UnicodeEncodeError:
+                unicode_title = u''
+        else:
+            unicode_title = u''
+
         self.setWindowTitle(_("Text editor") + \
-                            "%s" % (" - "+str(title) if str(title) else ""))
+                            u"%s" % (u" - " + unicode_title
+                                     if unicode_title else u""))
         self.resize(size[0], size[1])
 
     @Slot()


Test patch:
diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_texteditor.py b/spyder/plugins/variableexplorer/widgets/tests/test_texteditor.py
--- a/spyder/plugins/variableexplorer/widgets/tests/test_texteditor.py
+++ b/spyder/plugins/variableexplorer/widgets/tests/test_texteditor.py
@@ -23,27 +23,39 @@
 @pytest.fixture
 def texteditor(qtbot):
     """Set up TextEditor."""
-    texteditor = TextEditor(TEXT)
-    qtbot.addWidget(texteditor)
-    return texteditor
+    def create_texteditor(text, **kwargs):
+        editor = TextEditor(text, **kwargs)
+        qtbot.addWidget(editor)
+        return editor
+    return create_texteditor
 
 
 def test_texteditor(texteditor):
     """Run TextEditor dialog."""
-    texteditor.show()
-    assert texteditor
-    dlg_text = texteditor.get_value()
+    editor = texteditor(TEXT)
+    editor.show()
+    assert editor
+    dlg_text = editor.get_value()
     assert TEXT == dlg_text
 
 
 @pytest.mark.skipif(PY3, reason="It makes no sense in Python 3")
-def test_texteditor_setup_and_check():
+def test_texteditor_setup_and_check(texteditor):
     import string
     dig_its = string.digits
     translate_digits = string.maketrans(dig_its,len(dig_its)*' ')
-    editor = TextEditor(None)
+
+    editor = texteditor(None)
     assert not editor.setup_and_check(translate_digits)
 
 
+@pytest.mark.parametrize("title", [u"ñ", u"r"])
+def test_title(texteditor, title):
+    editor = texteditor(TEXT, title=title)
+    editor.show()
+    dlg_title = editor.windowTitle()
+    assert title in dlg_title
+
+
 if __name__ == "__main__":
     pytest.main()