win32com.client.DispatchWithEvents and exceptions from callback

Gillou nospam at
Wed Feb 13 13:24:56 EST 2002


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.


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

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

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)

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

def newDomDocument():
    dd = win32com.client.DispatchWithEvents('Msxml2.DOMDocument.4.0',
    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
    # Should have an exception before next line
    print domdoc.xml.encode('cp850')

More information about the Python-list mailing list