From 1720db308bf4481d6be45d4f7f611bab576b1184 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Fri, 18 Sep 2015 13:53:58 +0200 Subject: Simplify exceptions returned by PythonSession.run Don't include the first stack entry or the filename (which is ). --- server/python_session.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'server') diff --git a/server/python_session.py b/server/python_session.py index 33fe7cc..62fcbf8 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -172,9 +172,17 @@ def _run_exec(conn, code, expr=None, stdin=''): if expr: result = eval(expr, env) except Exception as ex: - # Exception is not JSON serializable, so return traceback as string. + # Exception is not JSON serializable, so return traceback as string + # (without the first entry, which is this function). import traceback - exc = traceback.format_exc() + e_type, e_value, e_tb = sys.exc_info() + stack = traceback.extract_tb(e_tb) + exc = ''.join( + ['Traceback (most recent call last):\n'] + + [' line {}, in {}\n'.format(lineno, name) + (line+'\n' if line else '') + for filename, lineno, name, line in stack[1:]] + + traceback.format_exception_only(e_type, e_value) + ).rstrip() finally: out = sys.stdout.getvalue() err = sys.stderr.getvalue() -- cgit v1.2.1