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