]> granicus.if.org Git - python/commitdiff
Get rid of ugly code duplication for ElementTree.parse when the accelerator
authorEli Bendersky <eliben@gmail.com>
Mon, 20 May 2013 01:47:23 +0000 (18:47 -0700)
committerEli Bendersky <eliben@gmail.com>
Mon, 20 May 2013 01:47:23 +0000 (18:47 -0700)
is imported. Instead, ElementTree.parse can look for a special internal method
defined by the accelerator.

Lib/xml/etree/ElementTree.py
Modules/_elementtree.c

index df9a38066b566f2ef75d45afc2f3edc6ffd0beb3..edf25818f13e66a9cb5ec2f48578518b19a1cb93 100644 (file)
@@ -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
index 40cc09639a2eca1af0a326cfbe86df1c0cf82564..c53f5ee219ef6ae98b0274ea6822919b0c22ce29 100644 (file)
@@ -3347,10 +3347,9 @@ xmlparser_feed(XMLParserObject* self, PyObject* args)
 }
 
 static PyObject*
-xmlparser_parse(XMLParserObject* self, PyObject* args)
+xmlparser_parse_whole(XMLParserObject* self, PyObject* args)
 {
-    /* (internal) parse until end of input stream */
-
+    /* (internal) parse the whole input, until end of stream */
     PyObject* reader;
     PyObject* buffer;
     PyObject* temp;
@@ -3526,7 +3525,7 @@ xmlparser_setevents(XMLParserObject *self, PyObject* args)
 static PyMethodDef xmlparser_methods[] = {
     {"feed", (PyCFunction) xmlparser_feed, METH_VARARGS},
     {"close", (PyCFunction) xmlparser_close, METH_VARARGS},
-    {"_parse", (PyCFunction) xmlparser_parse, METH_VARARGS},
+    {"_parse_whole", (PyCFunction) xmlparser_parse_whole, METH_VARARGS},
     {"_setevents", (PyCFunction) xmlparser_setevents, METH_VARARGS},
     {"doctype", (PyCFunction) xmlparser_doctype, METH_VARARGS},
     {NULL, NULL}