How can I get the source file name and current line number insideexecuted C-function

Discussion in 'Python' started by Marek Prerovsky, Feb 16, 2005.

  1. Hello,

    I implemented some Python functions in my C/C++ code. I need to know the Python source file name and line number of just executed
    Python command which calls my function.
    How can I get this information inside my C/C++ function?

    Thanks for any help.

    Marek
    Marek Prerovsky, Feb 16, 2005
    #1
    1. Advertising

  2. Marek Prerovsky

    python Guest

    Re: How can I get the source file name and current line number inside executed C-function

    Below is a function to find the caller's file name, line number, etc.
    inside Python. Maybe this works for your case.

    /Jean Brouwers


    - 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()



    Marek Prerovsky wrote:
    > Hello,
    >
    > I implemented some Python functions in my C/C++ code. I need to know

    the Python source file name and line number of just executed
    > Python command which calls my function.
    > How can I get this information inside my C/C++ function?
    >
    > Thanks for any help.
    >
    > Marek
    python, Feb 16, 2005
    #2
    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. Hugo
    Replies:
    10
    Views:
    1,288
    Matt Humphrey
    Oct 18, 2004
  2. scad
    Replies:
    23
    Views:
    1,129
    Alf P. Steinbach
    May 17, 2009
  3. Peng Yu
    Replies:
    0
    Views:
    290
    Peng Yu
    Nov 25, 2009
  4. mani
    Replies:
    16
    Views:
    2,265
  5. Nick Weavers
    Replies:
    4
    Views:
    119
Loading...

Share This Page