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