##// END OF EJS Templates
Merge pull request #12758 from takuya1981/Fix-issue-#11615--handle-UnicodeEncodeError-when-print-stack-trace...
Matthias Bussonnier -
r26462:e783d238 merge
parent child Browse files
Show More
@@ -2085,13 +2085,17 b' class InteractiveShell(SingletonConfigurable):'
2085 except KeyboardInterrupt:
2085 except KeyboardInterrupt:
2086 print('\n' + self.get_exception_only(), file=sys.stderr)
2086 print('\n' + self.get_exception_only(), file=sys.stderr)
2087
2087
2088 def _showtraceback(self, etype, evalue, stb):
2088 def _showtraceback(self, etype, evalue, stb: str):
2089 """Actually show a traceback.
2089 """Actually show a traceback.
2090
2090
2091 Subclasses may override this method to put the traceback on a different
2091 Subclasses may override this method to put the traceback on a different
2092 place, like a side channel.
2092 place, like a side channel.
2093 """
2093 """
2094 print(self.InteractiveTB.stb2text(stb))
2094 val = self.InteractiveTB.stb2text(stb)
2095 try:
2096 print(val)
2097 except UnicodeEncodeError:
2098 print(val.encode("utf-8", "backslashreplace").decode())
2095
2099
2096 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2100 def showsyntaxerror(self, filename=None, running_compiled_code=False):
2097 """Display the syntax error that just occurred.
2101 """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