1 | #!/usr/bin/env python3
|
---|
2 | import sys
|
---|
3 | import setup_test
|
---|
4 | import libxml2
|
---|
5 |
|
---|
6 | #memory debug specific
|
---|
7 | libxml2.debugMemory(1)
|
---|
8 |
|
---|
9 | #
|
---|
10 | # A document hosting the nodes returned from the extension function
|
---|
11 | #
|
---|
12 | mydoc = libxml2.newDoc("1.0")
|
---|
13 |
|
---|
14 | def foo(ctx, str):
|
---|
15 | global mydoc
|
---|
16 |
|
---|
17 | #
|
---|
18 | # test returning a node set works as expected
|
---|
19 | #
|
---|
20 | parent = mydoc.newDocNode(None, 'p', None)
|
---|
21 | mydoc.addChild(parent)
|
---|
22 | node = mydoc.newDocText(str)
|
---|
23 | parent.addChild(node)
|
---|
24 | return [parent]
|
---|
25 |
|
---|
26 | doc = libxml2.parseFile("tst.xml")
|
---|
27 | ctxt = doc.xpathNewContext()
|
---|
28 | libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
---|
29 | res = ctxt.xpathEval("foo('hello')")
|
---|
30 | if type(res) != type([]):
|
---|
31 | print("Failed to return a nodeset")
|
---|
32 | sys.exit(1)
|
---|
33 | if len(res) != 1:
|
---|
34 | print("Unexpected nodeset size")
|
---|
35 | sys.exit(1)
|
---|
36 | node = res[0]
|
---|
37 | if node.name != 'p':
|
---|
38 | print("Unexpected nodeset element result")
|
---|
39 | sys.exit(1)
|
---|
40 | node = node.children
|
---|
41 | if node.type != 'text':
|
---|
42 | print("Unexpected nodeset element children type")
|
---|
43 | sys.exit(1)
|
---|
44 | if node.content != 'hello':
|
---|
45 | print("Unexpected nodeset element children content")
|
---|
46 | sys.exit(1)
|
---|
47 |
|
---|
48 | doc.freeDoc()
|
---|
49 | mydoc.freeDoc()
|
---|
50 | ctxt.xpathFreeContext()
|
---|
51 |
|
---|
52 | #memory debug specific
|
---|
53 | libxml2.cleanupParser()
|
---|
54 | if libxml2.debugMemory(1) == 0:
|
---|
55 | print("OK")
|
---|
56 | else:
|
---|
57 | print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
|
---|