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