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


Groups > comp.lang.python > #78389

Re: any way to tell at runtime whether a callable is implemented in Python or C ?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin2!goblin.stu.neva.ru!newsfeed1.swip.net!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'python.': 0.02; 'cpython': 0.05; 'subject:Python': 0.06; 'args': 0.07; 'attribute': 0.07; 'builtin': 0.09; 'exception,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'jan': 0.12; "wouldn't": 0.14; 'attribute,': 0.16; 'attribute.': 0.16; 'callable': 0.16; 'meanwhile,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject: \n ': 0.16; 'subject: ?': 0.16; 'language': 0.16; 'wrote:': 0.18; '(but': 0.19; 'stefan': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'either.': 0.24; 'extension': 0.26; 'pass': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'am,': 0.29; 'code': 0.31; 'too.': 0.31; '(perhaps': 0.31; '>>>>': 0.31; 'coded': 0.31; 'inspect': 0.31; 'sep': 0.31; 'languages': 0.32; 'running': 0.33; 'fri,': 0.33; 'implemented': 0.33; 'classes': 0.35; 'definition': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'catch': 0.60; 'ian': 0.60; 'times': 0.62; '26,': 0.68; 'invalid': 0.68; 'of:': 0.68; 'fails,': 0.84; 'inexpensive': 0.84; 'received:fios.verizon.net': 0.84; 'subject:tell': 0.84; 'why?': 0.91; 'received:108': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: any way to tell at runtime whether a callable is implemented in Python or C ?
Date Fri, 26 Sep 2014 14:42:45 -0400
References <m035kl$54v$1@ger.gmane.org> <CAPTjJmpg1LkHk4FDSQX30Tusdw_9=+iVUDXyntLN1TVrbX4U7g@mail.gmail.com> <m03l7b$umb$1@ger.gmane.org> <CALwzid=jrg+UaPW+ubnnP4Ux8V7duGuT81c-j-6jDhkgzybkPw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-108-16-203-145.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <CALwzid=jrg+UaPW+ubnnP4Ux8V7duGuT81c-j-6jDhkgzybkPw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14403.1411757011.18130.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1411757011 news.xs4all.nl 2829 [2001:888:2000:d::a6]:33569
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78389

Show key headers only | View raw


On 9/26/2014 12:10 PM, Ian Kelly wrote:
> On Fri, Sep 26, 2014 at 6:12 AM, Stefan Behnel <stefan_ml@behnel.de> wrote:

>>> On Fri, Sep 26, 2014 at 5:47 PM, Wolfgang Maier wrote:
>>>> is there any reliable and inexpensive way to inspect a callable from running
>>>> Python code to learn whether it is implemented in Python or C before calling
>>>> into it ?

Implementation languages are not part of the language definition and are 
not limited to Python and C.  Some CPython extension modules have used 
Fortran (perhaps with a very thin C layer).

One way I can think of: apply inspect.signature. If it fails, the 
function is not coded in Python.  If it succeeds, pass a correct number 
of args but invalid types/values (float('nan') for instance), catch the 
exception, and see if the traceback contains a line of Python code from 
the function.  (But I am not sure what happens if the function was coded 
in Python but the code is not available.)

As someone already asked, why?

>> Cython implemented native functions have a "__code__" attribute, too. Their
>> current "__code__.co_code" attribute is empty (no bytecode), but I wouldn't
>> rely on that for all times either.
>
> Meanwhile, Python classes and objects with __call__ methods have no
> __code__ attribute.

Also, Python functions can call C functions, and many builtin functions 
take Python functions as arguments.

-- 
Terry Jan Reedy

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


Thread

Re: any way to tell at runtime whether a callable is implemented in Python or C ? Terry Reedy <tjreedy@udel.edu> - 2014-09-26 14:42 -0400

csiph-web