From 0595d1b91952966bfcf3a0607a447528975b6189 Mon Sep 17 00:00:00 2001 From: Romain Boman <r.boman@uliege.be> Date: Mon, 6 Jun 2022 10:50:38 +0200 Subject: [PATCH] better exception handling --- fossils.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/fossils.py b/fossils.py index 2046afa..196ba50 100644 --- a/fossils.py +++ b/fossils.py @@ -63,8 +63,10 @@ class Window(QWidget, Ui_Form): def run_simulation(testname): """Runs a python file """ - - exec(open(testname, encoding='utf-8').read(), + # thanks to the "compile" command, the filename appears in the stack + # trace in case of errors + script = open(testname, encoding='utf-8').read() + exec(compile(script, testname, 'exec'), {'__file__': testname, '__name__':'__main__'}) @@ -155,13 +157,15 @@ if __name__ == "__main__": workspace = win.wrkspLineEdit.text() action = win.action testname = win.inpFileLineEdit.text() + gui = True else: workspace = None action = 'run' testname = os.path.abspath(args.file) + gui = False # run the simulation or display results - + print(f'action = {action}') if action=='run' or action=='post': testname = os.path.normcase(testname) # F:/ => f:/ on Windows @@ -186,7 +190,14 @@ if __name__ == "__main__": tee = cxxfem.Tee('stdout.txt') # split streams - if action == 'run': - run_simulation(testname) - elif action == 'post': - view_results() + try: + if action == 'run': + run_simulation(testname) + elif action == 'post': + view_results() + except Exception as err: + print(f'\n** ERROR: {err}\n') + import traceback + traceback.print_exc() + if gui: + input('\nPress <ENTER> to continue...\n') -- GitLab