How to print absolute line number in uncaught exception?
Question: When error occured Python prints something like this:

    
    
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "<stdin>", line 8, in m
      File "<stdin>", line 5, in exec_st
      File "<stdin>", line 9, in exec_assign
      File "<stdin>", line 48, in ref_by_id
    IndexError: list index out of range
    

where 2, ... , 48 are relative line numbers which are not very convenient. How
to print absolute line numbers in such error messages?

EDIT: Maybe it's a dumb question, but answer will facilitate development a
little. I'm printing text in several files. When done, press shortcut which
runs python and copies contents of current file to console. Proposed solution
forces to press excess keystrokes (Ctrl+S, Alt+Tab) and create additional
files. I hope I have put it clear.


Answer: A few minutes of hacking around gives me this Read-Eval-Print Loop in Python
2.7:

    
    
    #!/usr/bin/env python
    import code
    import sys
    
    LINE_NUMBER=0
    
    def reset_linenum():
        global LINE_NUMBER
        LINE_NUMBER=-1
    
    def resettable_REPL():
        global LINE_NUMBER
        BUFFERED_LINES=[]
        ii=code.InteractiveInterpreter({"reset_linenum":reset_linenum})
        while True:
            try:
                BUFFERED_LINES.append(raw_input("!"+sys.ps1)+'\n')
                while (BUFFERED_LINES[-1][:1] in " \t" or
                       ii.runsource("\n"*LINE_NUMBER+"".join(BUFFERED_LINES), "console")):
                    BUFFERED_LINES.append(raw_input("!"+sys.ps2)+'\n')
                LINE_NUMBER+=len(BUFFERED_LINES)
                BUFFERED_LINES=[]
            except EOFError:
                break
    
    if __name__=='__main__':
        resettable_REPL()
    

It's a bit hackish, but it keeps track of line numbers as the session
continues, and allows the current count to be reset by a call to
`reset_linenum()`. An example session:

    
    
    !!!> print "hello"              # line 1
    hello
    !!!> def badfunc():             # line 2
    !...     raise Exception("doh") # line 3
    !!!> badfunc()                  # line 4
    Traceback (most recent call last):
      File "console", line 4, in <module>
      File "console", line 3, in badfunc
    Exception: doh
    !!!> ?                          # line 5
      File "console", line 5
        ?
        ^
    SyntaxError: invalid syntax
    !!!> reset_linenum()            # RESET
    !!!> raise Exception("!")       # line 1
    Traceback (most recent call last):
      File "console", line 1, in <module>
    Exception: !
    !!!> 
    

**EDIT** : Fixed REPL to not execute indented blocks quite so prematurely.

