Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a369923

Browse files
committed
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.
1 parent e26fa1b commit a369923

2 files changed

Lines changed: 14 additions & 32 deletions

File tree

Lib/xml/etree/ElementTree.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,17 @@ def parse(self, source, parser=None):
587587
source = open(source, "rb")
588588
close_source = True
589589
try:
590-
if not parser:
591-
parser = XMLParser(target=TreeBuilder())
592-
while 1:
590+
if parser is None:
591+
# If no parser was specified, create a default XMLParser
592+
parser = XMLParser()
593+
if hasattr(parser, '_parse_whole'):
594+
# The default XMLParser, when it comes from an accelerator,
595+
# can define an internal _parse_whole API for efficiency.
596+
# It can be used to parse the whole source without feeding
597+
# it with chunks.
598+
self._root = parser._parse_whole(source)
599+
return self._root
600+
while True:
593601
data = source.read(65536)
594602
if not data:
595603
break
@@ -1651,30 +1659,5 @@ def close(self):
16511659

16521660
# Element, SubElement, ParseError, TreeBuilder, XMLParser
16531661
from _elementtree import *
1654-
1655-
# Overwrite 'ElementTree.parse' to use the C XMLParser
1656-
class ElementTree(ElementTree):
1657-
__doc__ = ElementTree.__doc__
1658-
def parse(self, source, parser=None):
1659-
__doc__ = ElementTree.parse.__doc__
1660-
close_source = False
1661-
if not hasattr(source, 'read'):
1662-
source = open(source, 'rb')
1663-
close_source = True
1664-
try:
1665-
if parser is not None:
1666-
while True:
1667-
data = source.read(65536)
1668-
if not data:
1669-
break
1670-
parser.feed(data)
1671-
self._root = parser.close()
1672-
else:
1673-
parser = XMLParser()
1674-
self._root = parser._parse(source)
1675-
return self._root
1676-
finally:
1677-
if close_source:
1678-
source.close()
16791662
except ImportError:
16801663
pass

Modules/_elementtree.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3347,10 +3347,9 @@ xmlparser_feed(XMLParserObject* self, PyObject* args)
33473347
}
33483348

33493349
static PyObject*
3350-
xmlparser_parse(XMLParserObject* self, PyObject* args)
3350+
xmlparser_parse_whole(XMLParserObject* self, PyObject* args)
33513351
{
3352-
/* (internal) parse until end of input stream */
3353-
3352+
/* (internal) parse the whole input, until end of stream */
33543353
PyObject* reader;
33553354
PyObject* buffer;
33563355
PyObject* temp;
@@ -3526,7 +3525,7 @@ xmlparser_setevents(XMLParserObject *self, PyObject* args)
35263525
static PyMethodDef xmlparser_methods[] = {
35273526
{"feed", (PyCFunction) xmlparser_feed, METH_VARARGS},
35283527
{"close", (PyCFunction) xmlparser_close, METH_VARARGS},
3529-
{"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS},
3528+
{"_parse_whole", (PyCFunction) xmlparser_parse_whole, METH_VARARGS},
35303529
{"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS},
35313530
{"doctype", (PyCFunction) xmlparser_doctype, METH_VARARGS},
35323531
{NULL, NULL}

0 commit comments

Comments
 (0)