66import os
77import socket
88import struct
9+ import sys
910import threading
1011import xml .etree .ElementTree as ET_xml
1112from collections import namedtuple
@@ -95,24 +96,24 @@ class BadXMLSpec(RuntimeError):
9596
9697def current_XML_parser (get_module = False ):
9798 d = getattr (_thrlocal , "xml_type" , _default_XML )
98- return d if not get_module else _XML_parsers [ d ]
99+ return d if not get_module else _get_XML_parser_for ( d )
99100
100101
101102def default_XML_parser (get_module = False ):
102103 d = _default_XML
103- return d if not get_module else _XML_parsers [ d ]
104+ return d if not get_module else _get_XML_parser_for ( d )
104105
106+ def _get_XML_parser_for (d ):
107+ return sys .modules [__name__ ]._XML_parser [d ]
108+
109+ #def __getattr__(name):
110+ # msg = f'module {__name__!r} has no attribute {name!r}'
111+ # raise AttributeError(msg)
105112
106113def string_for_XML_parser (parser_enum ):
107114 return PARSER_TYPE_STRINGS [parser_enum ]
108115
109116
110- _XML_parsers = {
111- XML_Parser_Type .STANDARD_XML : ET_xml ,
112- XML_Parser_Type .QUASI_XML : ET_quasi_xml ,
113- XML_Parser_Type .SECURE_XML : ET_secure_xml ,
114- }
115-
116117_reversed_XML_strings_lookup = {v : k for k , v in _XML_strings .items ()}
117118
118119
@@ -166,9 +167,9 @@ def ET(xml_type=(), server_version=None):
166167 _thrlocal .irods_server_version = tuple (
167168 server_version
168169 ) # A default server version for Quasi-XML parsing is set (from the environment) and
169- return _XML_parsers [
170+ return _get_XML_parser_for (
170171 current_XML_parser ()
171- ] # applies to all threads in which ET() has not been called to update the value.
172+ ) # applies to all threads in which ET() has not been called to update the value.
172173
173174
174175logger = logging .getLogger (__name__ )
@@ -186,7 +187,22 @@ def __getattr__(name):
186187 if name in _deprecated_names :
187188 warn (f"{ name } is deprecated" , DeprecationWarning , stacklevel = 2 )
188189 return _deprecated_names [name ]
189- raise AttributeError (f"module { __name__ !r} has no attribute { name !r} " )
190+
191+ global __XML_parser
192+ # Define the _XML_parser module variable only when accessed (#802).
193+ if name == '_XML_parser' :
194+ impl = globals ().get ('__XML_parser' )
195+ if not impl :
196+ import defusedxml .ElementTree as ET_secure_xml
197+ impl = __XML_parser = {
198+ XML_Parser_Type .STANDARD_XML : ET_xml ,
199+ XML_Parser_Type .QUASI_XML : ET_quasi_xml ,
200+ XML_Parser_Type .SECURE_XML : ET_secure_xml ,
201+ }
202+ return impl
203+
204+ message = f"module { __name__ !r} has no attribute { name !r} "
205+ raise AttributeError (message )
190206
191207
192208UNICODE = str
0 commit comments