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


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

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

Started byTerry Reedy <tjreedy@udel.edu>
First post2014-09-26 14:42 -0400
Last post2014-09-26 14:42 -0400
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromTerry Reedy <tjreedy@udel.edu>
Date2014-09-26 14:42 -0400
SubjectRe: any way to tell at runtime whether a callable is implemented in Python or C ?
Message-ID<mailman.14403.1411757011.18130.python-list@python.org>
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

[toc] | [standalone]


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


csiph-web