printing line numbers for debugging purpose

python MrJean1 at gmail.com
Sun Jan 9 14:42:36 EST 2005


Below is a function to get the current line number and file name.

/Jean Brouwers


-  # dashes added to preserve indentation.
-
- import traceback
-
- def caller(up=0):
-     '''Get file name, line number, function name and
-        source text of the caller's caller as 4-tuple:
-        (file, line, func, text).
-
-        The optional argument 'up' allows retrieval of
-        a caller further back up into the call stack.
-
-        Note, the source text may be None and function
-        name may be '?' in the returned result.  In
-        Python 2.3+ the file name may be an absolute
-        path.
-     '''
-     try:  # just get a few frames
-         f = traceback.extract_stack(limit=up+2)
-         if f:
-            return f[0]
-     except:
-         if __debug__:
-            traceback.print_exc()
-         pass
-      # running with psyco?
-     return ('', 0, '', None)
- 
- 
- if __name__ == '__main__':
-     print caller()
-




More information about the Python-list mailing list