[xml] using "raise" in Python error handler callbacks



Whilst experimenting with the Python bindings I noticed that exceptions are not propagated when using registerErrorHandler(). If I run the following I get the error message:

        /etc/passwd : failed to parse.

However, there is no Traceback for the "raise".  Is this correct?

import libxml2
import sys

def callback(ctx, str):
    print str
    raise ("How come I don't see this exception: " % str)

libxml2.registerErrorHandler (callback, "!!!")

def processNode(reader):
    print "%d %d %s %d" % (reader.Depth(), reader.NodeType(),
                           reader.Name(), reader.IsEmptyElement())

def streamFile(filename):
    try:
        reader = libxml2.newTextReaderFilename(filename)
        reader.SetParserProp(libxml2.PARSER_DEFAULTATTRS, 1)
        reader.SetParserProp(libxml2.PARSER_VALIDATE, 1)
    except:
        print "unable to open %s" % (filename)
        return

    ret = reader.Read()

    while ret == 1:
        processNode(reader)
        ret = reader.Read()

    if ret != 0:
        print "%s : failed to parse" % (filename)
        sys.exit (1)

    if reader.IsValid() != 1:
        print "Document failed to validate"
        sys.exit(1)

streamFile ("/etc/passwd")




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