Skip to content

Commit

Permalink
Merge from 3.x: PR #4369
Browse files Browse the repository at this point in the history
Fixes #4191
  • Loading branch information
ccordoba12 committed Apr 19, 2017
2 parents 9ba55e9 + 2fcb55f commit 14a7a3b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
18 changes: 18 additions & 0 deletions spyder/plugins/tests/test_ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Licensed under the terms of the MIT License
#

import codecs
import os
import os.path as osp
import shutil
Expand Down Expand Up @@ -55,6 +56,23 @@ def close_widget():
#==============================================================================
# Tests
#==============================================================================
def test_read_stderr(ipyconsole, qtbot):
"""
Test the read operation of the stderr file of the kernel
"""

shell = ipyconsole.get_current_shellwidget()
client = ipyconsole.get_current_client()
qtbot.waitUntil(lambda: shell._prompt_html is not None, timeout=SHELL_TIMEOUT)

# Set contents of the stderr file of the kernel
content = 'Test text'
stderr_file = client.stderr_file
codecs.open(stderr_file, 'w', 'cp437').write(content)
# Assert that content is correct
assert content == client._read_stderr()


@flaky(max_runs=10)
@pytest.mark.skipif(os.name == 'nt', reason="It times out on Windows")
def test_run_doctest(ipyconsole, qtbot):
Expand Down
21 changes: 20 additions & 1 deletion spyder/widgets/ipythonconsole/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
from spyder.config.gui import get_font, get_shortcut
from spyder.utils import icon_manager as ima
from spyder.utils import sourcecode
from spyder.utils.encoding import get_coding
from spyder.utils.programs import TEMPDIR
from spyder.utils.qthelpers import (add_actions, create_action,
create_toolbutton)
from spyder.py3compat import to_text_string
from spyder.widgets.browser import WebView
from spyder.widgets.mixins import SaveHistoryMixin
from spyder.widgets.ipythonconsole import ShellWidget
Expand Down Expand Up @@ -378,7 +380,17 @@ def restart_kernel(self):
@Slot(str)
def kernel_restarted_message(self, msg):
"""Show kernel restarted/died messages."""
stderr = codecs.open(self.stderr_file, 'r', encoding='utf-8').read()
try:
stderr = codecs.open(self.stderr_file, 'r',
encoding='utf-8').read()
except UnicodeDecodeError:
# This is needed since the stderr file could be encoded
# in something different to utf-8.
# See issue 4191
try:
stderr = self._read_stderr()
except:
stderr = None

if stderr:
self.show_kernel_error('<tt>%s</tt>' % stderr)
Expand Down Expand Up @@ -438,3 +450,10 @@ def _hide_loading_page(self):

document = self.get_control().document()
document.contentsChange.disconnect(self._hide_loading_page)

def _read_stderr(self):
"""Read the stderr file of the kernel."""
stderr_text = open(self.stderr_file, 'rb').read()
encoding = get_coding(stderr_text)
stderr = to_text_string(stderr_text, encoding)
return stderr

0 comments on commit 14a7a3b

Please sign in to comment.