diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2012-02-13 10:03:30 (GMT) |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2012-02-13 10:03:30 (GMT) |
commit | a72a98f24a19928e31dcc4cab2cd2ad0f1846e11 (patch) | |
tree | ad529c9dde015c31cd942875a78de8e6eb52da58 /Lib/xml/etree | |
parent | d1c7b1afe8eb49bc7ef3bbcae391e763c695d195 (diff) | |
download | cpython-a72a98f24a19928e31dcc4cab2cd2ad0f1846e11.zip cpython-a72a98f24a19928e31dcc4cab2cd2ad0f1846e11.tar.gz cpython-a72a98f24a19928e31dcc4cab2cd2ad0f1846e11.tar.bz2 |
Issue #13988: cElementTree is deprecated and the _elementtree accelerator is automatically used whenever available.
Diffstat (limited to 'Lib/xml/etree')
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 90 | ||||
-rw-r--r-- | Lib/xml/etree/cElementTree.py | 154 |
2 files changed, 88 insertions, 156 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index f94c48c..93147eb 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -68,8 +68,9 @@ __all__ = [ "tostring", "tostringlist", "TreeBuilder", "VERSION", - "XML", + "XML", "XMLID", "XMLParser", "XMLTreeBuilder", + "register_namespace", ] VERSION = "1.3.0" @@ -148,9 +149,9 @@ class ParseError(SyntaxError): # @defreturn flag def iselement(element): - # FIXME: not sure about this; might be a better idea to look - # for tag/attrib/text attributes - return isinstance(element, Element) or hasattr(element, "tag") + # FIXME: not sure about this; + # isinstance(element, Element) or look for tag/attrib/text attributes + return hasattr(element, 'tag') ## # Element class. This class defines the Element interface, and @@ -1684,6 +1685,87 @@ class XMLParser: del self.target, self._parser # get rid of circular references return tree + +# Import the C accelerators +try: + # Element, SubElement, ParseError, TreeBuilder, XMLParser + from _elementtree import * +except ImportError: + pass +else: + # Overwrite 'ElementTree.parse' and 'iterparse' to use the C XMLParser + + class ElementTree(ElementTree): + def parse(self, source, parser=None): + close_source = False + if not hasattr(source, 'read'): + source = open(source, 'rb') + close_source = True + try: + if parser is not None: + while True: + data = source.read(65536) + if not data: + break + parser.feed(data) + self._root = parser.close() + else: + parser = XMLParser() + self._root = parser._parse(source) + return self._root + finally: + if close_source: + source.close() + + class iterparse: + root = None + def __init__(self, file, events=None): + self._close_file = False + if not hasattr(file, 'read'): + file = open(file, 'rb') + self._close_file = True + self._file = file + self._events = [] + self._index = 0 + self._error = None + self.root = self._root = None + b = TreeBuilder() + self._parser = XMLParser(b) + self._parser._setevents(self._events, events) + + def __next__(self): + while True: + try: + item = self._events[self._index] + self._index += 1 + return item + except IndexError: + pass + if self._error: + e = self._error + self._error = None + raise e + if self._parser is None: + self.root = self._root + if self._close_file: + self._file.close() + raise StopIteration + # load event buffer + del self._events[:] + self._index = 0 + data = self._file.read(16384) + if data: + try: + self._parser.feed(data) + except SyntaxError as exc: + self._error = exc + else: + self._root = self._parser.close() + self._parser = None + + def __iter__(self): + return self + # compatibility XMLTreeBuilder = XMLParser diff --git a/Lib/xml/etree/cElementTree.py b/Lib/xml/etree/cElementTree.py index aaef59e..368e679 100644 --- a/Lib/xml/etree/cElementTree.py +++ b/Lib/xml/etree/cElementTree.py @@ -1,153 +1,3 @@ -# Wrapper module for _elementtree +# Deprecated alias for xml.etree.ElementTree -from xml.etree.ElementTree import (ElementTree, dump, iselement, QName, - fromstringlist, - tostring, tostringlist, VERSION) -# These ones are not in ElementTree.__all__ -from xml.etree.ElementTree import ElementPath, register_namespace - -# Import the C accelerators: -# Element, SubElement, TreeBuilder, XMLParser, ParseError -from _elementtree import * - - -class ElementTree(ElementTree): - - def parse(self, source, parser=None): - close_source = False - if not hasattr(source, 'read'): - source = open(source, 'rb') - close_source = True - try: - if parser is not None: - while True: - data = source.read(65536) - if not data: - break - parser.feed(data) - self._root = parser.close() - else: - parser = XMLParser() - self._root = parser._parse(source) - return self._root - finally: - if close_source: - source.close() - - -class iterparse: - root = None - - def __init__(self, file, events=None): - self._close_file = False - if not hasattr(file, 'read'): - file = open(file, 'rb') - self._close_file = True - self._file = file - self._events = [] - self._index = 0 - self._error = None - self.root = self._root = None - b = TreeBuilder() - self._parser = XMLParser(b) - self._parser._setevents(self._events, events) - - def __next__(self): - while True: - try: - item = self._events[self._index] - self._index += 1 - return item - except IndexError: - pass - if self._error: - e = self._error - self._error = None - raise e - if self._parser is None: - self.root = self._root - if self._close_file: - self._file.close() - raise StopIteration - # load event buffer - del self._events[:] - self._index = 0 - data = self._file.read(16384) - if data: - try: - self._parser.feed(data) - except SyntaxError as exc: - self._error = exc - else: - self._root = self._parser.close() - self._parser = None - - def __iter__(self): - return self - - -# ============================================================================= -# -# Everything below this line can be removed -# after cElementTree is folded behind ElementTree. -# -# ============================================================================= - -from xml.etree.ElementTree import Comment as _Comment, PI as _PI - - -def parse(source, parser=None): - tree = ElementTree() - tree.parse(source, parser) - return tree - - -def XML(text, parser=None): - if not parser: - parser = XMLParser() - parser = XMLParser() - parser.feed(text) - return parser.close() - - -def XMLID(text, parser=None): - tree = XML(text, parser=parser) - ids = {} - for elem in tree.iter(): - id = elem.get('id') - if id: - ids[id] = elem - return tree, ids - - -class CommentProxy: - - def __call__(self, text=None): - element = Element(_Comment) - element.text = text - return element - - def __eq__(self, other): - return _Comment == other - - -class PIProxy: - - def __call__(self, target, text=None): - element = Element(_PI) - element.text = target - if text: - element.text = element.text + ' ' + text - return element - - def __eq__(self, other): - return _PI == other - - -Comment = CommentProxy() -PI = ProcessingInstruction = PIProxy() -del CommentProxy, PIProxy - -# Aliases -fromstring = XML -XMLTreeBuilder = XMLParser +from xml.etree.ElementTree import * |