Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

filename of calling function?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


phlip2005 at gmail

Nov 28, 2009, 8:19 AM

Post #1 of 6 (267 views)
Permalink
filename of calling function?

Consider these two python modules:

aa.py

def a():
print '?'

bb.py
import aa

def bb():
aa.a()

bb()

How do I make the print line emit the filename of bb.py? (It could be
anything.)

I am currently playing with sys.exc_info, but it seems to only emit
the stack between the raise and the except. Could be pilot error, of
course. Thanks for any help!

--
Phlip
http://c2.com/cgi/wiki?ZeekLand
--
http://mail.python.org/mailman/listinfo/python-list


phlip2005 at gmail

Nov 28, 2009, 8:40 AM

Post #2 of 6 (246 views)
Permalink
Re: filename of calling function? [In reply to]

On Nov 28, 8:19 am, Phlip <phlip2...@gmail.com> wrote:
> Consider these two python modules:
>
> aa.py
>
> def a():
>     print '?'
>
> bb.py
>   import aa
>
> def bb():
>   aa.a()
>
> bb()
>
> How do I make the print line emit the filename of bb.py? (It could be
> anything.)

try:
raise None
except:
import sys
from traceback import extract_tb, extract_stack
frame = sys.exc_info()[2].tb_frame.f_back
calling_file = extract_stack(frame, 2)[1][0]
--
http://mail.python.org/mailman/listinfo/python-list


callmeclaudius at gmail

Nov 28, 2009, 9:04 AM

Post #3 of 6 (239 views)
Permalink
Re: filename of calling function? [In reply to]

On Nov 28, 11:40 am, Phlip <phlip2...@gmail.com> wrote:
> On Nov 28, 8:19 am, Phlip <phlip2...@gmail.com> wrote:
>
>
>
> > Consider these two python modules:
>
> > aa.py
>
> > def a():
> >     print '?'
>
> > bb.py
> >   import aa
>
> > def bb():
> >   aa.a()
>
> > bb()
>
> > How do I make the print line emit the filename of bb.py? (It could be
> > anything.)
>
>         try:
>             raise None
>         except:
>             import sys
>             from traceback import extract_tb, extract_stack
>             frame = sys.exc_info()[2].tb_frame.f_back
>             calling_file = extract_stack(frame, 2)[1][0]

code works perfectly except on my system both the indexes need to be 0
(eg: "extract_stack(frame, 2)[0][0]")
--
http://mail.python.org/mailman/listinfo/python-list


phlip2005 at gmail

Nov 28, 2009, 9:37 AM

Post #4 of 6 (246 views)
Permalink
Re: filename of calling function? [In reply to]

On Nov 28, 9:04 am, Joel Davis <callmeclaud...@gmail.com> wrote:

> >         try:
> >             raise None
> >         except:
> >             import sys
> >             from traceback import extract_tb, extract_stack
> >             frame = sys.exc_info()[2].tb_frame.f_back
> >             calling_file = extract_stack(frame, 2)[1][0]
>
> code works perfectly except on my system both the indexes need to be 0
> (eg: "extract_stack(frame, 2)[0][0]")

I thought the 0 was myself, 1 my caller, etc. But tx for trying it
--
http://mail.python.org/mailman/listinfo/python-list


grflanagan at gmail

Nov 28, 2009, 12:14 PM

Post #5 of 6 (237 views)
Permalink
Re: filename of calling function? [In reply to]

Phlip wrote:
> Consider these two python modules:
>
> aa.py
>
> def a():
> print '?'
>
> bb.py
> import aa
>
> def bb():
> aa.a()
>
> bb()
>
> How do I make the print line emit the filename of bb.py? (It could be
> anything.)
>

Possibly not very reliable in every situation (doctests, other pythons,
...) but this is what I do:


--------- aa.py --------------
import __main__ as CALLER

def mynameis():
print CALLER.__file__

--------- bb.py --------------

import aa

def whosthere():
aa.mynameis()

whosthere()

-------------------------------

OUTPUT: bb.py


hth

G.F.

--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 28, 2009, 5:05 PM

Post #6 of 6 (231 views)
Permalink
Re: filename of calling function? [In reply to]

On Sat, 28 Nov 2009 08:19:02 -0800, Phlip wrote:

> Consider these two python modules:
>
> aa.py
>
> def a():
> print '?'
>
> bb.py
> import aa
>
> def bb():
> aa.a()
>
> bb()
>
> How do I make the print line emit the filename of bb.py? (It could be
> anything.)


See the inspect module:

http://stefaanlippens.net/python_inspect


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.