Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/xml/sax/__init__.py: 33%
51 statements
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
« prev ^ index » next coverage.py v7.0.5, created at 2023-01-17 06:13 +0000
1"""Simple API for XML (SAX) implementation for Python.
3This module provides an implementation of the SAX 2 interface;
4information about the Java version of the interface can be found at
5http://www.megginson.com/SAX/. The Python version of the interface is
6documented at <...>.
8This package contains the following modules:
10handler -- Base classes and constants which define the SAX 2 API for
11 the 'client-side' of SAX for Python.
13saxutils -- Implementation of the convenience classes commonly used to
14 work with SAX.
16xmlreader -- Base classes and constants which define the SAX 2 API for
17 the parsers used with SAX for Python.
19expatreader -- Driver that allows use of the Expat parser with SAX.
20"""
22from .xmlreader import InputSource
23from .handler import ContentHandler, ErrorHandler
24from ._exceptions import SAXException, SAXNotRecognizedException, \
25 SAXParseException, SAXNotSupportedException, \
26 SAXReaderNotAvailable
29def parse(source, handler, errorHandler=ErrorHandler()):
30 parser = make_parser()
31 parser.setContentHandler(handler)
32 parser.setErrorHandler(errorHandler)
33 parser.parse(source)
35def parseString(string, handler, errorHandler=ErrorHandler()):
36 import io
37 if errorHandler is None:
38 errorHandler = ErrorHandler()
39 parser = make_parser()
40 parser.setContentHandler(handler)
41 parser.setErrorHandler(errorHandler)
43 inpsrc = InputSource()
44 if isinstance(string, str):
45 inpsrc.setCharacterStream(io.StringIO(string))
46 else:
47 inpsrc.setByteStream(io.BytesIO(string))
48 parser.parse(inpsrc)
50# this is the parser list used by the make_parser function if no
51# alternatives are given as parameters to the function
53default_parser_list = ["xml.sax.expatreader"]
55# tell modulefinder that importing sax potentially imports expatreader
56_false = 0
57if _false:
58 import xml.sax.expatreader
60import os, sys
61if not sys.flags.ignore_environment and "PY_SAX_PARSER" in os.environ:
62 default_parser_list = os.environ["PY_SAX_PARSER"].split(",")
63del os
65_key = "python.xml.sax.parser"
66if sys.platform[:4] == "java" and sys.registry.containsKey(_key):
67 default_parser_list = sys.registry.getProperty(_key).split(",")
70def make_parser(parser_list=()):
71 """Creates and returns a SAX parser.
73 Creates the first parser it is able to instantiate of the ones
74 given in the iterable created by chaining parser_list and
75 default_parser_list. The iterables must contain the names of Python
76 modules containing both a SAX parser and a create_parser function."""
78 for parser_name in list(parser_list) + default_parser_list:
79 try:
80 return _create_parser(parser_name)
81 except ImportError as e:
82 import sys
83 if parser_name in sys.modules:
84 # The parser module was found, but importing it
85 # failed unexpectedly, pass this exception through
86 raise
87 except SAXReaderNotAvailable:
88 # The parser module detected that it won't work properly,
89 # so try the next one
90 pass
92 raise SAXReaderNotAvailable("No parsers found", None)
94# --- Internal utility methods used by make_parser
96if sys.platform[ : 4] == "java":
97 def _create_parser(parser_name):
98 from org.python.core import imp
99 drv_module = imp.importName(parser_name, 0, globals())
100 return drv_module.create_parser()
102else:
103 def _create_parser(parser_name):
104 drv_module = __import__(parser_name,{},{},['create_parser'])
105 return drv_module.create_parser()
107del sys