| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# this tests the basic APIs of the XmlTextReader interface |
| 4 |
# |
| 5 |
import setup_test |
| 6 |
import libxml2 |
| 7 |
import sys |
| 8 |
try: |
| 9 |
import StringIO |
| 10 |
str_io = StringIO.StringIO |
| 11 |
except: |
| 12 |
import io |
| 13 |
str_io = io.StringIO |
| 14 |
|
| 15 |
# Memory debug specific |
| 16 |
libxml2.debugMemory(1) |
| 17 |
|
| 18 |
expect="""--> (3) test1:1:xmlns: URI foo is not absolute |
| 19 |
--> (4) test1:1:Opening and ending tag mismatch: c line 1 and a |
| 20 |
""" |
| 21 |
err="" |
| 22 |
def myErrorHandler(arg,msg,severity,locator): |
| 23 |
global err |
| 24 |
err = err + "%s (%d) %s:%d:%s" % (arg,severity,locator.BaseURI(),locator.LineNumber(),msg) |
| 25 |
|
| 26 |
f = str_io("""<a xmlns="foo"><b b1="b1"/><c>content of c</a>""") |
| 27 |
input = libxml2.inputBuffer(f) |
| 28 |
reader = input.newTextReader("test1") |
| 29 |
reader.SetErrorHandler(myErrorHandler,"-->") |
| 30 |
while reader.Read() == 1: |
| 31 |
pass |
| 32 |
|
| 33 |
if err != expect: |
| 34 |
print("error") |
| 35 |
print("received %s" %(err)) |
| 36 |
print("expected %s" %(expect)) |
| 37 |
sys.exit(1) |
| 38 |
|
| 39 |
reader.SetErrorHandler(None,None) |
| 40 |
if reader.GetErrorHandler() != (None,None): |
| 41 |
print("GetErrorHandler failed") |
| 42 |
sys.exit(1) |
| 43 |
|
| 44 |
# |
| 45 |
# cleanup for memory allocation counting |
| 46 |
# |
| 47 |
del f |
| 48 |
del input |
| 49 |
del reader |
| 50 |
|
| 51 |
# Memory debug specific |
| 52 |
libxml2.cleanupParser() |
| 53 |
if libxml2.debugMemory(1) == 0: |
| 54 |
print("OK") |
| 55 |
else: |
| 56 |
print("Memory leak %d bytes" % (libxml2.debugMemory(1))) |