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