pushSAXhtml.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4. # Memory debug specific
  5. libxml2.debugMemory(1)
  6. log = ""
  7. class callback:
  8. def startDocument(self):
  9. global log
  10. log = log + "startDocument:"
  11. def endDocument(self):
  12. global log
  13. log = log + "endDocument:"
  14. def startElement(self, tag, attrs):
  15. global log
  16. log = log + "startElement %s %s:" % (tag, attrs)
  17. def endElement(self, tag):
  18. global log
  19. log = log + "endElement %s:" % (tag)
  20. def characters(self, data):
  21. global log
  22. log = log + "characters: %s:" % (data)
  23. def warning(self, msg):
  24. global log
  25. log = log + "warning: %s:" % (msg)
  26. def error(self, msg):
  27. global log
  28. log = log + "error: %s:" % (msg)
  29. def fatalError(self, msg):
  30. global log
  31. log = log + "fatalError: %s:" % (msg)
  32. handler = callback()
  33. ctxt = libxml2.htmlCreatePushParser(handler, "<foo", 4, "test.xml")
  34. chunk = " url='tst'>b"
  35. ctxt.htmlParseChunk(chunk, len(chunk), 0)
  36. chunk = "ar</foo>"
  37. ctxt.htmlParseChunk(chunk, len(chunk), 1)
  38. ctxt=None
  39. reference = """startDocument:startElement html None:startElement body None:startElement foo {'url': 'tst'}:error: Tag foo invalid
  40. :characters: bar:endElement foo:endElement body:endElement html:endDocument:"""
  41. if log != reference:
  42. print "Error got: %s" % log
  43. print "Exprected: %s" % reference
  44. sys.exit(1)
  45. # Memory debug specific
  46. libxml2.cleanupParser()
  47. if libxml2.debugMemory(1) == 0:
  48. print "OK"
  49. else:
  50. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  51. libxml2.dumpMemory()