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