1 | #!/usr/bin/python -u
|
---|
2 | # -*- coding: ISO-8859-1 -*-
|
---|
3 | #
|
---|
4 | # this tests the next API of the XmlTextReader interface
|
---|
5 | #
|
---|
6 | import libxml2
|
---|
7 | import sys
|
---|
8 | try:
|
---|
9 | import StringIO
|
---|
10 | str_io = StringIO.StringIO
|
---|
11 | except:
|
---|
12 | import io
|
---|
13 | str_io = io.StringIO
|
---|
14 |
|
---|
15 | # Memory debug specific
|
---|
16 | libxml2.debugMemory(1)
|
---|
17 |
|
---|
18 | f = str_io("""<a><b><c /></b><d>content of d</d></a>""")
|
---|
19 | input = libxml2.inputBuffer(f)
|
---|
20 | reader = input.newTextReader("test_next")
|
---|
21 | ret = reader.Read()
|
---|
22 | if ret != 1:
|
---|
23 | print("test_next: Error reading to first element")
|
---|
24 | sys.exit(1)
|
---|
25 | if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
|
---|
26 | reader.NodeType() != 1 or reader.HasAttributes() != 0:
|
---|
27 | print("test_next: Error reading the first element")
|
---|
28 | sys.exit(1)
|
---|
29 | ret = reader.Read()
|
---|
30 | if ret != 1:
|
---|
31 | print("test_next: Error reading to second element")
|
---|
32 | sys.exit(1)
|
---|
33 | if reader.Name() != "b" or reader.IsEmptyElement() != 0 or \
|
---|
34 | reader.NodeType() != 1 or reader.HasAttributes() != 0:
|
---|
35 | print("test_next: Error reading the second element")
|
---|
36 | sys.exit(1)
|
---|
37 | ret = reader.Read()
|
---|
38 | if ret != 1:
|
---|
39 | print("test_next: Error reading to third element")
|
---|
40 | sys.exit(1)
|
---|
41 | if reader.Name() != "c" or reader.NodeType() != 1 or \
|
---|
42 | reader.HasAttributes() != 0:
|
---|
43 | print("test_next: Error reading the third element")
|
---|
44 | sys.exit(1)
|
---|
45 | ret = reader.Read()
|
---|
46 | if ret != 1:
|
---|
47 | print("test_next: Error reading to end of third element")
|
---|
48 | sys.exit(1)
|
---|
49 | if reader.Name() != "b" or reader.NodeType() != 15:
|
---|
50 | print("test_next: Error reading to end of second element")
|
---|
51 | sys.exit(1)
|
---|
52 | ret = reader.Next()
|
---|
53 | if ret != 1:
|
---|
54 | print("test_next: Error moving to third element")
|
---|
55 | sys.exit(1)
|
---|
56 | if reader.Name() != "d" or reader.IsEmptyElement() != 0 or \
|
---|
57 | reader.NodeType() != 1 or reader.HasAttributes() != 0:
|
---|
58 | print("test_next: Error reading third element")
|
---|
59 | sys.exit(1)
|
---|
60 | ret = reader.Next()
|
---|
61 | if ret != 1:
|
---|
62 | print("test_next: Error reading to end of first element")
|
---|
63 | sys.exit(1)
|
---|
64 | if reader.Name() != "a" or reader.IsEmptyElement() != 0 or \
|
---|
65 | reader.NodeType() != 15 or reader.HasAttributes() != 0:
|
---|
66 | print("test_next: Error reading the end of first element")
|
---|
67 | sys.exit(1)
|
---|
68 | ret = reader.Read()
|
---|
69 | if ret != 0:
|
---|
70 | print("test_next: Error reading to end of document")
|
---|
71 | sys.exit(1)
|
---|
72 |
|
---|
73 | #
|
---|
74 | # cleanup for memory allocation counting
|
---|
75 | #
|
---|
76 | del f
|
---|
77 | del input
|
---|
78 | del reader
|
---|
79 |
|
---|
80 | # Memory debug specific
|
---|
81 | libxml2.cleanupParser()
|
---|
82 | if libxml2.debugMemory(1) == 0:
|
---|
83 | print("OK")
|
---|
84 | else:
|
---|
85 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|
86 | libxml2.dumpMemory()
|
---|