xpathext.py 1.1 KB

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