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

M

Marek Prerovsky

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
 
P

python

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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top