def tb_lineno(tb):

    """ Calculate the correct line number of the
        traceback given in tb (even with -O on)
    """
    c = tb.tb_frame.f_code
    tab = c.co_lnotab
    line = c.co_firstlineno
    stopat = tb.tb_lasti
    addr = 0
    for i in range(0,len(tab),2):
        addr = addr + ord(tab[i])
        if addr > stopat:
            break
        line = line + ord(tab[i+1])
    return line