From 4e8ac63408505759bbaacc52b8e8c60615d20ea9 Mon Sep 17 00:00:00 2001 From: Jean-Paul Chaput Date: Fri, 3 Nov 2017 15:41:29 +0100 Subject: [PATCH] In CRL Core Python helpers, new showPythonTrace(). * New: In CRL Core Python helpers, add a "showPythonTrace()" function to custom display the Python stack trace in case of exception. It ha been made to look like a gdb trace. * In Unicorn, cgt.py, use showPythonTrace(). --- crlcore/python/helpers/__init__.py | 34 ++++++++++++++++++++++++++---- unicorn/src/cgt.py | 8 ++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/crlcore/python/helpers/__init__.py b/crlcore/python/helpers/__init__.py index b44cd57d..6f076f1d 100644 --- a/crlcore/python/helpers/__init__.py +++ b/crlcore/python/helpers/__init__.py @@ -47,9 +47,35 @@ def isderived ( derived, base ): return False -def truncPath ( path ): - if len(path) < 70: return path - return '.../'+os.sep.join(path.split(os.sep)[-4:]) +def truncPath ( path, maxlength=70 ): + if len(path) < maxlength: return path + return '.../'+os.sep.join(path.split(os.sep)[-4:]) + + +def showPythonTrace ( scriptPath=None, e=None, tryContinue=True ): + if scriptPath: + print '[ERROR] An exception occured while loading the Python script module:' + print ' \"%s\"\n' % (scriptPath) + print ' You should check for simple python errors in this module.\n' + + print ' Python stack trace:' + trace = traceback.extract_tb( sys.exc_info()[2] ) + maxdepth = len( trace ) + for depth in range( maxdepth ): + filename, line, function, code = trace[ maxdepth-depth-1 ] + if len(filename) > 38: + filename = filename[-38:] + filename = '.../' + filename[ filename.find('/')+1 : ] + #print ' [%02d] %45s:%-5d in \"%s()\"' % ( maxdepth-depth-1, filename, line, function ) + print ' #%d in %25s() at %s:%d' % ( depth, function, filename, line ) + + if e: + print ' Error was:' + print ' %s\n' % e + + if tryContinue: + print ' Trying to continue anyway...' + return class ErrorMessage ( Exception ): @@ -126,7 +152,7 @@ class ErrorMessage ( Exception ): ewrap = e if footer: ewrap.addMessage(footer) print ewrap - if showTrace: traceback.print_tb(sys.exc_info()[2]) + if showTrace: showPythonTrace() return diff --git a/unicorn/src/cgt.py b/unicorn/src/cgt.py index 865fb900..73330b72 100755 --- a/unicorn/src/cgt.py +++ b/unicorn/src/cgt.py @@ -8,6 +8,7 @@ try: import Cfg import Hurricane import Viewer + from helpers import showPythonTrace import CRL import Anabatic import Katana @@ -85,12 +86,7 @@ def runScript ( scriptPath, editor ): print ' Error was:' print ' %s\n' % e except Exception, e: - print '[ERROR] An exception occured while loading the Stratus script module:' - print ' <%s>\n' % (scriptPath) - print ' You should check for simple python errors in this module.' - print ' Error was:' - print ' %s\n' % e - print ' Trying to continue anyway...' + showPythonTrace( scriptPath, e ) return