Skip to content

Commit 4fb4387

Browse files
committed
[_802] lazy definition for irods.message._XML_parser
1 parent 4da5432 commit 4fb4387

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

irods/message/__init__.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import socket
88
import struct
9+
import sys
910
import threading
1011
import xml.etree.ElementTree as ET_xml
1112
from collections import namedtuple
@@ -95,24 +96,24 @@ class BadXMLSpec(RuntimeError):
9596

9697
def 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

101102
def 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

106113
def 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

174175
logger = 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

192208
UNICODE = str

0 commit comments

Comments
 (0)