New version of tb_lineno(), this time *not* using try-except, to avoid

disturbing the current exception, and returning tb.tb_lineno, which is
the line number of thr traceback, rather than the current line number.
By Jim Hugunin.
This commit is contained in:
Guido van Rossum 1998-02-26 17:25:02 +00:00
parent 2474d68548
commit 6e73af723c
1 changed files with 8 additions and 8 deletions

View File

@ -186,16 +186,16 @@ def extract_stack(f=None, limit = None):
# with -O on). # with -O on).
# Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line() # Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
# in compile.c. # in compile.c.
# Revised version by Jim Hugunin to work with JPython too.
def tb_lineno(tb): def tb_lineno(tb):
f = tb.tb_frame c = tb.tb_frame.f_code
try: if not hasattr(c, 'co_lnotab'):
c = f.f_code return tb.tb_lineno
tab = c.co_lnotab tab = c.co_lnotab
line = c.co_firstlineno line = c.co_firstlineno
stopat = tb.tb_lasti stopat = tb.tb_lasti
except AttributeError:
return f.f_lineno
addr = 0 addr = 0
for i in range(0, len(tab), 2): for i in range(0, len(tab), 2):
addr = addr + ord(tab[i]) addr = addr + ord(tab[i])