how to get id(function) for each function in stack?

D

dmitrey

hi all,
how to get id(func) for each func in stack? (I mean memory address, to
compare it with id(some known funcs))
Thank you in advance, D.
 
I

Ian Kelly

hi all,
how to get id(func) for each func in stack? (I mean memory address, to
compare it with id(some known funcs))
Thank you in advance, D.

The answer hasn't changed since your last thread about this. The
stack contains code objects, not functions. You can get the code
objects using inspect.stack(), and compare them to the func_code
attributes of the functions you're interested in.

Also, there's no need to use id() for this. Just use the "is"
operator to check identity.

for frame_tuple in inspect.stack():
frame = frame_tuple[0]
if frame.f_code is some_function.func_code:
print("Found it!")

Cheers,
Ian
 
D

dmitrey

hi all,
how to get id(func) for each func in stack? (I mean memory address, to
compare it with id(some known funcs))
Thank you in advance, D.

The answer hasn't changed since your last thread about this.  The
stack contains code objects, not functions.  You can get the code
objects using inspect.stack(), and compare them to the func_code
attributes of the functions you're interested in.

Also, there's no need to use id() for this.  Just use the "is"
operator to check identity.

for frame_tuple in inspect.stack():
    frame = frame_tuple[0]
    if frame.f_code is some_function.func_code:
        print("Found it!")

Cheers,
Ian

Python build-in function sum() has no attribute func_code, what should
I do in the case?
D.
 
I

Ian Kelly

Python build-in function sum() has no attribute func_code, what should
I do in the case?

Built-in functions and C extension functions have no code objects, and
for that reason they also do not exist in the stack. There is no way
to find sum() in the Python stack, because it isn't there.
 
D

Dave Angel

hi all,
how to get id(func) for each func in stack? (I mean memory address, to
compare it with id(some known funcs))
Thank you in advance, D.
The answer hasn't changed since your last thread about this. The
stack contains code objects, not functions. You can get the code
objects using inspect.stack(), and compare them to the func_code
attributes of the functions you're interested in.

Also, there's no need to use id() for this. Just use the "is"
operator to check identity.

for frame_tuple in inspect.stack():
frame = frame_tuple[0]
if frame.f_code is some_function.func_code:
print("Found it!")

Cheers,
Ian
Python build-in function sum() has no attribute func_code, what should
I do in the case?
D.
flag = False

for frame_tuple in inspect.stack():
frame = frame_tuple[0]
if frame.f_code is some_function.func_code:
flag = True

if !flag:
print "FuncDesigner.sum() not used. Change."
 
I

Ian Kelly

hi all,
how to get id(func) for each func in stack? (I mean memory address, to
compare it with id(some known funcs))
Thank you in advance, D.

The answer hasn't changed since your last thread about this.  The
stack contains code objects, not functions.  You can get the code
objects using inspect.stack(), and compare them to the func_code
attributes of the functions you're interested in.

Also, there's no need to use id() for this.  Just use the "is"
operator to check identity.

for frame_tuple in inspect.stack():
    frame = frame_tuple[0]
    if frame.f_code is some_function.func_code:
        print("Found it!")

Cheers,
Ian

Python build-in function sum() has no attribute func_code, what should
I do in the case?
D.

flag = False


for frame_tuple in inspect.stack():
   frame = frame_tuple[0]
   if frame.f_code is some_function.func_code:
       flag = True

if !flag:
   print "FuncDesigner.sum() not used.  Change."

That would also print when no sum function is used at all, e.g. a
simple "a + b". I don't think that's what the OP wants.
 
L

Lie Ryan

Built-in functions and C extension functions have no code objects, and
for that reason they also do not exist in the stack. There is no way
to find sum() in the Python stack, because it isn't there.

a practical solution to this issue is to wrap the C functions in Python
functions. You lose some speed but that might be an acceptable tradeoff
in some situations (especially if you're only wrapping when debugging).
 
S

Steven D'Aprano

hi all,
how to get id(func) for each func in stack? (I mean memory address, to
compare it with id(some known funcs)) Thank you in advance, D.

id() does not return a memory address, except perhaps by accident. It
returns a ID number. In Jython, and I think IronPython, id() returns a
sequential number for each object created. The first object created by
the Jython virtual machine gets ID 1, the second gets ID 2, and so on.
 
R

Robert Kern

a practical solution to this issue is to wrap the C functions in Python
functions. You lose some speed but that might be an acceptable tradeoff in some
situations (especially if you're only wrapping when debugging).

His problem is that he wants to find out when someone is using the builtin sum()
on his objects (which apparently don't react well to it) and give an informative
warning. __builtin__.sum() is not under his control, fortunately.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top