469,610 Members | 2,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Determining caller's file and line number

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
Jul 18 '05 #1
2 2778

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 <8a**************************@posting.google.com >, David
Abrahams <da**@boost-consulting.com> 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

Jul 18 '05 #2
Jean Brouwers <mr*****@comcast.net> wrote in message news:<230620041639365943%mr*****@comcast.net>...
Here is one example:


Fantastic! Thank you so much!

--
David Abrahams
Boost Consulting
http://www.boost-consulting.com
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by ivan_oj28 | last post: by
66 posts views Thread by Darren Dale | last post: by
2 posts views Thread by Luca | last post: by
12 posts views Thread by johny smith | last post: by
3 posts views Thread by Fred Nelson | last post: by
5 posts views Thread by Bob Day | last post: by
14 posts views Thread by googler | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.