Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18612 > unrolled thread

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

Started bydmitrey <dmitrey15@gmail.com>
First post2012-01-06 10:02 -0800
Last post2012-01-07 00:54 +0000
Articles 9 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  how to get id(function) for each function in stack? dmitrey <dmitrey15@gmail.com> - 2012-01-06 10:02 -0800
    Re: how to get id(function) for each function in stack? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 11:28 -0700
      Re: how to get id(function) for each function in stack? dmitrey <dmitrey15@gmail.com> - 2012-01-06 11:29 -0800
        Re: how to get id(function) for each function in stack? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 12:50 -0700
        Re: how to get id(function) for each function in stack? Dave Angel <d@davea.name> - 2012-01-06 14:56 -0500
        Re: how to get id(function) for each function in stack? Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-06 13:37 -0700
        Re: how to get id(function) for each function in stack? Lie Ryan <lie.1296@gmail.com> - 2012-01-07 11:17 +1100
        Re: how to get id(function) for each function in stack? Robert Kern <robert.kern@gmail.com> - 2012-01-07 10:39 +0000
    Re: how to get id(function) for each function in stack? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-07 00:54 +0000

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

Fromdmitrey <dmitrey15@gmail.com>
Date2012-01-06 10:02 -0800
Subjecthow to get id(function) for each function in stack?
Message-ID<1002d4cd-6cfe-4b79-917f-361a06ffd215@a11g2000vbz.googlegroups.com>
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.

[toc] | [next] | [standalone]


#18613

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-01-06 11:28 -0700
Message-ID<mailman.4490.1325874536.27778.python-list@python.org>
In reply to#18612
On Fri, Jan 6, 2012 at 11:02 AM, dmitrey <dmitrey15@gmail.com> wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#18614

Fromdmitrey <dmitrey15@gmail.com>
Date2012-01-06 11:29 -0800
Message-ID<9f8a8fd4-3541-4f9b-a887-3d10524de8f1@t30g2000vbx.googlegroups.com>
In reply to#18613
On Jan 6, 8:28 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey <dmitre...@gmail.com> wrote:
> > 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.

[toc] | [prev] | [next] | [standalone]


#18615

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-01-06 12:50 -0700
Message-ID<mailman.4491.1325879475.27778.python-list@python.org>
In reply to#18614
On Fri, Jan 6, 2012 at 12:29 PM, dmitrey <dmitrey15@gmail.com> wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#18616

FromDave Angel <d@davea.name>
Date2012-01-06 14:56 -0500
Message-ID<mailman.4492.1325879798.27778.python-list@python.org>
In reply to#18614
On 01/06/2012 02:29 PM, dmitrey wrote:
> On Jan 6, 8:28 pm, Ian Kelly<ian.g.ke...@gmail.com>  wrote:
>> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey<dmitre...@gmail.com>  wrote:
>>> 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."






-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#18618

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-01-06 13:37 -0700
Message-ID<mailman.4493.1325882303.27778.python-list@python.org>
In reply to#18614
On Fri, Jan 6, 2012 at 12:56 PM, Dave Angel <d@davea.name> wrote:
> On 01/06/2012 02:29 PM, dmitrey wrote:
>>
>> On Jan 6, 8:28 pm, Ian Kelly<ian.g.ke...@gmail.com>  wrote:
>>>
>>> On Fri, Jan 6, 2012 at 11:02 AM, dmitrey<dmitre...@gmail.com>  wrote:
>>>>
>>>> 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.

[toc] | [prev] | [next] | [standalone]


#18624

FromLie Ryan <lie.1296@gmail.com>
Date2012-01-07 11:17 +1100
Message-ID<mailman.4498.1325895612.27778.python-list@python.org>
In reply to#18614
On 01/07/2012 06:50 AM, Ian Kelly wrote:
> On Fri, Jan 6, 2012 at 12:29 PM, dmitrey<dmitrey15@gmail.com>  wrote:
>> 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.

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).

[toc] | [prev] | [next] | [standalone]


#18634

FromRobert Kern <robert.kern@gmail.com>
Date2012-01-07 10:39 +0000
Message-ID<mailman.4504.1325932780.27778.python-list@python.org>
In reply to#18614
On 1/7/12 12:17 AM, Lie Ryan wrote:
> On 01/07/2012 06:50 AM, Ian Kelly wrote:
>> On Fri, Jan 6, 2012 at 12:29 PM, dmitrey<dmitrey15@gmail.com> wrote:
>>> 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.
>
> 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

[toc] | [prev] | [next] | [standalone]


#18627

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-01-07 00:54 +0000
Message-ID<4f0797d3$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#18612
On Fri, 06 Jan 2012 10:02:28 -0800, dmitrey wrote:

> 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.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web