[XML-SIG] how to clean up parser without causing parsing?

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Sat, 23 Dec 2000 12:44:31 +0100


> I thought of the following alternatives:
> 
> 1. have my handler set a flag, and then ignore further calls.
> 2. point the parser to a do nothing handler before calling close()
> 3. doing the following:  p.reset()  p.close()
> 
> But they're not as efficient. What should I be doing?

I suggest to use PyXML 0.6, and the SAX2 xmlproc driver. AFAICT, it is
safe to just drop the reference to the parser (certainly in Python
2.0, where potential cycles are collected). The xmlproc driver is not
incremental, so the SAX2 version releases the underlying parser at the
end of parse(). If you release the reader object, that will in turn
release the references to your handlers.

So in short, you should write

! p = XMLParserFactory.make_parser( 'xml.sax.drivers.drv_xmlproc' )
! sp = MyHandler()
! p.setDocumentHandler( sp )   # other handlers left out for simplicity
! p.parseFile( file )
! p = None

> (P.S. Any chance of ExtendedParser adding a free() method? :)

Since it is an experimental interface, why not? Please submit patches
to sourceforge.net/projects/pyxml.

In Python, explicit memory management is normally not necessary. So
these methods are typically called close() or release().

Please note that adding the operation to the interface won't give you
anything; you'd also have to modify the existing parsers. I personally
won't change any of the existing SAX1 drivers; efforts should be put
into the SAX2 drivers, IMO. Also, PyXML 0.5 is no longer maintained,
so I'd apply any patches I get only to 0.6.x.

Regards,
Martin