[xml] proper way to re-use stylesheets in Python, and memory corruption



This note may be related to "debugging output buffer" thread.

I'm using libxml2.py dated 1/9/2003  (Windows)

libxml2mod.pyd same date.

(sorry, but libxml2.__version__ doesn't exist)

I suspect I'm getting some kind of memory corruption using libxslt. I'm not sure if its 
because I'm mis-using the library, or if there's something amiss in the version of libxml2 
that I have.

I'm running under medusa, serving up .xml or .htm files. When the user asks for the 
.htm version, I use libxslt to do the conversion.

Everything works fine if I serve up .xml files, but once I start serving .htm files, other 
objects get screwed up.. Images that used to serve ok suddenly appear distorted, or 
valid .js files get syntax errors, etc.  

I'm not getting a GPF (which I'd prefer to see), but it looks like some memory is being 
over-written.

I cache and re-use stylesheets using this code. This is a perpetual process, so I'm not 
worried about freeing the stylesheets.

Before I break out the debugger, does anyone see anything wrong with the code 
fragment below?

Class <snip>

    def makeStyleSheet(self,basepath):
        """Create  a new stylesheet from basepath"""
        f = self.filehandler.getFileObject((self.baseHREF,basepath + '.xsl'))
        docText = f.fileObject().read()
        styleDoc = libxml2.parseDoc(docText)
        style = libxslt.parseStylesheetDoc(styleDoc)
        self.stylesheetCache[basepath] = style
        return style
    

    def renderHTML(self,httphelper,basepath,xml):
        """convert XML into html

            httphelper -- contains parameters and args

            basepath -- the filename portion of the xsl sheet to launch (ie, 'computer')

            xml -- the xml data as a string
        """
        cachedStyleSheet = self.stylesheetCache.get(basepath)
        if not cachedStyleSheet:
            cachedStyleSheet = self.makeStyleSheet(basepath)

        doc = libxml2.parseDoc(xml)
        result = cachedStyleSheet.applyStylesheet(doc,self.defaultStyleSheetParams) 
        res = cachedStyleSheet.saveResultToString(result)
        result.freeDoc()
        doc.freeDoc()
        return res





-- 
Brad Clements,                bkc murkworks com   (315)268-1000
http://www.murkworks.com                          (315)268-9812 Fax
http://www.wecanstopspam.org/                   AOL-IM: BKClements




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