From aa6cf127b18f4a86f702ea79ba94f5c3669df0ab Mon Sep 17 00:00:00 2001
From: Martyn Gigg <martyn.gigg@gmail.com>
Date: Tue, 13 Nov 2018 13:33:33 +0000
Subject: [PATCH] Decode editor error byte strings as utf-8

The rest of the framework expects unicode objects and
uses unicode_literals in Python 2.
---
 .../widgets/codeeditor/errorformatter.py      |  9 ++++++++
 .../codeeditor/test/test_errorformatter.py    | 22 +++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/qt/python/mantidqt/widgets/codeeditor/errorformatter.py b/qt/python/mantidqt/widgets/codeeditor/errorformatter.py
index 506a26233eb..059cd7979ba 100644
--- a/qt/python/mantidqt/widgets/codeeditor/errorformatter.py
+++ b/qt/python/mantidqt/widgets/codeeditor/errorformatter.py
@@ -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)
diff --git a/qt/python/mantidqt/widgets/codeeditor/test/test_errorformatter.py b/qt/python/mantidqt/widgets/codeeditor/test/test_errorformatter.py
index 323a16e4718..53e33527051 100644
--- a/qt/python/mantidqt/widgets/codeeditor/test/test_errorformatter.py
+++ b/qt/python/mantidqt/widgets/codeeditor/test/test_errorformatter.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 # Mantid Repository : https://github.com/mantidproject/mantid
 #
 # Copyright &copy; 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()
-- 
GitLab