1#!/usr/bin/env python3 2# 3# this tests the entities substitutions with the XmlTextReader interface 4# 5import sys 6import libxml2 7 8# Memory debug specific 9libxml2.debugMemory(1) 10 11result = "" 12def 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# 21docstr="""<foo> 22<label>some text</label> 23<item>100</item> 24</foo>""" 25expect="""0 1 foo 0 261 14 #text 0 271 1 label 0 282 3 #text 0 291 15 label 0 301 14 #text 0 311 1 item 0 322 3 #text 0 331 15 item 0 341 14 #text 0 350 15 foo 0 36""" 37result = "" 38 39doc = libxml2.parseDoc(docstr) 40reader = doc.readerWalker(); 41ret = reader.Read() 42while ret == 1: 43 processNode(reader) 44 ret = reader.Read() 45 46if ret != 0: 47 print("Error parsing the document test1") 48 sys.exit(1) 49 50if result != expect: 51 print("Unexpected result for test1") 52 print(result) 53 sys.exit(1) 54 55doc.freeDoc() 56 57# 58# Reuse the reader for another document testing the ReaderNewWalker API 59# 60docstr="""<foo> 61<label>some text</label> 62<item>1000</item> 63</foo>""" 64expect="""0 1 foo 0 651 14 #text 0 661 1 label 0 672 3 #text 0 681 15 label 0 691 14 #text 0 701 1 item 0 712 3 #text 0 721 15 item 0 731 14 #text 0 740 15 foo 0 75""" 76result = "" 77 78doc = libxml2.parseDoc(docstr) 79reader.NewWalker(doc) 80 81ret = reader.Read() 82while ret == 1: 83 processNode(reader) 84 ret = reader.Read() 85 86if ret != 0: 87 print("Error parsing the document test2") 88 sys.exit(1) 89 90if result != expect: 91 print("Unexpected result for test2") 92 print(result) 93 sys.exit(1) 94 95doc.freeDoc() 96 97# 98# Reuse the reader for another document testing the ReaderNewxxx API 99# 100docstr="""<foo> 101<label>some text</label> 102<item>1000</item> 103</foo>""" 104expect="""0 1 foo 0 1051 14 #text 0 1061 1 label 0 1072 3 #text 0 1081 15 label 0 1091 14 #text 0 1101 1 item 0 1112 3 #text 0 1121 15 item 0 1131 14 #text 0 1140 15 foo 0 115""" 116result = "" 117 118reader.NewDoc(docstr, "test3", None, 0) 119ret = reader.Read() 120while ret == 1: 121 processNode(reader) 122 ret = reader.Read() 123 124if ret != 0: 125 print("Error parsing the document test3") 126 sys.exit(1) 127 128if result != expect: 129 print("Unexpected result for test3") 130 print(result) 131 sys.exit(1) 132 133# 134# cleanup 135# 136del reader 137 138# Memory debug specific 139libxml2.cleanupParser() 140if libxml2.debugMemory(1) == 0: 141 print("OK") 142else: 143 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 144 libxml2.dumpMemory() 145