reader4.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. def tst_reader(s):
  11. f = StringIO.StringIO(s)
  12. input = libxml2.inputBuffer(f)
  13. reader = input.newTextReader("tst")
  14. res = ""
  15. while reader.Read():
  16. res=res + "%s (%s) [%s] %d\n" % (reader.NodeType(),reader.Name(),
  17. reader.Value(), reader.IsEmptyElement())
  18. if reader.NodeType() == 1: # Element
  19. while reader.MoveToNextAttribute():
  20. res = res + "-- %s (%s) [%s]\n" % (reader.NodeType(),
  21. reader.Name(),reader.Value())
  22. return res
  23. expect="""1 (test) [None] 0
  24. 1 (b) [None] 1
  25. 1 (c) [None] 1
  26. 15 (test) [None] 0
  27. """
  28. res = tst_reader("""<test><b/><c/></test>""")
  29. if res != expect:
  30. print "Did not get the expected error message:"
  31. print res
  32. sys.exit(1)
  33. # Memory debug specific
  34. libxml2.cleanupParser()
  35. if libxml2.debugMemory(1) == 0:
  36. print "OK"
  37. else:
  38. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  39. libxml2.dumpMemory()