indexes.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/python -u
  2. # -*- coding: ISO-8859-1 -*-
  3. import sys
  4. import libxml2
  5. # Memory debug specific
  6. libxml2.debugMemory(1)
  7. ctxt = None
  8. class callback:
  9. def __init__(self, startd, starte, ende, delta, endd):
  10. self.startd = startd
  11. self.starte = starte
  12. self.ende = ende
  13. self.endd = endd
  14. self.delta = delta
  15. self.count = 0
  16. def startDocument(self):
  17. global ctxt
  18. if ctxt.byteConsumed() != self.startd:
  19. print "document start at wrong index: %d expecting %d\n" % (
  20. ctxt.byteConsumed(), self.startd)
  21. sys.exit(1)
  22. def endDocument(self):
  23. global ctxt
  24. expect = self.ende + self.delta * (self.count - 1) + self.endd
  25. if ctxt.byteConsumed() != expect:
  26. print "document end at wrong index: %d expecting %d\n" % (
  27. ctxt.byteConsumed(), expect)
  28. sys.exit(1)
  29. def startElement(self, tag, attrs):
  30. global ctxt
  31. if tag == "bar1":
  32. expect = self.starte + self.delta * self.count
  33. if ctxt.byteConsumed() != expect:
  34. print "element start at wrong index: %d expecting %d\n" % (
  35. ctxt.byteConsumed(), expect)
  36. sys.exit(1)
  37. def endElement(self, tag):
  38. global ctxt
  39. if tag == "bar1":
  40. expect = self.ende + self.delta * self.count
  41. if ctxt.byteConsumed() != expect:
  42. print "element end at wrong index: %d expecting %d\n" % (
  43. ctxt.byteConsumed(), expect)
  44. sys.exit(1)
  45. self.count = self.count + 1
  46. def characters(self, data):
  47. pass
  48. #
  49. # First run a pure UTF-8 test
  50. #
  51. handler = callback(0, 13, 27, 198, 183)
  52. ctxt = libxml2.createPushParser(handler, "<foo>\n", 6, "test.xml")
  53. chunk = """ <bar1>chars1</bar1>
  54. <bar2>chars2</bar2>
  55. <bar3>chars3</bar3>
  56. <bar4>chars4</bar4>
  57. <bar5>chars5</bar5>
  58. <bar6>&lt;s6</bar6>
  59. <bar7>chars7</bar7>
  60. <bar8>&#38;8</bar8>
  61. <bar9>chars9</bar9>
  62. """
  63. i = 0
  64. while i < 10000:
  65. ctxt.parseChunk(chunk, len(chunk), 0)
  66. i = i + 1
  67. chunk = "</foo>"
  68. ctxt.parseChunk(chunk, len(chunk), 1)
  69. ctxt=None
  70. #
  71. # Then run a test relying on ISO-Latin-1
  72. #
  73. handler = callback(43, 57, 71, 198, 183)
  74. chunk="""<?xml version="1.0" encoding="ISO-8859-1"?>
  75. <foo>
  76. """
  77. ctxt = libxml2.createPushParser(handler, chunk, len(chunk), "test.xml")
  78. chunk = """ <bar1>chars1</bar1>
  79. <bar2>chars2</bar2>
  80. <bar3>chars3</bar3>
  81. <bar4>chàrs4</bar4>
  82. <bar5>chars5</bar5>
  83. <bar6>&lt;s6</bar6>
  84. <bar7>chars7</bar7>
  85. <bar8>&#38;8</bar8>
  86. <bar9>très 9</bar9>
  87. """
  88. i = 0
  89. while i < 10000:
  90. ctxt.parseChunk(chunk, len(chunk), 0)
  91. i = i + 1
  92. chunk = "</foo>"
  93. ctxt.parseChunk(chunk, len(chunk), 1)
  94. ctxt=None
  95. # Memory debug specific
  96. libxml2.cleanupParser()
  97. if libxml2.debugMemory(1) == 0:
  98. print "OK"
  99. else:
  100. print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  101. libxml2.dumpMemory()