Much better exception handling - Have them make more sense. Get rid of the multiple traceback prints, one for each level of catchs on python3. Also improve traceback on both python3 and python2 to exclude the function wrapper, and instead follow the execution frame. Downside is that neither form compiles on the other python, so have to use exec...

This commit is contained in:
Tim Savannah 2017-05-24 02:01:58 -04:00
parent fd0260fc05
commit 76cf4f6456

View File

@ -61,9 +61,14 @@ def func_timeout(timeout, func, args=(), kwargs=None):
# Don't print traceback to stderr if we time out # Don't print traceback to stderr if we time out
pass pass
except Exception as e: except Exception as e:
exc_info = sys.exc_info()
if isStopped is False: if isStopped is False:
# Don't capture stopping exception # Assemble the alternate traceback, excluding this function
exception.append(e) # from the trace (by going to next frame)
# Pytohn3 reads native from __traceback__,
# python2 has a different form for "raise"
e.__traceback__ = exc_info[2].tb_next
exception.append( e )
thread = StoppableThread(target=funcwrap, args=(args, kwargs)) thread = StoppableThread(target=funcwrap, args=(args, kwargs))
thread.daemon = True thread.daemon = True
@ -96,9 +101,10 @@ def func_timeout(timeout, func, args=(), kwargs=None):
# This, in effect, prevents the "funcwrap" wrapper ( chained # This, in effect, prevents the "funcwrap" wrapper ( chained
# in context to an exception raised here, due to scope ) # in context to an exception raised here, due to scope )
# Only available in python3.3+ # Only available in python3.3+
exec('raise exception[0] from None', None, { 'exception' : exception }) exec('raise exception[0] from None', None, { 'exception' : exception, })
else: else:
raise exception[0] # Python2 allows specifying an alternate traceback.
exec('raise exception[0] , None, exception[0].__traceback__', None, {'exception' : exception})
if ret: if ret:
return ret[0] return ret[0]