473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to get a Function object from a Frame object

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

Jul 19 '05 #1
3 3002
Gigi wrote:
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
Jul 19 '05 #2
Steven Bethard wrote:
Gigi wrote:
I need access to a function object that corresponds to a frame object
in a certain case from inside the function.


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.

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 19 '05 #3
Greg Ewing wrote:
Steven Bethard wrote:
Gigi wrote:
I need access to a function object that corresponds to a frame object
in a certain case from inside the function.

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.


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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: George Sakkis | last post by:
Is there a general way of injecting code into a function, typically before and/or after the existing code ? I know that for most purposes, an OO solution, such as the template pattern, is a cleaner...
0
by: Vijay Kumar | last post by:
I have written a trace function and it recieves the current frame object from the interpreter. I want to know whether the event pertains to a class or method or function. I can only get a...
9
by: Martin Waller | last post by:
Hello, I've been playing with the idea of just how to use an ASP page to provide a remote function call. In an ideal world this would be a web service but how can you do it if restricted to ASP...
0
by: harold fellermann | last post by:
Hi all, I am trying to write a script that prints out the signatures of each function call that occurs during the execution of a second script which is invoked by my program. i.e. if the...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
18
by: Chris Ianson | last post by:
Hi geniuses (or is that genii, or genies) The challenge is as above really. I have a page with an iframe in it, and need to call a JS function in the *parent* page, *from* inside the iframe. ...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
6
by: SanPy | last post by:
The subject of this message might be a little cryptic, so here's an example of what I mean: def foo(): """doc string of foo""" print foo.__doc__ doc string of foo What I want to know is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.