]> granicus.if.org Git - python/commitdiff
Issue #6676: Ensure a meaningful exception is raised when attempting
authorNed Deily <nad@acm.org>
Thu, 27 Mar 2014 23:38:32 +0000 (16:38 -0700)
committerNed Deily <nad@acm.org>
Thu, 27 Mar 2014 23:38:32 +0000 (16:38 -0700)
to parse more than one XML document per pyexpat xmlparser instance.
(Original patches by Hirokazu Yamamoto and Amaury Forgeot d'Arc, with
suggested wording by David Gutteridge)

Doc/library/pyexpat.rst
Lib/test/test_pyexpat.py
Misc/NEWS
Modules/pyexpat.c

index 5ce4c0252fdf8692e06906bacfe0a030f50340dd..20ca3bc2749781278073a13ab74fe3125c6eb113 100644 (file)
@@ -103,6 +103,10 @@ The :mod:`xml.parsers.expat` module contains two functions:
       http://www.python.org/ns/ elem1
       elem2
 
+   Due to limitations in the ``Expat`` library used by :mod:`pyexpat`,
+   the :class:`xmlparser` instance returned can only be used to parse a single
+   XML document.  Call ``ParserCreate`` for each document to provide unique
+   parser instances.
 
 .. seealso::
 
@@ -122,7 +126,9 @@ XMLParser Objects
 
    Parses the contents of the string *data*, calling the appropriate handler
    functions to process the parsed data.  *isfinal* must be true on the final call
-   to this method.  *data* can be the empty string at any time.
+   to this method; it allows the parsing of a single file in fragments,
+   not the submission of multiple files.
+   *data* can be the empty string at any time.
 
 
 .. method:: xmlparser.ParseFile(file)
index b911a2058cc3db31a4bdfb4de82111208b8d0119..9f63d4e9797991ec37a1868c2ffe2e98db39369c 100644 (file)
@@ -228,6 +228,17 @@ class ParseTest(unittest.TestCase):
         finally:
             test_support.unlink(test_support.TESTFN)
 
+    def test_parse_again(self):
+        parser = expat.ParserCreate()
+        file = StringIO.StringIO(data)
+        parser.ParseFile(file)
+        # Issue 6676: ensure a meaningful exception is raised when attempting
+        # to parse more than one XML document per xmlparser instance,
+        # a limitation of the Expat library.
+        with self.assertRaises(expat.error) as cm:
+            parser.ParseFile(file)
+        self.assertEqual(expat.ErrorString(cm.exception.code),
+                          expat.errors.XML_ERROR_FINISHED)
 
 class NamespaceSeparatorTest(unittest.TestCase):
     def test_legal(self):
index b3b99d3be7d2179d958e542313c91684831309ef..83c73836a6a6a085ff01ce30285f644b87cf1cc4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -246,6 +246,11 @@ Library
 - Issue #19286: Directories in ``package_data`` are no longer added to
   the filelist, preventing failure outlined in the ticket.
 
+- Issue #6676: Ensure a meaningful exception is raised when attempting
+  to parse more than one XML document per pyexpat xmlparser instance.
+  (Original patches by Hirokazu Yamamoto and Amaury Forgeot d'Arc, with
+  suggested wording by David Gutteridge)
+
 Tools/Demos
 -----------
 
index 8de3fb17d3315ad68d241f02508a31e88fb5b1b6..a95c3885fa2cb32a5e8b083d0aeeb7f0a0b3f35c 100644 (file)
@@ -976,7 +976,7 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
         void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
         if (buf == NULL) {
             Py_XDECREF(readmethod);
-            return PyErr_NoMemory();
+            return get_parse_result(self, 0);
         }
 
         bytes_read = readinst(buf, BUF_SIZE, readmethod);