import libxml2 libxml2.debugMemory(True) d = libxml2.parseDoc("") for expr in ("/fish[.=$var]", "local-name('one','two')", "~?)%^*("): try: result = d.xpathEval(expr) except libxml2.xpathError, e: print "xpathError raised:", e else: raise ValueError("Unexpected success", expr, result) d.freeDoc() libxml2.cleanupParser() leakedbytes = libxml2.debugMemory(True) if leakedbytes == 0: print "OK" else: print "Memory leak", leakedbytes, "bytes" # drop file to .memdump file in cwd, but won't work if not compiled in libxml2.dumpMemory()