1#!/usr/bin/python
2import sys, libxml2
3
4libxml2.debugMemory(True)
5
6expect="""--> Invalid expression
7--> xmlXPathEval: evaluation failed
8--> Invalid expression
9--> xmlXPathEval: evaluation failed
10--> Invalid expression
11--> xmlXPathEval: evaluation failed
12--> Invalid expression
13--> xmlXPathEval: evaluation failed
14--> Invalid expression
15--> xmlXPathEval: evaluation failed
16--> Invalid expression
17--> xmlXPathEval: evaluation failed
18--> Invalid expression
19--> xmlXPathEval: evaluation failed
20--> Invalid expression
21--> xmlXPathEval: evaluation failed
22--> Invalid expression
23--> xmlXPathEval: evaluation failed
24--> Invalid expression
25--> xmlXPathEval: evaluation failed
26"""
27err=""
28def callback(ctx, str):
29     global err
30
31     err = err + "%s %s" % (ctx, str)
32
33libxml2.registerErrorHandler(callback, "-->")
34
35doc = libxml2.parseDoc("<fish/>")
36ctxt = doc.xpathNewContext()
37ctxt.setContextNode(doc)
38badexprs = (
39	":false()", "bad:()", "bad(:)", ":bad(:)", "bad:(:)", "bad:bad(:)",
40	"a:/b", "/c:/d", "//e:/f", "g://h"
41	)
42for expr in badexprs:
43	try:
44		ctxt.xpathEval(expr)
45	except libxml2.xpathError:
46	        pass
47	else:
48		print("Unexpectedly legal expression:", expr)
49ctxt.xpathFreeContext()
50doc.freeDoc()
51
52if err != expect:
53    print("error")
54    print("received %s" %(err))
55    print("expected %s" %(expect))
56    sys.exit(1)
57
58libxml2.cleanupParser()
59leakedbytes = libxml2.debugMemory(True)
60if leakedbytes == 0:
61	print("OK")
62else:
63	print("Memory leak", leakedbytes, "bytes")
64	# drop file to .memdump file in cwd, but won't work if not compiled in
65	libxml2.dumpMemory()
66