Determining caller's file and line number

Discussion in 'Python' started by David Abrahams, Jun 23, 2004.

  1. 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
    http://www.boost-consulting.com
     
    David Abrahams, Jun 23, 2004
    #1
    1. Advertising

  2. Here is one example:

    <pre>

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

    </pre>

    /Jean Brouwers
    ProphICy Semiconductor, Inc.



    In article <>, David
    Abrahams <> 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
    > http://www.boost-consulting.com
     
    Jean Brouwers, Jun 24, 2004
    #2
    1. Advertising

  3. Jean Brouwers <> wrote in message news:<230620041639365943%>...
    > Here is one example:


    Fantastic! Thank you so much!

    --
    David Abrahams
    Boost Consulting
    http://www.boost-consulting.com
     
    David Abrahams, Jun 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne
    Replies:
    3
    Views:
    1,774
    vMike
    Nov 17, 2003
  2. Hugo
    Replies:
    10
    Views:
    1,368
    Matt Humphrey
    Oct 18, 2004
  3. Jim Cobban
    Replies:
    0
    Views:
    418
    Jim Cobban
    Nov 15, 2003
  4. Mark
    Replies:
    2
    Views:
    412
  5. Replies:
    4
    Views:
    290
Loading...

Share This Page