/usr/share/doc/python-libxml2/examples/reader3.py is in python-libxml2 2.7.8.dfsg-5.1ubuntu4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #!/usr/bin/python -u
#
# this tests the entities substitutions with the XmlTextReader interface
#
import sys
import StringIO
import libxml2
docstr="""<?xml version='1.0'?>
<!DOCTYPE doc [
<!ENTITY tst "<p>test</p>">
]>
<doc>&tst;</doc>"""
# Memory debug specific
libxml2.debugMemory(1)
#
# First test, normal don't substitute entities.
#
f = StringIO.StringIO(docstr)
input = libxml2.inputBuffer(f)
reader = input.newTextReader("test_noent")
ret = reader.Read()
if ret != 1:
print "Error reading to root"
sys.exit(1)
if reader.Name() == "doc" or reader.NodeType() == 10:
ret = reader.Read()
if ret != 1:
print "Error reading to root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 1:
print "test_normal: Error reading the root element"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_normal: Error reading to the entity"
sys.exit(1)
if reader.Name() != "tst" or reader.NodeType() != 5:
print "test_normal: Error reading the entity"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_normal: Error reading to the end of root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 15:
print "test_normal: Error reading the end of the root element"
sys.exit(1)
ret = reader.Read()
if ret != 0:
print "test_normal: Error detecting the end"
sys.exit(1)
#
# Second test, completely substitute the entities.
#
f = StringIO.StringIO(docstr)
input = libxml2.inputBuffer(f)
reader = input.newTextReader("test_noent")
reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES, 1)
ret = reader.Read()
if ret != 1:
print "Error reading to root"
sys.exit(1)
if reader.Name() == "doc" or reader.NodeType() == 10:
ret = reader.Read()
if ret != 1:
print "Error reading to root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 1:
print "test_noent: Error reading the root element"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the entity content"
sys.exit(1)
if reader.Name() != "p" or reader.NodeType() != 1:
print "test_noent: Error reading the p element from entity"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the text node"
sys.exit(1)
if reader.NodeType() != 3 or reader.Value() != "test":
print "test_noent: Error reading the text node"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the end of p element"
sys.exit(1)
if reader.Name() != "p" or reader.NodeType() != 15:
print "test_noent: Error reading the end of the p element"
sys.exit(1)
ret = reader.Read()
if ret != 1:
print "test_noent: Error reading to the end of root"
sys.exit(1)
if reader.Name() != "doc" or reader.NodeType() != 15:
print "test_noent: Error reading the end of the root element"
sys.exit(1)
ret = reader.Read()
if ret != 0:
print "test_noent: Error detecting the end"
sys.exit(1)
#
# third test, crazy stuff about empty element in external parsed entities
#
s = """<!DOCTYPE struct [
<!ENTITY simplestruct2.ent SYSTEM "simplestruct2.ent">
]>
<struct>&simplestruct2.ent;</struct>
"""
expect="""10 struct 0 0
1 struct 0 0
1 descr 1 1
15 struct 0 0
"""
res=""
simplestruct2_ent="""<descr/>"""
def myResolver(URL, ID, ctxt):
if URL == "simplestruct2.ent":
return(StringIO.StringIO(simplestruct2_ent))
return None
libxml2.setEntityLoader(myResolver)
input = libxml2.inputBuffer(StringIO.StringIO(s))
reader = input.newTextReader("test3")
reader.SetParserProp(libxml2.PARSER_SUBST_ENTITIES,1)
while reader.Read() == 1:
res = res + "%s %s %d %d\n" % (reader.NodeType(),reader.Name(),
reader.Depth(),reader.IsEmptyElement())
if res != expect:
print "test3 failed: unexpected output"
print res
sys.exit(1)
#
# cleanup
#
del f
del input
del reader
# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
print "OK"
else:
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
libxml2.dumpMemory()
|