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("Exprected: %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("Exprected: %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("Exprected: %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: reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
88: if log != reference:
89: print("Error got: %s" % log)
90: print("Exprected: %s" % reference)
91: sys.exit(1)
92:
93: log=""
94: chunk="""<foo><bar2 a="1" b='2' >"""
95: ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
96: ctxt.parseChunk(chunk, len(chunk), 0)
97: ctxt=None
98:
99: reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:"
100: if log != reference:
101: print("Error got: %s" % log)
102: print("Exprected: %s" % reference)
103: sys.exit(1)
104:
105: log=""
106: chunk="""<foo><bar2 a="1" b='2' ></bar2>"""
107: ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
108: ctxt.parseChunk(chunk, len(chunk), 0)
109: ctxt=None
110:
111: reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
112: if log != reference:
113: print("Error got: %s" % log)
114: print("Exprected: %s" % reference)
115: sys.exit(1)
116:
117: log=""
118: chunk="""<foo><bar2 a="b='1' />"""
119: ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
120: ctxt.parseChunk(chunk, len(chunk), 0)
121: ctxt=None
122:
123: reference = "startDocument:startElement foo None:"
124: if log != reference:
125: print("Error got: %s" % log)
126: print("Exprected: %s" % reference)
127: sys.exit(1)
128:
129: # Memory debug specific
130: libxml2.cleanupParser()
131: if libxml2.debugMemory(1) == 0:
132: print("OK")
133: else:
134: print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
135: libxml2.dumpMemory()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>