xref: /aosp_15_r20/external/libxml2/python/tests/error.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='--> I/O --> warning : --> failed to load "missing.xml": No such file or directory\n'
14err=""
15def callback(ctx, str):
16     global err
17
18     err = err + "%s %s" % (ctx, str)
19
20got_exc = 0
21libxml2.registerErrorHandler(callback, "-->")
22try:
23    doc = libxml2.parseFile("missing.xml")
24except libxml2.parserError:
25    got_exc = 1
26
27if got_exc == 0:
28    print("Failed to get a parser exception")
29    sys.exit(1)
30
31if err != expect:
32    print("error")
33    print("received %s" %(err))
34    print("expected %s" %(expect))
35    sys.exit(1)
36
37i = 10000
38while i > 0:
39    try:
40        doc = libxml2.parseFile("missing.xml")
41    except libxml2.parserError:
42        got_exc = 1
43    err = ""
44    i = i - 1
45
46# Memory debug specific
47libxml2.cleanupParser()
48if libxml2.debugMemory(1) == 0:
49    print("OK")
50else:
51    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
52