def _frame_lineno(frame):
    """
    Calculate correct line number of stack frame given in frame.
    """
    code = frame.f_code
    if not hasattr(code, 'co_lnotab'):
        return frame.f_lineno

    tab = code.co_lnotab
    line = code.co_firstlineno
    stopat = frame.f_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