ctxterror.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/python -u
  2. #
  3. # This test exercise the redirection of error messages with a
  4. # functions defined in Python.
  5. #
  6. import sys
  7. import libxml2
  8. # Memory debug specific
  9. libxml2.debugMemory(1)
  10. expect="""--> (3) xmlns: URI foo is not absolute
  11. --> (4) Opening and ending tag mismatch: x line 0 and y
  12. """
  13. err=""
  14. def callback(arg,msg,severity,reserved):
  15. global err
  16. err = err + "%s (%d) %s" % (arg,severity,msg)
  17. s = """<x xmlns="foo"></y>"""
  18. parserCtxt = libxml2.createPushParser(None,"",0,"test.xml")
  19. parserCtxt.setErrorHandler(callback, "-->")
  20. if parserCtxt.getErrorHandler() != (callback,"-->"):
  21. print "getErrorHandler failed"
  22. sys.exit(1)
  23. parserCtxt.parseChunk(s,len(s),1)
  24. doc = parserCtxt.doc()
  25. doc.freeDoc()
  26. parserCtxt = None
  27. if err != expect:
  28. print "error"
  29. print "received %s" %(err)
  30. print "expected %s" %(expect)
  31. sys.exit(1)
  32. i = 10000
  33. while i > 0:
  34. parserCtxt = libxml2.createPushParser(None,"",0,"test.xml")
  35. parserCtxt.setErrorHandler(callback, "-->")
  36. parserCtxt.parseChunk(s,len(s),1)
  37. doc = parserCtxt.doc()
  38. doc.freeDoc()
  39. parserCtxt = None
  40. err = ""
  41. i = i - 1
  42. # Memory debug specific
  43. libxml2.cleanupParser()
  44. if libxml2.debugMemory(1) == 0:
  45. print "OK"
  46. else:
  47. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  48. libxml2.dumpMemory()