[Pyrex] C-API implementation in Pyrex 0.9.6

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Oct 13 13:41:46 CEST 2007


All right, here's how I would arrange the declarations you
presented in your example. The code below passes both
Pyrex and C compilation for me, and the generated .h files
contain only opaque definitions of struct xmlDoc and
struct xmlNode.

#------------------------------------------------------------
# tree.pxd
#------------------------------------------------------------

cdef struct xmlDoc
cdef struct xmlNode

#------------------------------------------------------------
# tree.pyx
#------------------------------------------------------------

cdef struct xmlDoc:
   int foo

cdef struct xmlNode:
   int blarg

#------------------------------------------------------------
# etree.pxd
#------------------------------------------------------------

cimport tree

cdef class _BaseParser

cdef public class _Document [type LxmlDocumentType, object LxmlDocument]:
   cdef tree.xmlDoc *_c_doc
   cdef _BaseParser _parser

cdef public class _Element [type LxmlElementType, object LxmlElement]:
   cdef _Document _doc
   cdef tree.xmlNode *_c_node

cdef object getAttributeValue(_Element element, key, default)

#------------------------------------------------------------
# etree.pyx
#------------------------------------------------------------

cdef public class _Document [type LxmlDocumentType, object LxmlDocument]:
   pass

cdef public class _Element [type LxmlElementType, object LxmlElement]:
	pass

cdef class _BaseParser:
   pass

# To use the following function from C as well, add "api"

cdef object getAttributeValue(_Element element, key, default):
   pass

#--------------------------------------------------------------
# use_etree.pyx - to show that cimporting from etree.pxd works
#--------------------------------------------------------------

from etree cimport _Document, _Element, getAttributeValue

doc = _Document()
elem = _Element()
getAttributeValue(elem, "zax", "ftang")

#--------------------------------------------------------------

--
Greg



More information about the Pyrex mailing list