Skip to content

Commit 92a90a3

Browse files
committed
Better traceback handling, version 0.20.3
1 parent 009e5ba commit 92a90a3

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

metakernel/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88

99
__all__ = ['Magic', 'MetaKernel', 'option']
1010

11-
__version__ = '0.20.2'
11+
__version__ = '0.20.3'
1212

1313
del magic, _metakernel, parser, process_metakernel

metakernel/magics/python_magic.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@
2121
PY3 = sys.version_info[0] == 3
2222

2323
def exec_code(code, env, kernel):
24+
import traceback
2425
try:
25-
exec(code, env)
26+
ccode = compile(code, "python cell", "exec")
2627
except Exception as exc:
27-
import traceback
2828
ex_type, ex, tb = sys.exc_info()
29-
return ExceptionWrapper(ex_type.__name__, repr(exc.args), traceback.format_tb(tb))
29+
tb_format = ["%s: %s" % (ex.__class__.__name__, str(ex))]
30+
return ExceptionWrapper(ex_type.__name__, repr(exc.args), tb_format)
31+
try:
32+
exec(ccode, env)
33+
except Exception as exc:
34+
ex_type, ex, tb = sys.exc_info()
35+
line1 = ["Traceback (most recent call last):"]
36+
line2 = ["%s: %s" % (ex.__class__.__name__, str(ex))]
37+
tb_format = line1 + [line.rstrip() for line in traceback.format_tb(tb)[1:]] + line2
38+
return ExceptionWrapper(ex_type.__name__, repr(exc.args), tb_format)
3039
if "retval" in env:
3140
return env["retval"]
3241

@@ -44,7 +53,7 @@ def line_python(self, *args):
4453
This line magic will evaluate the CODE (either expression or
4554
statement) as Python code.
4655
47-
Note that the version of Python is that of the notebook server.
56+
Note that the version of Python is that of the notebook server.
4857
4958
Examples:
5059
%python x = 42
@@ -93,16 +102,16 @@ def cell_python(self, eval_output=False):
93102
variable "retval".
94103
95104
The -e or --eval_output flag signals that the retval value expression
96-
will be used as code for the cell to be evaluated by the host
105+
will be used as code for the cell to be evaluated by the host
97106
language.
98107
99-
Note that the version of Python is that of the notebook server.
108+
Note that the version of Python is that of the notebook server.
100109
101110
Examples:
102-
%%python
111+
%%python
103112
x = 42
104113
105-
%%python
114+
%%python
106115
import math
107116
retval = x + math.pi
108117
@@ -116,7 +125,7 @@ def cell_python(self, eval_output=False):
116125
if self.code.strip():
117126
if eval_output:
118127
self.eval(self.code)
119-
self.code = str(self.env["retval"]) if ("retval" in self.env and
128+
self.code = str(self.env["retval"]) if ("retval" in self.env and
120129
self.env["retval"] != None) else ""
121130
self.retval = None
122131
self.env["retval"] = None
@@ -184,7 +193,7 @@ def get_help_on(self, info, level=0, none_on_fail=False):
184193

185194
if not obj:
186195
return default
187-
196+
188197
strhelp = pydoc.render_doc(obj, "Help on %s")
189198
if level == 0:
190199
return getattr(obj, '__doc__', strhelp)
@@ -194,4 +203,3 @@ def get_help_on(self, info, level=0, none_on_fail=False):
194203

195204
def register_magics(kernel):
196205
kernel.register_magics(PythonMagic)
197-

0 commit comments

Comments
 (0)