(root)/
libxml2-2.12.3/
python/
tests/
xpathext.py
       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  def foo(ctx, x):
      10      return x + 1
      11  
      12  def bar(ctx, x):
      13      return "%d" % (x + 2)
      14  
      15  doc = libxml2.parseFile("tst.xml")
      16  ctxt = doc.xpathNewContext()
      17  res = ctxt.xpathEval("//*")
      18  if len(res) != 2:
      19      print("xpath query: wrong node set size")
      20      sys.exit(1)
      21  if res[0].name != "doc" or res[1].name != "foo":
      22      print("xpath query: wrong node set value")
      23      sys.exit(1)
      24  
      25  libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
      26  libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
      27  i = 10000
      28  while i > 0:
      29      res = ctxt.xpathEval("foo(1)")
      30      if res != 2:
      31          print("xpath extension failure")
      32          sys.exit(1)
      33      i = i - 1
      34  i = 10000
      35  while i > 0:
      36      res = ctxt.xpathEval("bar(1)")
      37      if res != "3":
      38          print("xpath extension failure got %s expecting '3'")
      39          sys.exit(1)
      40      i = i - 1
      41  doc.freeDoc()
      42  ctxt.xpathFreeContext()
      43  
      44  # Memory debug specific
      45  libxml2.cleanupParser()
      46  if libxml2.debugMemory(1) == 0:
      47      print("OK")
      48  else:
      49      print("Memory leak %d bytes" % (libxml2.debugMemory(1)))