Learning Pyrex with libxml2

Victor Ng vng1 at mac.com
Wed Jun 23 13:41:55 CEST 2004

Hi, I'm trying to learn how to use Pyrex by converting some of the 
libxml2 examples.  I'm not having much luck - I can create a document, 
but any attempt to create nodes causes a segfault.

The code I'm trying to port is io2.c (http://xmlsoft.org/examples/io2.c)

I've commented the couple lines that cause a problem for me.



cdef extern from "libxml/tree.h":
    ctypedef xmlDocPtr
    ctypedef xmlNodePtr
    ctypedef xmlNsPtr
    xmlDocPtr xmlNewDoc(unsigned char *)
    xmlNodePtr xmlNewNode (xmlNsPtr, unsigned char *)
    void xmlNodeSetContent	(xmlNodePtr, unsigned char *)
    xmlNodePtr xmlDocSetRootElement	(xmlDocPtr, xmlNodePtr )
    void xmlDocDumpFormatMemory	(xmlDocPtr, unsigned char **, int *, int )
    xmlNodePtr xmlNewChild (xmlNodePtr, xmlNsPtr, unsigned char *, 
unsigned char *)
    void xmlFreeDoc (xmlDocPtr)
    void xmlFree (xmlDocPtr)

def testNewDoc():
    cdef xmlNodePtr n
    cdef xmlDocPtr doc
    cdef unsigned char* xmlbuff
    cdef int buffersize

    doc = xmlNewDoc(<unsigned char *> "1.0")

    ## The following three lines cause a segfault	
    # n = xmlNewNode(None, <unsigned char *> "root")
    # xmlNodeSetContent(n, <unsigned char *> "content")
    # xmlDocSetRootElement(doc, n)

    xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1)
    xmlFrag = <char* > xmlbuff
    print "%d bytes in string" % len(xmlFrag)
    print "[%s]" % xmlFrag


More information about the Python-list mailing list