Determining caller's file and line number
Jean Brouwers
mrjean1 at comcast.net
Wed Jun 23 19:28:31 EDT 2004
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 <8a638f47.0406231330.7d59152f at posting.google.com>, David
Abrahams <dave at 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
More information about the Python-list
mailing list