[Numpy-discussion] Getting C-function pointers from Python to C

Nathaniel Smith njs@pobox....
Thu Apr 12 12:24:39 CDT 2012


On Wed, Apr 11, 2012 at 10:23 PM, Travis Oliphant <teoliphant@gmail.com> wrote:
>>> In the mean-time, I think we could do as Robert essentially suggested and just use Capsule Objects around an agreed-upon simple C-structure:
>>>
>>>      int   id   /* Some number that can be used as a "type-check" */
>>>      void *func;
>>>      char *string;
>>>
>>> We can then just create some nice functions to go to and from this form in NumPy ctypeslib and then use this while the Python PEP gets written and adopted.
>>
>> What is not clear to me is how one get from the Python callable to the
>> capsule.
>
> This varies substantially based on the tool.   Numba would do it's work and create the capsule object using it's approach.   Cython would use a different approach.
>
> I would also propose to have in NumPy some basic functions that go back-and forth between this representation, ctypes, and any other useful representations that might emerge.
>
>>
>> Or do you simply intend to pass a non-callable capsule as an argument in
>> place of the callback?
>
> I had simply intended to allow a non-callable capsule argument to be passed in instead of another call-back to any SciPy or NumPy function that can take a raw C-function pointer.

If the cython folks are worried about type-checking overhead, then
PyCapsule seems sub-optimal, because it's unnecessarily complicated to
determine what sort of PyCapsule you have, and then extract the actual
C struct. (At a minimum, it requires two calls to non-inlineable
functions, plus an unnecessary pointer indirection.)

A tiny little custom class in a tiny little library that everyone can
share might be better? (Bonus: a custom class could define a __call__
method that used ctypes to call the function directly, for interactive
convenience/testing/etc.)

-- Nathaniel


More information about the NumPy-Discussion mailing list