Re: [xml] strange transformCtxt free-ing problem



I've made a simple test case for my problem.

It always breaks but doesn't always cause libxm2 to segfault.

The htmlCtxtReadFile never seems to return any HTML.

Anyone have a clue?



import os
import libxml2
import libxslt
import logging
import sys

def loader(url, pctx, ctx, type):
    doc = None
    context_object = None
    if type:
        context_object = libxslt.stylesheet(_obj=ctx)
    else:
        context_object = libxslt.transformCtxt(_obj=ctx)
    # The parserContext and resulting document
    parserContext = libxml2.parserCtxt(_obj=pctx)
    doc = None
    if url == "/one":
        doc = parserContext.htmlCtxtReadFile("./file2.html", "UTF8", 1)
    else:
        doc = parserContext.ctxtReadDoc("""<document>
<h1>this is xml</h1>
</document>""", url, "UTF8", 0)
    return doc

def transfrm(source_document_str, stylesheet_document_str):
    logger = logging.getLogger("transfrm")
    style = libxslt.newStylesheet()
    style_doc = None
    transform_ctxt = None
    result = None
    try:
        try:
            src_doc = libxml2.parseDoc(source_document_str)
            style_doc = libxml2.parseDoc(stylesheet_document_str)
            style.parseStylesheetProcess(style_doc)
            # Setup the transformation context
            transform_ctxt = style.newTransformContext(src_doc)
            # Make the result and send it to the client
            result = style.applyStylesheetUser(src_doc, {}, transform_ctxt)
            if result:
                output_method = style.method()
                xslt_output_str = style.saveResultToString(result)
                return xslt_output_str
            else:
                return None

        except Exception, e:
            return None

    finally:
        # Free the objects we created.
        if transform_ctxt:
            try:
                transform_ctxt.freeTransformContext()
            except Exception,e:
                logger.error("problem freeing tranfrom context")

        if style:
            try:
                style.freeStylesheet()
            except Exception, e:
                logger.error("problem freeing stylesheet object")

        if style_doc:
            try:
                style_doc.freeDoc()
            except Exception, e:
                logger.error("problem freeing style document")

        if result:
            try:
                result.freeDoc()
            except Exception, e:
                logger.error("problem freeing style document")
                

if __name__ == "__main__":
    # fixme - set these too well known rss, atom and hatom feeds
    try:
        libxslt.setLoaderFunc(loader)
    except Exception, e:
        # Whoops! serious error
        print >>sys.stderr, "couldn't set the resolver!"
    else:
        src_doc = """<example/>"""
        xslt_doc = """<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet  version="1.0" 
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
            </head>
            <body>
               <div>
                 <xsl:copy-of select="document('/one')"/>
               </div>
               <div>
                 <xsl:apply-templates select="document('/two')//h1"/>
               </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>"""

        #import pdb
        #print pdb.runcall(transfrm, src_doc, xslt_doc)
        print transfrm(src_doc, xslt_doc)

# End

-- 
Nic Ferrier
http://www.tapsellferrier.co.uk   for all your tapsell ferrier needs



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]