Skip to content
Snippets Groups Projects
Commit aa6cf127 authored by Martyn Gigg's avatar Martyn Gigg
Browse files

Decode editor error byte strings as utf-8

The rest of the framework expects unicode objects and
uses unicode_literals in Python 2.
parent 78fe973a
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,9 @@ from __future__ import (absolute_import, unicode_literals)
# std imports
import traceback
# third-party imports
import six
class ErrorFormatter(object):
"""Formats errors to strings"""
......@@ -30,4 +33,10 @@ class ErrorFormatter(object):
lines = traceback.format_exception_only(exc_type, exc_value)
if stack is not None:
lines.extend(traceback.format_list(stack))
if six.PY2:
# traceback always returns a list of ascii string objects
# encoded as utf-8 we want unicode to be consistent
# with using unicode_literals across the codebase
lines = map(lambda x: x.decode('utf-8') , lines)
return ''.join(lines)
# -*- coding: utf-8 -*-
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2017 ISIS Rutherford Appleton Laboratory UKRI,
......@@ -13,6 +14,9 @@ import sys
import traceback
import unittest
# third-party imports
import six
# local imports
from mantidqt.widgets.codeeditor.errorformatter import ErrorFormatter
......@@ -67,6 +71,24 @@ foo()
for produced, expected in zip(error_lines, expected_lines):
self.assertRegexpMatches(produced, expected)
def test_errors_containing_unicode_produce_expected_value_in_python2(self):
if not six.PY2:
# everything is already unicode in python > 2
return
try:
exec("é =")
except SyntaxError:
exc_type, exc_value = sys.exc_info()[:2]
formatter = ErrorFormatter()
error = formatter.format(exc_type, exc_value, None)
expected = """ File "<string>", line 1
é =
^
SyntaxError: invalid syntax
"""
self.assertEqual(expected, error)
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment