pushSAX.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.createPushParser(handler, "<foo", 4, "test.xml")
  34. chunk = " url='tst'>b"
  35. ctxt.parseChunk(chunk, len(chunk), 0)
  36. chunk = "ar</foo>"
  37. ctxt.parseChunk(chunk, len(chunk), 1)
  38. ctxt=None
  39. reference = "startDocument:startElement foo {'url': 'tst'}:characters: bar:endElement foo:endDocument:"
  40. if log != reference:
  41. print "Error got: %s" % log
  42. print "Exprected: %s" % reference
  43. sys.exit(1)
  44. # Memory debug specific
  45. libxml2.cleanupParser()
  46. if libxml2.debugMemory(1) == 0:
  47. print "OK"
  48. else:
  49. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  50. libxml2.dumpMemory()