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


Groups > comp.lang.python > #18257

Re: How to get function string name from i-th stack position?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'distinct': 0.05; 'subject:position': 0.05; 'attribute': 0.07; 'wrapper': 0.07; 'python': 0.08; 'attribute.': 0.09; 'func': 0.09; 'function:': 0.09; 'iterate': 0.09; 'messing': 0.09; 'stack,': 0.09; 'subject:string': 0.09; 'tuple.': 0.09; 'am,': 0.12; 'def': 0.13; 'folks': 0.15; '"is"': 0.16; 'bytecode': 0.16; 'frames': 0.16; 'funcdesigner': 0.16; 'lambda': 0.16; 'numpy': 0.16; 'problem).': 0.16; 'subject:function': 0.16; '\xa0you': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '(which': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'suggest': 0.20; 'trying': 0.21; 'maybe': 0.21; 'dec': 0.22; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'appear': 0.23; 'objects,': 0.23; 'somehow': 0.23; 'though.': 0.23; '\xa0if': 0.23; 'string': 0.24; 'cc:2**0': 0.24; 'candidates': 0.24; 'stack': 0.24; 'sat,': 0.25; 'code': 0.25; 'helpful': 0.26; 'testing': 0.26; 'function': 0.27; 'import': 0.27; 'compare': 0.28; 'message-id:@mail.gmail.com': 0.28; 'elements': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'class': 0.29; 'generally': 0.30; 'ctypes': 0.30; 'dept': 0.30; 'kelly': 0.30; 'modules,': 0.30; 'objects.': 0.30; 'recursion': 0.30; 'skip:g 40': 0.30; 'subject:?': 0.31; 'does': 0.32; 'list': 0.32; 'actual': 0.32; 'objects': 0.32; "won't": 0.33; 'rather': 0.33; 'instead': 0.33; 'there': 0.33; 'object': 0.33; 'fri,': 0.34; 'probably': 0.34; 'it.': 0.34; 'received:209.85.212': 0.34; 'calling': 0.34; 'anything': 0.34; 'frame': 0.34; 'stores': 0.34; 'trigger': 0.34; 'thank': 0.35; 'something': 0.35; 'subject:How': 0.35; 'example,': 0.37; 'checks': 0.37; 'comparing': 0.37; 'two': 0.37; 'but': 0.37; 'except': 0.37; 'received:google.com': 0.37; 'think': 0.37; 'could': 0.37; 'replace': 0.38; 'several': 0.38; 'received:209.85': 0.38; 'subject:from': 0.38; 'e.g.': 0.39; 'represent': 0.39; 'should': 0.39; 'being': 0.39; 'called': 0.40; "it's": 0.40; 'received:209': 0.40; 'might': 0.40; 'more': 0.61; '2011': 0.61; 'you.': 0.63; '31,': 0.64; 'due': 0.66; 'subject:name': 0.67; 'details.': 0.72; '30,': 0.74; '11:43': 0.84; 'hole': 0.84; 'other?': 0.84; 'warning.': 0.84; 'sum': 0.89; 'avoided.': 0.91; 'summarized': 0.91; '\xa0there': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=xvPn71zoYRmkQXQ1RKBvKWpFHtnFROa9v09WbbmCDYc=; b=EeNmbhtGz9G59FKbtXKHw/b3qsmkHSOOUqSTudk5b20XpsYEchgOhQIgvltUk5Xk7w AIk6WAL7Xi26BBuxYq96v04NlhD/bFImQFMrm/jxmWiqFYGIz85XA72s5gWI7Pw2Y9/i t3zMAAjfXhuT9SNgBD+WHh6CtxARMJ4W2Pbdc=
MIME-Version 1.0
In-Reply-To <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com>
References <00667d71-b9bc-411d-b2bc-0ead1d1468d7@g41g2000yqa.googlegroups.com> <mailman.4250.1325270163.27778.python-list@python.org> <a9e70268-ee24-4d18-abe2-e994e326be9c@d9g2000yqg.googlegroups.com> <mailman.4255.1325281766.27778.python-list@python.org> <4b0f3a04-3b29-4cb7-af52-2001b25d560a@t30g2000vbx.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 31 Dec 2011 10:13:08 -0700
Subject Re: How to get function string name from i-th stack position?
To dmitrey <dmitrey15@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4264.1325351621.27778.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325351621 news.xs4all.nl 6895 [2001:888:2000:d::a6]:45087
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18257

Show key headers only | View raw


On Sat, Dec 31, 2011 at 1:44 AM, dmitrey <dmitrey15@gmail.com> wrote:
> On Dec 30, 11:48 pm, Ian Kelly <ian.g.ke...@gmail.com> wrote:
>> On Fri, Dec 30, 2011 at 11:43 AM, dmitrey <dmitre...@gmail.com> wrote:
>> > Thank you. And what should I do to get function by itself instead of
>> > its string name, e.g. I want to know does this function is my_func or
>> > any other? For example, I would like to check is this function Python
>> > sum(), or maybe numpy.sum(), or anything else?
>>
>> The Python stack only includes Python code objects.  Built-ins like
>> sum won't appear in it because they're basically C functions and don't
>> have associated code objects.  If you really want to see them, you
>> could probably do something with ctypes to inspect the C stack, but I
>> don't recommend it.
>>
>> You can get the Python code objects from the stack by calling
>> inspect.stack(), which includes each frame object currently on the
>> stack as the first member of each tuple.  E.g.:
>>
>> frames = map(operator.itemgetter(0), inspect.stack())
>>
>> Each frame has an f_code attribute that stores the code object
>> associated with that frame.  Getting the actual function from the code
>> object is tricky, for two reasons.  One, not all code objects
>> represent functions.  There are also code objects for modules, class
>> definitions, and probably other thing as well.  Two, code objects
>> don't have associated functions. The relationship is the reverse:
>> functions have associated code objects.  You would have to iterate
>> over each function that you're interested in, looking for one with a
>> func_code attribute that "is" the frame's f_code attribute.
>>
>> In any case, testing function identity is a rather large rabbit hole
>> that is best avoided.  These are mathematically the same function:
>>
>> def plus1(value):
>>     return value + 1
>>
>> plus_one = lambda x: x + 1
>>
>> But they are two distinct function objects, and there is no way
>> programmatically to determine that they are the same function except
>> by comparing the bytecode (which won't work generally because of the
>> halting problem).
>>
>> What is it that you're trying to do?  Perhaps the helpful folks on the
>> list will be able to suggest a better solution if you can provide more
>> details.
>>
>> Cheers,
>> Ian
>
> Maybe it is somehow possible to compare function id with my candidates
> id, e.g.
> PythonSumID = id(sum)
> import numpy
> NumpySumID = id(numpy.sum)
> func = getting_function_from_Nth_stack_level_above
> if id(func) == PythonSumID:
>  ....
> elif id(func) == NumpySumID:
>  ....
> else:
>  ....
> I need it due to the following reason: FuncDesigner users often use
> Python or numpy sum on FuncDesigner objects, while FuncDesigner.sum()
> is optimized for this case, works faster and doesn't lead to "Max
> recursion dept exceeded", that sometimes trigger for numpy or Python
> sum() when number of summarized elements is more than several
> hundreds. I would like to print warning "you'd better use FuncDesigner
> sum" if this case has been identified.

The only think I can think of would be to replace the sum built-in
with a wrapper that checks whether it's being called on FuncDesigner
objects and issues a warning.  Users might not like you messing with
their built-ins in that way, though.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to get function string name from i-th stack position? dmitrey <dmitrey15@gmail.com> - 2011-12-30 09:51 -0800
  Re: How to get function string name from i-th stack position? Tim Chase <python.list@tim.thechases.com> - 2011-12-30 12:35 -0600
    Re: How to get function string name from i-th stack position? dmitrey <dmitrey15@gmail.com> - 2011-12-30 10:43 -0800
      Re: How to get function string name from i-th stack position? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-30 14:48 -0700
        Re: How to get function string name from i-th stack position? dmitrey <dmitrey15@gmail.com> - 2011-12-31 00:44 -0800
          Re: How to get function string name from i-th stack position? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-31 10:13 -0700
      Re: How to get function string name from i-th stack position? Lie Ryan <lie.1296@gmail.com> - 2011-12-31 22:41 +1100
      Re: How to get function string name from i-th stack position? Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-31 10:05 -0700

csiph-web