1 #!/usr/bin/python -u 2 import sys 3 import libxml2 4 5 #memory debug specific 6 libxml2.debugMemory(1) 7 8 # 9 # A document hosting the nodes returned from the extension function 10 # 11 mydoc = libxml2.newDoc("1.0") 12 13 def 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 25 doc = libxml2.parseFile("tst.xml") 26 ctxt = doc.xpathNewContext() 27 libxml2.registerXPathFunction(ctxt._o, "foo", None, foo) 28 res = ctxt.xpathEval("foo('hello')") 29 if type(res) != type([]): 30 print("Failed to return a nodeset") 31 sys.exit(1) 32 if len(res) != 1: 33 print("Unexpected nodeset size") 34 sys.exit(1) 35 node = res[0] 36 if node.name != 'p': 37 print("Unexpected nodeset element result") 38 sys.exit(1) 39 node = node.children 40 if node.type != 'text': 41 print("Unexpected nodeset element children type") 42 sys.exit(1) 43 if node.content != 'hello': 44 print("Unexpected nodeset element children content") 45 sys.exit(1) 46 47 doc.freeDoc() 48 mydoc.freeDoc() 49 ctxt.xpathFreeContext() 50 51 #memory debug specific 52 libxml2.cleanupParser() 53 if libxml2.debugMemory(1) == 0: 54 print("OK") 55 else: 56 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 57 libxml2.dumpMemory() 58