Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/defusedxml/ElementTree.py: 72%
71 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1# defusedxml
2#
3# Copyright (c) 2013 by Christian Heimes <christian@python.org>
4# Licensed to PSF under a Contributor Agreement.
5# See https://www.python.org/psf/license for licensing details.
6"""Defused xml.etree.ElementTree facade
7"""
8from __future__ import print_function, absolute_import
10import sys
11import warnings
12from xml.etree.ElementTree import ParseError
13from xml.etree.ElementTree import TreeBuilder as _TreeBuilder
14from xml.etree.ElementTree import parse as _parse
15from xml.etree.ElementTree import tostring
17from .common import PY3
19if PY3: 19 ↛ 22line 19 didn't jump to line 22, because the condition on line 19 was never false
20 import importlib
21else:
22 from xml.etree.ElementTree import XMLParser as _XMLParser
23 from xml.etree.ElementTree import iterparse as _iterparse
26from .common import (
27 DTDForbidden,
28 EntitiesForbidden,
29 ExternalReferenceForbidden,
30 _generate_etree_functions,
31)
33__origin__ = "xml.etree.ElementTree"
36def _get_py3_cls():
37 """Python 3.3 hides the pure Python code but defusedxml requires it.
39 The code is based on test.support.import_fresh_module().
40 """
41 pymodname = "xml.etree.ElementTree"
42 cmodname = "_elementtree"
44 pymod = sys.modules.pop(pymodname, None)
45 cmod = sys.modules.pop(cmodname, None)
47 sys.modules[cmodname] = None
48 try:
49 pure_pymod = importlib.import_module(pymodname)
50 finally:
51 # restore module
52 sys.modules[pymodname] = pymod
53 if cmod is not None: 53 ↛ 56line 53 didn't jump to line 56, because the condition on line 53 was never false
54 sys.modules[cmodname] = cmod
55 else:
56 sys.modules.pop(cmodname, None)
57 # restore attribute on original package
58 etree_pkg = sys.modules["xml.etree"]
59 if pymod is not None: 59 ↛ 61line 59 didn't jump to line 61, because the condition on line 59 was never false
60 etree_pkg.ElementTree = pymod
61 elif hasattr(etree_pkg, "ElementTree"):
62 del etree_pkg.ElementTree
64 _XMLParser = pure_pymod.XMLParser
65 _iterparse = pure_pymod.iterparse
66 # patch pure module to use ParseError from C extension
67 pure_pymod.ParseError = ParseError
69 return _XMLParser, _iterparse
72if PY3: 72 ↛ 76line 72 didn't jump to line 76, because the condition on line 72 was never false
73 _XMLParser, _iterparse = _get_py3_cls()
76_sentinel = object()
79class DefusedXMLParser(_XMLParser):
80 def __init__(
81 self,
82 html=_sentinel,
83 target=None,
84 encoding=None,
85 forbid_dtd=False,
86 forbid_entities=True,
87 forbid_external=True,
88 ):
89 # Python 2.x old style class
90 _XMLParser.__init__(self, target=target, encoding=encoding)
91 if html is not _sentinel: 91 ↛ 94line 91 didn't jump to line 94, because the condition on line 91 was never true
92 # the 'html' argument has been deprecated and ignored in all
93 # supported versions of Python. Python 3.8 finally removed it.
94 if html:
95 raise TypeError("'html=True' is no longer supported.")
96 else:
97 warnings.warn(
98 "'html' keyword argument is no longer supported. Pass "
99 "in arguments as keyword arguments.",
100 category=DeprecationWarning,
101 )
103 self.forbid_dtd = forbid_dtd
104 self.forbid_entities = forbid_entities
105 self.forbid_external = forbid_external
106 if PY3: 106 ↛ 109line 106 didn't jump to line 109, because the condition on line 106 was never false
107 parser = self.parser
108 else:
109 parser = self._parser
110 if self.forbid_dtd: 110 ↛ 111line 110 didn't jump to line 111, because the condition on line 110 was never true
111 parser.StartDoctypeDeclHandler = self.defused_start_doctype_decl
112 if self.forbid_entities: 112 ↛ 115line 112 didn't jump to line 115, because the condition on line 112 was never false
113 parser.EntityDeclHandler = self.defused_entity_decl
114 parser.UnparsedEntityDeclHandler = self.defused_unparsed_entity_decl
115 if self.forbid_external: 115 ↛ exitline 115 didn't return from function '__init__', because the condition on line 115 was never false
116 parser.ExternalEntityRefHandler = self.defused_external_entity_ref_handler
118 def defused_start_doctype_decl(self, name, sysid, pubid, has_internal_subset):
119 raise DTDForbidden(name, sysid, pubid)
121 def defused_entity_decl(
122 self, name, is_parameter_entity, value, base, sysid, pubid, notation_name
123 ):
124 raise EntitiesForbidden(name, value, base, sysid, pubid, notation_name)
126 def defused_unparsed_entity_decl(self, name, base, sysid, pubid, notation_name):
127 # expat 1.2
128 raise EntitiesForbidden(name, None, base, sysid, pubid, notation_name) # pragma: no cover
130 def defused_external_entity_ref_handler(self, context, base, sysid, pubid):
131 raise ExternalReferenceForbidden(context, base, sysid, pubid)
134# aliases
135# XMLParse is a typo, keep it for backwards compatibility
136XMLTreeBuilder = XMLParse = XMLParser = DefusedXMLParser
138parse, iterparse, fromstring = _generate_etree_functions(
139 DefusedXMLParser, _TreeBuilder, _parse, _iterparse
140)
141XML = fromstring
144__all__ = [
145 "ParseError",
146 "XML",
147 "XMLParse",
148 "XMLParser",
149 "XMLTreeBuilder",
150 "fromstring",
151 "iterparse",
152 "parse",
153 "tostring",
154]