1 #!/usr/bin/python -u 2 # 3 # this tests the basic APIs of the XmlTextReader interface 4 # 5 import libxml2 6 import sys 7 try: 8 import StringIO 9 str_io = StringIO.StringIO 10 except: 11 import io 12 str_io = io.StringIO 13 14 # Memory debug specific 15 libxml2.debugMemory(1) 16 17 def tst_reader(s): 18 f = str_io(s) 19 input = libxml2.inputBuffer(f) 20 reader = input.newTextReader("tst") 21 res = "" 22 while reader.Read(): 23 res=res + "%s (%s) [%s] %d\n" % (reader.NodeType(),reader.Name(), 24 reader.Value(), reader.IsEmptyElement()) 25 if reader.NodeType() == 1: # Element 26 while reader.MoveToNextAttribute(): 27 res = res + "-- %s (%s) [%s]\n" % (reader.NodeType(), 28 reader.Name(),reader.Value()) 29 return res 30 31 expect="""1 (test) [None] 0 32 1 (b) [None] 1 33 1 (c) [None] 1 34 15 (test) [None] 0 35 """ 36 37 res = tst_reader("""<test><b/><c/></test>""") 38 39 if res != expect: 40 print("Did not get the expected error message:") 41 print(res) 42 sys.exit(1) 43 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() 51