From 8ad1d6166322e5b4faad880046efc463bd62f58e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20de=20Menten?= Date: Tue, 22 Feb 2022 08:14:34 +0100 Subject: [PATCH 1/2] CLN: use ternary op --- larray_editor/api.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/larray_editor/api.py b/larray_editor/api.py index 29ebe5d..5b9fb9f 100644 --- a/larray_editor/api.py +++ b/larray_editor/api.py @@ -117,11 +117,7 @@ def edit(obj=None, title='', minvalue=None, maxvalue=None, readonly=False, depth qt_app, parent = get_app_and_window("Viewer") caller_frame = sys._getframe(depth + 1) - if display_caller_info: - caller_info = getframeinfo(caller_frame) - else: - caller_info = None - + caller_info = getframeinfo(caller_frame) if display_caller_info else None if obj is None: global_vars = caller_frame.f_globals local_vars = caller_frame.f_locals From a15966e539d053a0ce7100889d8e43a27befc055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20de=20Menten?= Date: Tue, 22 Feb 2022 08:18:24 +0100 Subject: [PATCH 2/2] FEAT: only catch Exception in except hooks (closes #231) ie we shouldn't catch BaseException (KeyboardInterrupt, ...) * run_editor_on_exception was very annoyingly opening the editor to "debug" KeyboardInterrupt exceptions * we could not kill any editor launched by a program (explicitly or via run_editor_on_exception) using the "stop" button in PyCharm (which sends a KeyboardInterrupt exception to the program) --- doc/source/changes/version_0_34.rst.inc | 7 ++++++- larray_editor/api.py | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/doc/source/changes/version_0_34.rst.inc b/doc/source/changes/version_0_34.rst.inc index 4f33a27..cafb1f2 100644 --- a/doc/source/changes/version_0_34.rst.inc +++ b/doc/source/changes/version_0_34.rst.inc @@ -41,4 +41,9 @@ Miscellaneous improvements Fixes ^^^^^ -* fixed something (closes :editor_issue:`1`). +* fixed `run_editor_on_exception` so that the larray editor is not opened when trying to stop a program (via Ctrl-C or + the IDE stop button). Closes :editor_issue:`231`. + +* fixed the larray editor window not closing when trying to stop a program (via `Ctrl-C` or the IDE stop button) with + such a window open (closes :editor_issue:`231`). With PyCharm for example, to actually close the program, the user + had to press the IDE stop button a second time (which killed the program instead of stopping it cleanly). diff --git a/larray_editor/api.py b/larray_editor/api.py index 5b9fb9f..45a6832 100644 --- a/larray_editor/api.py +++ b/larray_editor/api.py @@ -302,6 +302,12 @@ def get_name(i, obj, depth=0): def _qt_except_hook(type, value, tback): # only print the exception and do *not* exit the program traceback.print_exception(type, value, tback) + # only catch simple Exception (avoid catching KeyboardInterrupt, ...) + if not isinstance(value, Exception): + # in a Qt app, the except hook is only called when the window gets the focus again, + # so e.g. if we try to stop an app from PyCharm, it stays alive until we switch + # back to the app window. + sys.exit(1) def install_except_hook(): @@ -368,10 +374,12 @@ def excepthook(type, value, tback): tb_limit = user_tb_length if usercode_traceback else None traceback.print_exception(type, value, main_tb, limit=tb_limit) - stack = extract_tb(main_tb, limit=tb_limit) - stack_pos = user_tb_length - 1 if user_tb_length is not None and usercode_frame else None - print("\nlaunching larray editor to debug...", file=sys.stderr) - _debug(stack, stack_pos=stack_pos) + # open the editor if this is a simple Exception (i.e. not KeyboardInterrupt, ...) + if isinstance(value, Exception): + stack = extract_tb(main_tb, limit=tb_limit) + stack_pos = user_tb_length - 1 if user_tb_length is not None and usercode_frame else None + print("\nlaunching larray editor to debug...", file=sys.stderr) + _debug(stack, stack_pos=stack_pos) return excepthook @@ -385,10 +393,10 @@ def run_editor_on_exception(root_path=None, usercode_traceback=True, usercode_fr root_path : str, optional Defaults to None (the directory of the main script). usercode_traceback : bool, optional - Whether or not to show only the part of the traceback (error log) which corresponds to the user code. + Whether to show only the part of the traceback (error log) which corresponds to the user code. Otherwise, it will show the complete traceback, including code inside libraries. Defaults to True. usercode_frame : bool, optional - Whether or not to start the debug window in the frame corresponding to the user code. + Whether to start the debug window in the frame corresponding to the user code. This argument is ignored (it is always True) if usercode_traceback is True. Defaults to True. Notes