From 3d482d0e41f038f16ea8693fcc3e7a36b2002f6a Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 9 Aug 2020 13:11:54 -0400 Subject: [PATCH 1/3] bpo-41468: Improve and test IDLE run error exit --- Lib/idlelib/idle_test/test_run.py | 30 +++++++++++++++++++++++++++++- Lib/idlelib/run.py | 8 ++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Lib/idlelib/idle_test/test_run.py b/Lib/idlelib/idle_test/test_run.py index 9995dbe2eca502..5586d56610460f 100644 --- a/Lib/idlelib/idle_test/test_run.py +++ b/Lib/idlelib/idle_test/test_run.py @@ -3,7 +3,8 @@ from idlelib import run import unittest from unittest import mock -from test.support import captured_stderr +from idlelib.idle_test.mock_idle import Func +from test.support import captured_output, captured_stderr import io import sys @@ -321,5 +322,32 @@ def func(): "docstring" self.assertEqual(func.__doc__, "more") +class HandleErrorTest(unittest.TestCase): + # Method of MyRPCServer + func = Func() + @mock.patch('run.thread.interrupt_main', new=func) + def test_error(self): + eq = self.assertEqual + with captured_output('__stderr__') as err: + try: + raise EOFError + except EOFError: + run.MyRPCServer.handle_error(None, 'abc', '123') + eq(run.exit_now, True) + run.exit_now = False + eq(err.getvalue(), '') + + try: + raise IndexError + except IndexError: + run.MyRPCServer.handle_error(None, 'abc', '123') + eq(run.quitting, True) + run.quitting = False + msg = err.getvalue() + self.assertIn('abc', msg) + self.assertIn('123', msg) + self.assertIn('IndexError', msg) + + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 5bd84aadcd8011..5c006e8f692324 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -388,12 +388,16 @@ def handle_error(self, request, client_address): except: erf = sys.__stderr__ print('\n' + '-'*40, file=erf) - print('Unhandled server exception!', file=erf) + print('Unhandled exection in user code execution server!', file=erf) print('Thread: %s' % threading.current_thread().name, file=erf) - print('Client Address: ', client_address, file=erf) + print('IDLE Client Address: ', client_address, file=erf) print('Request: ', repr(request), file=erf) traceback.print_exc(file=erf) print('\n*** Unrecoverable, server exiting!', file=erf) + print( +"""Users should never see this; it is likely transient. +If this recurs, report this with a copy of the message +and an explanation of how to make it repeat""", file=erf) print('-'*40, file=erf) quitting = True thread.interrupt_main() From 05fc597a127a9f24171e7219e6dc475f853b5b01 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 9 Aug 2020 13:37:26 -0400 Subject: [PATCH 2/3] More changes. --- Lib/idlelib/idle_test/test_run.py | 6 +++--- Lib/idlelib/run.py | 27 +++++++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Lib/idlelib/idle_test/test_run.py b/Lib/idlelib/idle_test/test_run.py index 89c1cd5f3ae00c..469c13d756d5e8 100644 --- a/Lib/idlelib/idle_test/test_run.py +++ b/Lib/idlelib/idle_test/test_run.py @@ -1,4 +1,4 @@ -"Test run, coverage 42%." +"Test run, coverage 49%." from idlelib import run import unittest @@ -327,7 +327,7 @@ def func(): "docstring" class HandleErrorTest(unittest.TestCase): # Method of MyRPCServer func = Func() - @mock.patch('run.thread.interrupt_main', new=func) + @mock.patch('idlelib.run.thread.interrupt_main', new=func) def test_error(self): eq = self.assertEqual with captured_output('__stderr__') as err: @@ -349,7 +349,7 @@ def test_error(self): self.assertIn('abc', msg) self.assertIn('123', msg) self.assertIn('IndexError', msg) - + eq(self.func.called, 2) if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index 5c006e8f692324..1e84ecc6584ef1 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -387,18 +387,21 @@ def handle_error(self, request, client_address): thread.interrupt_main() except: erf = sys.__stderr__ - print('\n' + '-'*40, file=erf) - print('Unhandled exection in user code execution server!', file=erf) - print('Thread: %s' % threading.current_thread().name, file=erf) - print('IDLE Client Address: ', client_address, file=erf) - print('Request: ', repr(request), file=erf) - traceback.print_exc(file=erf) - print('\n*** Unrecoverable, server exiting!', file=erf) - print( -"""Users should never see this; it is likely transient. -If this recurs, report this with a copy of the message -and an explanation of how to make it repeat""", file=erf) - print('-'*40, file=erf) + print(textwrap.dedent(f""" + {'-'*40} + Unhandled exception in user code execution server!' + Thread: {threading.current_thread().name} + IDLE Client Address: {client_address} + Request: {request!r} + """), file=erf) + traceback.print_exc(limit=-20, file=erf) + print(textwrap.dedent(f""" + *** Unrecoverable, server exiting! + + Users should never see this message; it is likely transient. + If this recurs, report this with a copy of the message + and an explanation of how to make it repeat. + {'-'*40}"""), file=erf) quitting = True thread.interrupt_main() From d4795c5d5dfb6dc37b4d4038ac339f1c8d388450 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Sun, 9 Aug 2020 13:43:27 -0400 Subject: [PATCH 3/3] News items. --- Lib/idlelib/NEWS.txt | 3 +++ Misc/NEWS.d/next/IDLE/2020-08-09-13-42-55.bpo-41468.zkP0_Y.rst | 1 + 2 files changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/IDLE/2020-08-09-13-42-55.bpo-41468.zkP0_Y.rst diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index e0a671983c746d..fd762077b1b3cf 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -3,6 +3,9 @@ Released on 2020-10-05? ====================================== +bpo-41468: Improve IDLE run crash error message (which users should +never see). + bpo-41373: Save files loaded with no line ending, as when blank, or different line endings, by setting its line ending to the system default. Fix regression in 3.8.4 and 3.9.0b4. diff --git a/Misc/NEWS.d/next/IDLE/2020-08-09-13-42-55.bpo-41468.zkP0_Y.rst b/Misc/NEWS.d/next/IDLE/2020-08-09-13-42-55.bpo-41468.zkP0_Y.rst new file mode 100644 index 00000000000000..e41c7d574905cd --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2020-08-09-13-42-55.bpo-41468.zkP0_Y.rst @@ -0,0 +1 @@ +Improve IDLE run crash error message (which users should never see).