summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimotej Lazar <timotej.lazar@araneo.org>2015-09-18 13:53:58 +0200
committerTimotej Lazar <timotej.lazar@araneo.org>2015-09-18 13:53:58 +0200
commit1720db308bf4481d6be45d4f7f611bab576b1184 (patch)
tree1bb1d0b6fef29f16aa7e2f646fe1e19330be0f01
parentdcdfe0563ab269807f66304107f9123cfc2dd37e (diff)
Simplify exceptions returned by PythonSession.run
Don't include the first stack entry or the filename (which is <string>).
-rw-r--r--server/python_session.py12
1 files changed, 10 insertions, 2 deletions
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()