1 #!/usr/bin/python -u 2 # 3 # this tests the entities substitutions with the XmlTextReader interface 4 # 5 import sys 6 import libxml2 7 8 # Memory debug specific 9 libxml2.debugMemory(1) 10 11 result = "" 12 def processNode(reader): 13 global result 14 15 result = result + "%d %d %s %d\n" % (reader.Depth(), reader.NodeType(), 16 reader.Name(), reader.IsEmptyElement()) 17 18 # 19 # Parse a document testing the readerForxxx API 20 # 21 docstr="""<foo> 22 <label>some text</label> 23 <item>100</item> 24 </foo>""" 25 expect="""0 1 foo 0 26 1 14 #text 0 27 1 1 label 0 28 2 3 #text 0 29 1 15 label 0 30 1 14 #text 0 31 1 1 item 0 32 2 3 #text 0 33 1 15 item 0 34 1 14 #text 0 35 0 15 foo 0 36 """ 37 result = "" 38 39 reader = libxml2.readerForDoc(docstr, "test1", None, 0) 40 ret = reader.Read() 41 while ret == 1: 42 processNode(reader) 43 ret = reader.Read() 44 45 if ret != 0: 46 print("Error parsing the document test1") 47 sys.exit(1) 48 49 if result != expect: 50 print("Unexpected result for test1") 51 print(result) 52 sys.exit(1) 53 54 # 55 # Reuse the reader for another document testing the ReaderNewxxx API 56 # 57 docstr="""<foo> 58 <label>some text</label> 59 <item>1000</item> 60 </foo>""" 61 expect="""0 1 foo 0 62 1 14 #text 0 63 1 1 label 0 64 2 3 #text 0 65 1 15 label 0 66 1 14 #text 0 67 1 1 item 0 68 2 3 #text 0 69 1 15 item 0 70 1 14 #text 0 71 0 15 foo 0 72 """ 73 result = "" 74 75 reader.NewDoc(docstr, "test2", None, 0) 76 ret = reader.Read() 77 while ret == 1: 78 processNode(reader) 79 ret = reader.Read() 80 81 if ret != 0: 82 print("Error parsing the document test2") 83 sys.exit(1) 84 85 if result != expect: 86 print("Unexpected result for test2") 87 print(result) 88 sys.exit(1) 89 90 # 91 # cleanup 92 # 93 del reader 94 95 # Memory debug specific 96 libxml2.cleanupParser() 97 if libxml2.debugMemory(1) == 0: 98 print("OK") 99 else: 100 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 101 libxml2.dumpMemory() 102