[Pyrex] C-API implementation in Pyrex 0.9.6

Stefan Behnel stefan_ml at behnel.de
Sat Oct 13 13:07:31 CEST 2007


Greg Ewing wrote:
> Stefan Behnel wrote:
>> I have two
>> public types and a public function in a .pyx file called etree.pyx:
>>
>> cdef public class _Document [ type LxmlDocumentType, object
>> LxmlDocument ]:
>> ...
>>
>> cdef public class _Element [ type LxmlElementType, object LxmlElement ]:
>> ...
>>
>> This is the public C-API definition in a separate .pxd file called
>> etreepublic.pxd:
>>
>>
>> cdef extern from "etree.h":
>>
>>     cdef class lxml.etree._Document [ object LxmlDocument ]:
>>         ...
>>
>>     cdef class lxml.etree._Element [ object LxmlElement ]:
>>         ...
> 
> I'm confused about what you're trying to achieve here. If you
> want to export _Document and _Element to other Pyrex modules,
> why don't you simply declare them in etree.pyx?

I'm not sure what you are suggesting here, but I need these types as part of
the C-API, as they are passed into functions. I also subclass some of them in
the Cython code of separate modules. They are really part of the C-level API
of the module.

I also don't distinguish between the API for C code and the API for Cython
implemented modules. Both are the same.


> If you're using the facilities properly, you should never have
> to include a .h file to get declarations from one Pyrex module
> to another. You should only need to cimport from the appropriate
> module.

... as long as the public types match the internal representation of the
types. Which they do not in this case, as only part of the type is public.


> I suspect you're using a mixture of the Cython and Pyrex way
> of doing things, which might be responsible for some of your
> problems.

As I said, Pyrex doesn't currently allow me to distinguish between public and
non-public attributes of extension types. The example I was giving was a
Cython example, meant to show what Pyrex currently does not support. On the
other hand, Pyrex distinguishes between a C code API and a Pyrex code API. I
don't see why. The whole purpose of having a public API is to let external
code interface with a module at the C level and I don't want to enforce the
decision whether the external code is in C or Cython or Pyrex.


>> (Alternatively, do not require the public API to be
>> defined in the module .pxd file).
> 
> Actually, I don't -- you can use the "public" and "api" keywords
> in the .pyx file to export things to C code only. But that
> probably doesn't help you here, as you appear to want to export
> them to other Pyrex modules as well, or maybe instead -- that's
> one of the things that's not clear.

Right. Both.

Stefan




More information about the Pyrex mailing list