xref: /aosp_15_r20/external/libxml2/python/tests/ctxterror.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2#
3# This test exercise the redirection of error messages with a
4# functions defined in Python.
5#
6import sys
7import setup_test
8import libxml2
9
10# Memory debug specific
11libxml2.debugMemory(1)
12
13expect="""--> (3) xmlns: URI foo is not absolute
14--> (4) Opening and ending tag mismatch: x line 1 and y
15"""
16
17err=""
18def callback(arg,msg,severity,reserved):
19    global err
20    err = err + "%s (%d) %s" % (arg,severity,msg)
21
22s = """<x xmlns="foo"></y>"""
23
24parserCtxt = libxml2.createPushParser(None,"",0,"test.xml")
25parserCtxt.setErrorHandler(callback, "-->")
26if parserCtxt.getErrorHandler() != (callback,"-->"):
27    print("getErrorHandler failed")
28    sys.exit(1)
29parserCtxt.parseChunk(s,len(s),1)
30doc = parserCtxt.doc()
31doc.freeDoc()
32parserCtxt = None
33
34if err != expect:
35    print("error")
36    print("received %s" %(err))
37    print("expected %s" %(expect))
38    sys.exit(1)
39
40i = 10000
41while i > 0:
42    parserCtxt = libxml2.createPushParser(None,"",0,"test.xml")
43    parserCtxt.setErrorHandler(callback, "-->")
44    parserCtxt.parseChunk(s,len(s),1)
45    doc = parserCtxt.doc()
46    doc.freeDoc()
47    parserCtxt = None
48    err = ""
49    i = i - 1
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