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 | expect="""--> (3) test1:1:xmlns: URI foo is not absolute
|
---|
18 | --> (4) test1:1:Opening and ending tag mismatch: c line 1 and a
|
---|
19 | """
|
---|
20 | err=""
|
---|
21 | def myErrorHandler(arg,msg,severity,locator):
|
---|
22 | global err
|
---|
23 | err = err + "%s (%d) %s:%d:%s" % (arg,severity,locator.BaseURI(),locator.LineNumber(),msg)
|
---|
24 |
|
---|
25 | f = str_io("""<a xmlns="foo"><b b1="b1"/><c>content of c</a>""")
|
---|
26 | input = libxml2.inputBuffer(f)
|
---|
27 | reader = input.newTextReader("test1")
|
---|
28 | reader.SetErrorHandler(myErrorHandler,"-->")
|
---|
29 | while reader.Read() == 1:
|
---|
30 | pass
|
---|
31 |
|
---|
32 | if err != expect:
|
---|
33 | print("error")
|
---|
34 | print("received %s" %(err))
|
---|
35 | print("expected %s" %(expect))
|
---|
36 | sys.exit(1)
|
---|
37 |
|
---|
38 | reader.SetErrorHandler(None,None)
|
---|
39 | if reader.GetErrorHandler() != (None,None):
|
---|
40 | print("GetErrorHandler failed")
|
---|
41 | sys.exit(1)
|
---|
42 |
|
---|
43 | #
|
---|
44 | # cleanup for memory allocation counting
|
---|
45 | #
|
---|
46 | del f
|
---|
47 | del input
|
---|
48 | del reader
|
---|
49 |
|
---|
50 | # Memory debug specific
|
---|
51 | libxml2.cleanupParser()
|
---|
52 | if libxml2.debugMemory(1) == 0:
|
---|
53 | print("OK")
|
---|
54 | else:
|
---|
55 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
56 | libxml2.dumpMemory()
|
---|