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