Determining caller's file and line number

Jean Brouwers mrjean1 at
Thu Jun 24 01:28:31 CEST 2004

Here is one example:


from traceback import extract_stack as tb_extract_stack,
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
    try:  # just get a few frames
        f = tb_extract_stack(limit=up+2)
        if f:
           return f[0]
     # running with psyco?
    return ('', 0, '', None)


/Jean Brouwers
 ProphICy Semiconductor, Inc.

In article <8a638f47.0406231330.7d59152f at>, David
Abrahams <dave at> wrote:

> Is there any way to determine the file and line number (if any) of a
> method's invocation from within its body?
> Many Thanks in advance,
> Dave
> --
> David Abrahams
> Boost Consulting

More information about the Python-list mailing list