(root)/
libxml2-2.12.3/
python/
tests/
tstxpath.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  called = ""
      10  
      11  def foo(ctx, x):
      12      global called
      13  
      14      #
      15      # test that access to the XPath evaluation contexts
      16      #
      17      pctxt = libxml2.xpathParserContext(_obj=ctx)
      18      ctxt = pctxt.context()
      19      called = ctxt.function()
      20      return x + 1
      21  
      22  def bar(ctxt, x):
      23      return "%d" % (x + 2)
      24  
      25  doc = libxml2.parseFile("tst.xml")
      26  ctxt = doc.xpathNewContext()
      27  res = ctxt.xpathEval("//*")
      28  if len(res) != 2:
      29      print("xpath query: wrong node set size")
      30      sys.exit(1)
      31  if res[0].name != "doc" or res[1].name != "foo":
      32      print("xpath query: wrong node set value")
      33      sys.exit(1)
      34  libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
      35  libxml2.registerXPathFunction(ctxt._o, "bar", None, bar)
      36  i = 10000
      37  while i > 0:
      38      res = ctxt.xpathEval("foo(1)")
      39      if res != 2:
      40          print("xpath extension failure")
      41          sys.exit(1)
      42      i = i - 1
      43  i = 10000
      44  while i > 0:
      45      res = ctxt.xpathEval("bar(1)")
      46      if res != "3":
      47          print("xpath extension failure got %s expecting '3'")
      48          sys.exit(1)
      49      i = i - 1
      50  doc.freeDoc()
      51  ctxt.xpathFreeContext()
      52  
      53  if called != "foo":
      54      print("xpath function: failed to access the context")
      55      print("xpath function: %s" % (called))
      56      sys.exit(1)
      57  
      58  #memory debug specific
      59  libxml2.cleanupParser()
      60  if libxml2.debugMemory(1) == 0:
      61      print("OK")
      62  else:
      63      print("Memory leak %d bytes" % (libxml2.debugMemory(1)))