readererr.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/python -u
  2. #
  3. # this tests the basic APIs of the XmlTextReader interface
  4. #
  5. import libxml2
  6. import StringIO
  7. import sys
  8. # Memory debug specific
  9. libxml2.debugMemory(1)
  10. expect="""--> (3) test1:1:xmlns: URI foo is not absolute
  11. --> (4) test1:1:Opening and ending tag mismatch: c line 1 and a
  12. """
  13. err=""
  14. def myErrorHandler(arg,msg,severity,locator):
  15. global err
  16. err = err + "%s (%d) %s:%d:%s" % (arg,severity,locator.BaseURI(),locator.LineNumber(),msg)
  17. f = StringIO.StringIO("""<a xmlns="foo"><b b1="b1"/><c>content of c</a>""")
  18. input = libxml2.inputBuffer(f)
  19. reader = input.newTextReader("test1")
  20. reader.SetErrorHandler(myErrorHandler,"-->")
  21. while reader.Read() == 1:
  22. pass
  23. if err != expect:
  24. print "error"
  25. print "received %s" %(err)
  26. print "expected %s" %(expect)
  27. sys.exit(1)
  28. reader.SetErrorHandler(None,None)
  29. if reader.GetErrorHandler() != (None,None):
  30. print "GetErrorHandler failed"
  31. sys.exit(1)
  32. #
  33. # cleanup for memory allocation counting
  34. #
  35. del f
  36. del input
  37. del reader
  38. # Memory debug specific
  39. libxml2.cleanupParser()
  40. if libxml2.debugMemory(1) == 0:
  41. print "OK"
  42. else:
  43. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  44. libxml2.dumpMemory()