Skip to content

Commit f3a4fa7

Browse files
d-w-moorealanking
authored andcommitted
[#802] prevent cyclic imports incurred by pydoc
1 parent 1b32105 commit f3a4fa7

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

irods/message/__init__.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
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
1213
from typing import Optional
1314
from warnings import warn
1415

15-
import defusedxml.ElementTree as ET_secure_xml
16-
1716
import irods.exception as ex
1817

1918
from . import quasixml as ET_quasi_xml
@@ -97,24 +96,22 @@ class BadXMLSpec(RuntimeError):
9796

9897
def current_XML_parser(get_module=False):
9998
d = getattr(_thrlocal, "xml_type", _default_XML)
100-
return d if not get_module else _XML_parsers[d]
99+
return d if not get_module else _get_XML_parser_for(d)
101100

102101

103102
def default_XML_parser(get_module=False):
104103
d = _default_XML
105-
return d if not get_module else _XML_parsers[d]
104+
return d if not get_module else _get_XML_parser_for(d)
105+
106+
107+
def _get_XML_parser_for(d):
108+
return sys.modules[__name__]._XML_parser[d]
106109

107110

108111
def string_for_XML_parser(parser_enum):
109112
return PARSER_TYPE_STRINGS[parser_enum]
110113

111114

112-
_XML_parsers = {
113-
XML_Parser_Type.STANDARD_XML: ET_xml,
114-
XML_Parser_Type.QUASI_XML: ET_quasi_xml,
115-
XML_Parser_Type.SECURE_XML: ET_secure_xml,
116-
}
117-
118115
_reversed_XML_strings_lookup = {v: k for k, v in _XML_strings.items()}
119116

120117

@@ -168,9 +165,9 @@ def ET(xml_type=(), server_version=None):
168165
_thrlocal.irods_server_version = tuple(
169166
server_version
170167
) # A default server version for Quasi-XML parsing is set (from the environment) and
171-
return _XML_parsers[
168+
return _get_XML_parser_for(
172169
current_XML_parser()
173-
] # applies to all threads in which ET() has not been called to update the value.
170+
) # applies to all threads in which ET() has not been called to update the value.
174171

175172

176173
logger = logging.getLogger(__name__)
@@ -188,7 +185,23 @@ def __getattr__(name):
188185
if name in _deprecated_names:
189186
warn(f"{name} is deprecated", DeprecationWarning, stacklevel=2)
190187
return _deprecated_names[name]
191-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
188+
189+
global __XML_parser
190+
# Define the _XML_parser module variable only when accessed (#802).
191+
if name == '_XML_parser':
192+
impl = globals().get('__XML_parser')
193+
if not impl:
194+
import defusedxml.ElementTree as ET_secure_xml
195+
196+
impl = __XML_parser = {
197+
XML_Parser_Type.STANDARD_XML: ET_xml,
198+
XML_Parser_Type.QUASI_XML: ET_quasi_xml,
199+
XML_Parser_Type.SECURE_XML: ET_secure_xml,
200+
}
201+
return impl
202+
203+
message = f"module {__name__!r} has no attribute {name!r}"
204+
raise AttributeError(message)
192205

193206

194207
UNICODE = str

irods/results.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from prettytable import PrettyTable
21
from irods.models import ModelBase
32

43

@@ -14,13 +13,17 @@ def __init__(self, raw):
1413
self.continue_index = 0
1514

1615
def __str__(self):
16+
from prettytable import PrettyTable
17+
1718
table = PrettyTable()
1819
for col in self.cols:
1920
table.add_column(ModelBase.columns()[col.attriInx].icat_key, col.value)
2021
table.align = "l"
2122
return table.get_string()
2223

2324
def get_html_string(self, *args, **kwargs):
25+
from prettytable import PrettyTable
26+
2427
table = PrettyTable()
2528
for col in self.cols:
2629
table.add_column(ModelBase.columns()[col.attriInx].icat_key, col.value)

0 commit comments

Comments
 (0)