Skip to content

Commit 9ce042b

Browse files
committed
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)
1 parent 1d4f3d0 commit 9ce042b

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

doc/source/changes/version_0_34.rst.inc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ Miscellaneous improvements
4141
Fixes
4242
^^^^^
4343

44-
* fixed something (closes :editor_issue:`1`).
44+
* fixed `run_editor_on_exception` so that the larray editor is not opened when trying to stop a program (via Ctrl-C or
45+
the IDE stop button). Closes :editor_issue:`231`.
46+
47+
* fixed the larray editor window not closing when trying to stop a program (via `Ctrl-C` or the IDE stop button) with
48+
such a window open (closes :editor_issue:`231`). With PyCharm for example, to actually close the program, the user
49+
had to press the IDE stop button a second time (which killed the program instead of stopping it cleanly).

larray_editor/api.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,12 @@ def get_name(i, obj, depth=0):
302302
def _qt_except_hook(type, value, tback):
303303
# only print the exception and do *not* exit the program
304304
traceback.print_exception(type, value, tback)
305+
# only catch simple Exception (avoid catching KeyboardInterrupt, ...)
306+
if not isinstance(value, Exception):
307+
# in a Qt app, the except hook is only called when the window gets the focus again,
308+
# so e.g. if we try to stop an app from PyCharm, it stays alive until we switch
309+
# back to the app window.
310+
sys.exit(1)
305311

306312

307313
def install_except_hook():
@@ -368,10 +374,12 @@ def excepthook(type, value, tback):
368374
tb_limit = user_tb_length if usercode_traceback else None
369375
traceback.print_exception(type, value, main_tb, limit=tb_limit)
370376

371-
stack = extract_tb(main_tb, limit=tb_limit)
372-
stack_pos = user_tb_length - 1 if user_tb_length is not None and usercode_frame else None
373-
print("\nlaunching larray editor to debug...", file=sys.stderr)
374-
_debug(stack, stack_pos=stack_pos)
377+
# open the editor if this is a simple Exception (i.e. not KeyboardInterrupt, ...)
378+
if isinstance(value, Exception):
379+
stack = extract_tb(main_tb, limit=tb_limit)
380+
stack_pos = user_tb_length - 1 if user_tb_length is not None and usercode_frame else None
381+
print("\nlaunching larray editor to debug...", file=sys.stderr)
382+
_debug(stack, stack_pos=stack_pos)
375383

376384
return excepthook
377385

@@ -385,10 +393,10 @@ def run_editor_on_exception(root_path=None, usercode_traceback=True, usercode_fr
385393
root_path : str, optional
386394
Defaults to None (the directory of the main script).
387395
usercode_traceback : bool, optional
388-
Whether or not to show only the part of the traceback (error log) which corresponds to the user code.
396+
Whether to show only the part of the traceback (error log) which corresponds to the user code.
389397
Otherwise, it will show the complete traceback, including code inside libraries. Defaults to True.
390398
usercode_frame : bool, optional
391-
Whether or not to start the debug window in the frame corresponding to the user code.
399+
Whether to start the debug window in the frame corresponding to the user code.
392400
This argument is ignored (it is always True) if usercode_traceback is True. Defaults to True.
393401
394402
Notes

0 commit comments

Comments
 (0)