summaryrefslogtreecommitdiffstats
path: root/Lib/xml/etree
diff options
context:
space:
mode:
authorEli Bendersky <eliben@gmail.com>2013-05-20 01:47:23 (GMT)
committerEli Bendersky <eliben@gmail.com>2013-05-20 01:47:23 (GMT)
commita369923cab024889e638db781b7495533d1a1e77 (patch)
tree712728a970cc18bbc00ec76e3e404ae23c2a258a /Lib/xml/etree
parente26fa1bdcbbffd5986630144a2e15795c5f83a19 (diff)
downloadcpython-a369923cab024889e638db781b7495533d1a1e77.zip
cpython-a369923cab024889e638db781b7495533d1a1e77.tar.gz
cpython-a369923cab024889e638db781b7495533d1a1e77.tar.bz2
Get rid of ugly code duplication for ElementTree.parse when the accelerator
is imported. Instead, ElementTree.parse can look for a special internal method defined by the accelerator.
Diffstat (limited to 'Lib/xml/etree')
-rw-r--r--Lib/xml/etree/ElementTree.py39
1 files changed, 11 insertions, 28 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index df9a380..edf2581 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -587,9 +587,17 @@ class ElementTree:
source = open(source, "rb")
close_source = True
try:
- if not parser:
- parser = XMLParser(target=TreeBuilder())
- while 1:
+ if parser is None:
+ # If no parser was specified, create a default XMLParser
+ parser = XMLParser()
+ if hasattr(parser, '_parse_whole'):
+ # The default XMLParser, when it comes from an accelerator,
+ # can define an internal _parse_whole API for efficiency.
+ # It can be used to parse the whole source without feeding
+ # it with chunks.
+ self._root = parser._parse_whole(source)
+ return self._root
+ while True:
data = source.read(65536)
if not data:
break
@@ -1651,30 +1659,5 @@ try:
# Element, SubElement, ParseError, TreeBuilder, XMLParser
from _elementtree import *
-
- # Overwrite 'ElementTree.parse' to use the C XMLParser
- class ElementTree(ElementTree):
- __doc__ = ElementTree.__doc__
- def parse(self, source, parser=None):
- __doc__ = ElementTree.parse.__doc__
- 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()
except ImportError:
pass