| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# this test exercise the XPath basic engine, parser, etc, and |
| 4 |
# allows to detect memory leaks |
| 5 |
# |
| 6 |
import sys |
| 7 |
import setup_test |
| 8 |
import libxml2 |
| 9 |
|
| 10 |
instance="""<?xml version="1.0"?> |
| 11 |
<tag xmlns:foo='urn:foo' xmlns:bar='urn:bar' xmlns:baz='urn:baz' />""" |
| 12 |
|
| 13 |
def namespaceDefs(node): |
| 14 |
n = node.nsDefs() |
| 15 |
while n: |
| 16 |
yield n |
| 17 |
n = n.next |
| 18 |
|
| 19 |
def checkNamespaceDefs(node, count): |
| 20 |
nsList = list(namespaceDefs(node)) |
| 21 |
#print nsList |
| 22 |
if len(nsList) != count : |
| 23 |
raise Exception("Error: saw %d namespace declarations. Expected %d" % (len(nsList), count)) |
| 24 |
|
| 25 |
# Memory debug specific |
| 26 |
libxml2.debugMemory(1) |
| 27 |
|
| 28 |
# Remove single namespace |
| 29 |
doc = libxml2.parseDoc(instance) |
| 30 |
node = doc.getRootElement() |
| 31 |
checkNamespaceDefs(node, 3) |
| 32 |
ns = node.removeNsDef('urn:bar') |
| 33 |
checkNamespaceDefs(node, 2) |
| 34 |
ns.freeNsList() |
| 35 |
doc.freeDoc() |
| 36 |
|
| 37 |
# Remove all namespaces |
| 38 |
doc = libxml2.parseDoc(instance) |
| 39 |
node = doc.getRootElement() |
| 40 |
checkNamespaceDefs(node, 3) |
| 41 |
ns = node.removeNsDef(None) |
| 42 |
checkNamespaceDefs(node, 0) |
| 43 |
ns.freeNsList() |
| 44 |
doc.freeDoc() |
| 45 |
|
| 46 |
# Remove a namespace referred to by a child |
| 47 |
doc = libxml2.newDoc("1.0") |
| 48 |
root = doc.newChild(None, "root", None) |
| 49 |
namespace = root.newNs("http://example.com/sample", "s") |
| 50 |
child = root.newChild(namespace, "child", None) |
| 51 |
root.removeNsDef("http://example.com/sample") |
| 52 |
doc.reconciliateNs(root) |
| 53 |
namespace.freeNsList() |
| 54 |
doc.serialize() # This should not segfault |
| 55 |
doc.freeDoc() |
| 56 |
|
| 57 |
# Memory debug specific |
| 58 |
libxml2.cleanupParser() |
| 59 |
if libxml2.debugMemory(1) == 0: |
| 60 |
print("OK") |
| 61 |
else: |
| 62 |
print("Memory leak %d bytes" % (libxml2.debugMemory(1))) |