diff options
Diffstat (limited to 'Lib/xml')
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 10 | ||||
-rw-r--r-- | Lib/xml/sax/expatreader.py | 17 |
2 files changed, 19 insertions, 8 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 9c129d8..edf2581 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1439,13 +1439,13 @@ class XMLParser: def __init__(self, html=0, target=None, encoding=None): try: from xml.parsers import expat - except ModuleNotFoundError: + except ImportError: try: import pyexpat as expat - except ModuleNotFoundError: - raise ModuleNotFoundError( - "No module named expat; use SimpleXMLTreeBuilder instead", - name='expat') + except ImportError: + raise ImportError( + "No module named expat; use SimpleXMLTreeBuilder instead" + ) parser = expat.ParserCreate(encoding, "}") if target is None: target = TreeBuilder() diff --git a/Lib/xml/sax/expatreader.py b/Lib/xml/sax/expatreader.py index 8619747..a227cda 100644 --- a/Lib/xml/sax/expatreader.py +++ b/Lib/xml/sax/expatreader.py @@ -20,7 +20,7 @@ del sys try: from xml.parsers import expat -except ModuleNotFoundError: +except ImportError: raise SAXReaderNotAvailable("expat not supported", None) else: if not hasattr(expat, "ParserCreate"): @@ -30,7 +30,18 @@ from xml.sax import xmlreader, saxutils, handler AttributesImpl = xmlreader.AttributesImpl AttributesNSImpl = xmlreader.AttributesNSImpl -import weakref +# If we're using a sufficiently recent version of Python, we can use +# weak references to avoid cycles between the parser and content +# handler, otherwise we'll just have to pretend. +try: + import _weakref +except ImportError: + def _mkproxy(o): + return o +else: + import weakref + _mkproxy = weakref.proxy + del weakref, _weakref # --- ExpatLocator @@ -41,7 +52,7 @@ class ExpatLocator(xmlreader.Locator): a circular reference between the parser and the content handler. """ def __init__(self, parser): - self._ref = weakref.proxy(parser) + self._ref = _mkproxy(parser) def getColumnNumber(self): parser = self._ref |