1 #!/usr/bin/python -u 2 import sys 3 import libxml2 4 5 # Memory debug specific 6 libxml2.debugMemory(1) 7 8 log = "" 9 10 class callback: 11 def startDocument(self): 12 global log 13 log = log + "startDocument:" 14 15 def endDocument(self): 16 global log 17 log = log + "endDocument:" 18 19 def startElement(self, tag, attrs): 20 global log 21 log = log + "startElement %s %s:" % (tag, attrs) 22 23 def endElement(self, tag): 24 global log 25 log = log + "endElement %s:" % (tag) 26 27 def characters(self, data): 28 global log 29 log = log + "characters: %s:" % (data) 30 31 def warning(self, msg): 32 global log 33 log = log + "warning: %s:" % (msg) 34 35 def error(self, msg): 36 global log 37 log = log + "error: %s:" % (msg) 38 39 def fatalError(self, msg): 40 global log 41 log = log + "fatalError: %s:" % (msg) 42 43 handler = callback() 44 45 log="" 46 chunk="""<foo><bar2/>""" 47 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 48 ctxt.parseChunk(chunk, len(chunk), 0) 49 ctxt=None 50 51 reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:" 52 if log != reference: 53 print("Error got: %s" % log) 54 print("Expected: %s" % reference) 55 sys.exit(1) 56 57 log="" 58 chunk="""<foo><bar2></bar2>""" 59 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 60 ctxt.parseChunk(chunk, len(chunk), 0) 61 ctxt=None 62 63 reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:" 64 if log != reference: 65 print("Error got: %s" % log) 66 print("Expected: %s" % reference) 67 sys.exit(1) 68 69 log="" 70 chunk="""<foo><bar2>""" 71 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 72 ctxt.parseChunk(chunk, len(chunk), 0) 73 ctxt=None 74 75 reference = "startDocument:startElement foo None:startElement bar2 None:" 76 if log != reference: 77 print("Error got: %s" % log) 78 print("Expected: %s" % reference) 79 sys.exit(1) 80 81 log="" 82 chunk="""<foo><bar2 a="1" b='2' />""" 83 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 84 ctxt.parseChunk(chunk, len(chunk), 0) 85 ctxt=None 86 87 reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:" 88 reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:endElement bar2:" 89 if log not in (reference1, reference2): 90 print("Error got: %s" % log) 91 print("Expected: %s" % reference) 92 sys.exit(1) 93 94 log="" 95 chunk="""<foo><bar2 a="1" b='2' >""" 96 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 97 ctxt.parseChunk(chunk, len(chunk), 0) 98 ctxt=None 99 100 reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:" 101 reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:" 102 if log not in (reference1, reference2): 103 print("Error got: %s" % log) 104 print("Expected: %s" % reference) 105 sys.exit(1) 106 107 log="" 108 chunk="""<foo><bar2 a="1" b='2' ></bar2>""" 109 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 110 ctxt.parseChunk(chunk, len(chunk), 0) 111 ctxt=None 112 113 reference1 = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:" 114 reference2 = "startDocument:startElement foo None:startElement bar2 {'b': '2', 'a': '1'}:endElement bar2:" 115 if log not in (reference1, reference2): 116 print("Error got: %s" % log) 117 print("Expected: %s" % reference) 118 sys.exit(1) 119 120 log="" 121 chunk="""<foo><bar2 a="b='1' />""" 122 ctxt = libxml2.createPushParser(handler, None, 0, "test.xml") 123 ctxt.parseChunk(chunk, len(chunk), 0) 124 ctxt=None 125 126 reference = "startDocument:startElement foo None:" 127 if log != reference: 128 print("Error got: %s" % log) 129 print("Expected: %s" % reference) 130 sys.exit(1) 131 132 # Memory debug specific 133 libxml2.cleanupParser() 134 if libxml2.debugMemory(1) == 0: 135 print("OK") 136 else: 137 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 138 libxml2.dumpMemory() 139