win32com.client.DispatchWithEvents and exceptions from callback

Gillou nospam at bigfoot.com
Wed Feb 13 19:24:56 CET 2002


Hi,

I got a strange behaviour of COM object made accessible through the Dispatch
This is a script that uses the latest MSXML 4.0 DomDocument.
The callback class DomDocumentEvents runs but the DomParseException
exception never raises.
Means that when using it with non well-formed or valid XML document, it
prints "Exception should raise" but the exception is NOT raised (the main
script carries on as usual) !
That said it returns a good DOM object when used with a valid XML doc, but I
don't find a *pythonic* way to handle validity errors if the exceptions is
not raised.

Any idea ? or is there something I'm missing ?

Thanks in advance.

--Gilles

======= MSXML4 wrapper =====
TRUE = 1
FALSE = 0
import exceptions
import win32com.client

class DomParseException(exceptions.Exception):
    '''Raised on well-formedness or validity error
    '''
    def __init__(self, errobj):
        # ...
        return
    def __str__(self):
        # ...
        return

class DomDocumentEvents:
    '''Handle callback events of MSXML4
    '''
    def Ononreadystatechange(self):
        '''Check for well-formedness or validity error
        '''
        if (self.readyState == 4 and self.childNodes.length == 0):
            print 'Exception should raise'
            raise DomParseException(self.parseError)
        return

    def Onondataavailable(self):
        '''May handle this for your own use
        '''
        print 'Data available'
        return

def newDomDocument():
    dd = win32com.client.DispatchWithEvents('Msxml2.DOMDocument.4.0',
DomDocumentEvents)
    dd.setProperty("SelectionLanguage", "XPath");
    dd.validateOnParse = TRUE
    dd.resolveExternals = TRUE
    dd.async = FALSE
    dd.preserveWhiteSpace = FALSE
    return dd

if __name__ == '__main__':
    domdoc = newDomDocument()
    # Loadind insane XML
    domdoc.load('text.xml')
    # Should have an exception before next line
    print domdoc.xml.encode('cp850')








More information about the Python-list mailing list