##// END OF EJS Templates
Backport PR #12758: Fix issue #11615 handle unicode encode error when print stack trace
Matthias Bussonnier -
Show More
@@ -2075,13 +2075,17 b' class InteractiveShell(SingletonConfigurable):'
2075 except KeyboardInterrupt:
2075 except KeyboardInterrupt:
2076 print('\n' + self.get_exception_only(), file=sys.stderr)
2076 print('\n' + self.get_exception_only(), file=sys.stderr)
2077
2077
2078 def _showtraceback(self, etype, evalue, stb):
2078 def _showtraceback(self, etype, evalue, stb: str):
2079 """Actually show a traceback.
2079 """Actually show a traceback.
2080
2080
2081 Subclasses may override this method to put the traceback on a different
2081 Subclasses may override this method to put the traceback on a different
2082 place, like a side channel.
2082 place, like a side channel.
2083 """
2083 """
2084 print(self.InteractiveTB.stb2text(stb))
2084 val = self.InteractiveTB.stb2text(stb)
2085 try:
2086 print(val)
2087 except UnicodeEncodeError:
2088 print(val.encode("utf-8", "backslashreplace").decode())
2085
2089
2086 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2090 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2087 """Display the syntax error that just occurred.
2091 """Display the syntax error that just occurred.
@@ -449,6 +449,25 b' class InteractiveShellTestCase(unittest.TestCase):'
449 # Reset the custom exception hook
449 # Reset the custom exception hook
450 ip.set_custom_exc((), None)
450 ip.set_custom_exc((), None)
451
451
452 @mock.patch("builtins.print")
453 def test_showtraceback_with_surrogates(self, mocked_print):
454 values = []
455
456 def mock_print_func(value, sep=" ", end="\n", file=sys.stdout, flush=False):
457 values.append(value)
458 if value == chr(0xD8FF):
459 raise UnicodeEncodeError("utf-8", chr(0xD8FF), 0, 1, "")
460
461 # mock builtins.print
462 mocked_print.side_effect = mock_print_func
463
464 # ip._showtraceback() is replaced in globalipapp.py.
465 # Call original method to test.
466 interactiveshell.InteractiveShell._showtraceback(ip, None, None, chr(0xD8FF))
467
468 self.assertEqual(mocked_print.call_count, 2)
469 self.assertEqual(values, [chr(0xD8FF), "\\ud8ff"])
470
452 def test_mktempfile(self):
471 def test_mktempfile(self):
453 filename = ip.mktempfile()
472 filename = ip.mktempfile()
454 # Check that we can open the file again on Windows
473 # Check that we can open the file again on Windows
General Comments 0
You need to be logged in to leave comments. Login now