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