How to get a Function object from a Frame object

G

Gigi

Hi,
I need access to a function object that corresponds to a frame object in
a certain case from inside the function.

I can get the frame object using:

f = sys._getframe(0)

But the resulting frame object doesn't contain the information I need.
There is a lot of information in the code object (f.f_code), but not the
actual function or method object.

The inspect module doesn't help either it just provides nicer interface
to the Frame object.

Any ideas out there?

Thanks, Gigi
 
S

Steven Bethard

Gigi said:
I need access to a function object that corresponds to a frame object in
a certain case from inside the function.

I can get the frame object using:

f = sys._getframe(0)

But the resulting frame object doesn't contain the information I need.
There is a lot of information in the code object (f.f_code), but not the
actual function or method object.

The inspect module doesn't help either it just provides nicer interface
to the Frame object.

Any ideas out there?

Can you look up the code's co_name in the previous frame's locals?

py> def getself():
.... frame = sys._getframe(1)
.... return frame.f_back.f_locals[frame.f_code.co_name]
....
py> def g():
.... def h():
.... return getself()
.... return dict(g=getself(), h=h())
....
py> g()
{'h': <function h at 0x01194BB0>, 'g': <function g at 0x01194BF0>}

You probably also need to check the previous frame's globals... And
there are probably some cases where this code still wouldn't work...

STeVe
 
G

Greg Ewing

Steven said:
Can you look up the code's co_name in the previous frame's locals?

You probably also need to check the previous frame's globals... And
there are probably some cases where this code still wouldn't work...

Note that in general it's impossible to tell exactly
which function object was involved, since there could
be more than one function object sharing the same code
object, and the frame only references the code object.
 
G

Gigi

Greg said:
Note that in general it's impossible to tell exactly
which function object was involved, since there could
be more than one function object sharing the same code
object, and the frame only references the code object.

I can get the co_name and everything that's available from the code
object. However, I can't get to the actual function object. I need the
function object to get a custom function attribute that was injected
earlier as context. I didn't know that code objects could be shared. I
guess it really makes the whole thing impossible, unless the code object
kept a list of all the functions that share it. Thanks, anyway. I found
a different solution.
 

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,053
Latest member
BrodieSola

Latest Threads

Top