Dag Sverre Seljebotn wrote:
> Stefan Behnel wrote:
>> it looks like Py3.1 has its own way of defining external C-APIs:
>>
>> http://docs.python.org/3.1/extending/extending.html#using-capsules
>>
>> I guess we should switch to that method for C code being compiled under
>> Py3.1+, so that Cython modules become compatible with other C extensions
>> that start using that mechanism.
> 
> Am I right in thinking that this is basically a standardization of what 
> we do with Cython's __pyx_capi? So we'd just have a 1:1 correspondance 
> with what we put in capsules and what we put in __pyx_capi?

Actually, rereading their examples now, it's not quite the same thing.
Cython exports a dict that contains PyCObjects/PyCapsules for each
function, whereas their proposed approach is encapsulated by a header file
that is presented to work like this:

Export:

- you create a void* array containing function pointers
- you wrap the pointer to it in a PyCapsule
- you write a header file around it that defines the names and an import
  function in whatever way.

Import:

The import function in the header file calls

        PyCapsule_Import(fq_module_attribute_name, non_blocking_flag)

to import the module and retrieve the pointer by attribute name.

The header file additionally defines a call macro that just statically
knows at what offset in the pointer table you find the right function.

Look at the example header file at the link above for details.

I actually find Pyrex's/Cython's way of doing that a *lot* nicer and safer.

However, the main machinery for C-API users is that you get a header file
from whatever source, #include it in your C source, call the import
function it defines, and can then call the function as you see fit. That's
the same for both cases, so the way Cython exports the C-API is just fine IMO.

OTOH, PyCapsule_Import() won't work on Cython modules, as it doesn't export
a plain void* but a dict of void*.

Also, Cython can't directly cimport a C-API that's defined in the described
way. Instead, you have to use the header file and call the import function
manually. But that's not too much overhead, as you have to define the
functions anyway. Switching to the described way would really remove
features from Cython here, with no added gain.

Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to