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