X Tutup
Skip to content

Commit 4b28b40

Browse files
committed
refactor the traceback offset hacking to make it orthogonal
1 parent 9cd8b78 commit 4b28b40

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

bpython/cli.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,14 @@ def __init__(self):
188188
necessarily must be with the current factoring) and then an exception
189189
callback can be added to the Interpeter instance afterwards - more
190190
specifically, this is so that autoindentation does not occur after a
191-
traceback."""
191+
traceback.
192+
193+
Interpreter.tblist_hook can be a function that will receive the tblist
194+
from showtraceback() (after it has been modified to retain only the
195+
last element) as the only argument and should mutate it in place."""
192196

193197
self.syntaxerror_callback = None
198+
self.tblist_hook = None
194199
# Unfortunately code.InteractiveInterpreter is a classic class, so no super()
195200
code.InteractiveInterpreter.__init__(self)
196201

@@ -229,6 +234,8 @@ def showtraceback(self):
229234
sys.last_traceback = tb
230235
tblist = traceback.extract_tb(tb)
231236
del tblist[:1]
237+
if self.tblist_hook is not None:
238+
self.tblist_hook(tblist)
232239

233240
l = traceback.format_list(tblist)
234241
if l:
@@ -327,6 +334,8 @@ def __init__(self, scr, interp, statusbar=None, idle=None):
327334
self.paste_time = 0.02
328335
sys.path.insert(0, '.')
329336

337+
self.interp.tblist_hook = self.fix_traceback_offset
338+
330339
if not OPTS.arg_spec:
331340
return
332341

@@ -336,6 +345,14 @@ def __init__(self, scr, interp, statusbar=None, idle=None):
336345
'ignore') as hfile:
337346
self.rl_hist = hfile.readlines()
338347

348+
def fix_traceback_offset(self, tblist):
349+
"""Will be assigned to interpreter.tblist_hook and, if the interpreter
350+
supports it, will be called when the tblist is created and modified to
351+
contain only the last value. This is basically a little hack to fix the
352+
line number offset for the traceback due to us inserting the encoding
353+
header into the interpreter."""
354+
tblist[0] = (tblist[0][0], 1) + tblist[0][2:]
355+
339356
def clean_object(self, obj):
340357
"""Try to make an object not exhibit side-effects on attribute
341358
lookup. Return the type's magic attributes so they can be reapplied

0 commit comments

Comments
 (0)
X Tutup