eval errors tracing

Skip Montanaro skip at pobox.com
Wed Aug 22 08:24:40 EDT 2001


    Sergey> I don't want to get errors printed as the standard tools do. I
    Sergey> want to get the line number and do some complicated
    Sergey> manupulations with it (they are really more than just
    Sergey> string.zfill(lineno, 4)). For now I use a dirty hack: get
    Sergey> str(exc_info()[1]), then find by a regexp the line number, then
    Sergey> do what I want.  But I consider dirty hacks aren't the best
    Sergey> solutions.

Check out the print_tb code in Lib/traceback.py.  It doesn't use dirty hacks
to get the line number.  It extracts them from the relevant code object
using traceback.tb_lineno():

    n = 0
    while tb is not None and (limit is None or n < limit):
        f = tb.tb_frame
        lineno = tb_lineno(tb)                  # <--- here
        ...
        tb = tb.tb_next
        n = n+1

use-the-source-luke-ly y'rs,

-- 
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list