sync.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/python -u
  2. import sys
  3. import libxml2
  4. # Memory debug specific
  5. libxml2.debugMemory(1)
  6. log = ""
  7. class callback:
  8. def startDocument(self):
  9. global log
  10. log = log + "startDocument:"
  11. def endDocument(self):
  12. global log
  13. log = log + "endDocument:"
  14. def startElement(self, tag, attrs):
  15. global log
  16. log = log + "startElement %s %s:" % (tag, attrs)
  17. def endElement(self, tag):
  18. global log
  19. log = log + "endElement %s:" % (tag)
  20. def characters(self, data):
  21. global log
  22. log = log + "characters: %s:" % (data)
  23. def warning(self, msg):
  24. global log
  25. log = log + "warning: %s:" % (msg)
  26. def error(self, msg):
  27. global log
  28. log = log + "error: %s:" % (msg)
  29. def fatalError(self, msg):
  30. global log
  31. log = log + "fatalError: %s:" % (msg)
  32. handler = callback()
  33. log=""
  34. chunk="""<foo><bar2/>"""
  35. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  36. ctxt.parseChunk(chunk, len(chunk), 0)
  37. ctxt=None
  38. reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
  39. if log != reference:
  40. print "Error got: %s" % log
  41. print "Exprected: %s" % reference
  42. sys.exit(1)
  43. log=""
  44. chunk="""<foo><bar2></bar2>"""
  45. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  46. ctxt.parseChunk(chunk, len(chunk), 0)
  47. ctxt=None
  48. reference = "startDocument:startElement foo None:startElement bar2 None:endElement bar2:"
  49. if log != reference:
  50. print "Error got: %s" % log
  51. print "Exprected: %s" % reference
  52. sys.exit(1)
  53. log=""
  54. chunk="""<foo><bar2>"""
  55. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  56. ctxt.parseChunk(chunk, len(chunk), 0)
  57. ctxt=None
  58. reference = "startDocument:startElement foo None:startElement bar2 None:"
  59. if log != reference:
  60. print "Error got: %s" % log
  61. print "Exprected: %s" % reference
  62. sys.exit(1)
  63. log=""
  64. chunk="""<foo><bar2 a="1" b='2' />"""
  65. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  66. ctxt.parseChunk(chunk, len(chunk), 0)
  67. ctxt=None
  68. reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
  69. if log != reference:
  70. print "Error got: %s" % log
  71. print "Exprected: %s" % reference
  72. sys.exit(1)
  73. log=""
  74. chunk="""<foo><bar2 a="1" b='2' >"""
  75. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  76. ctxt.parseChunk(chunk, len(chunk), 0)
  77. ctxt=None
  78. reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:"
  79. if log != reference:
  80. print "Error got: %s" % log
  81. print "Exprected: %s" % reference
  82. sys.exit(1)
  83. log=""
  84. chunk="""<foo><bar2 a="1" b='2' ></bar2>"""
  85. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  86. ctxt.parseChunk(chunk, len(chunk), 0)
  87. ctxt=None
  88. reference = "startDocument:startElement foo None:startElement bar2 {'a': '1', 'b': '2'}:endElement bar2:"
  89. if log != reference:
  90. print "Error got: %s" % log
  91. print "Exprected: %s" % reference
  92. sys.exit(1)
  93. log=""
  94. chunk="""<foo><bar2 a="b='1' />"""
  95. ctxt = libxml2.createPushParser(handler, None, 0, "test.xml")
  96. ctxt.parseChunk(chunk, len(chunk), 0)
  97. ctxt=None
  98. reference = "startDocument:startElement foo None:"
  99. if log != reference:
  100. print "Error got: %s" % log
  101. print "Exprected: %s" % reference
  102. sys.exit(1)
  103. # Memory debug specific
  104. libxml2.cleanupParser()
  105. if libxml2.debugMemory(1) == 0:
  106. print "OK"
  107. else:
  108. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  109. libxml2.dumpMemory()