changeset: 90002:74faca1ac59c branch: 2.7 parent: 89983:426a7046cdb0 user: Ned Deily date: Thu Mar 27 16:38:32 2014 -0700 files: Doc/library/pyexpat.rst Lib/test/test_pyexpat.py Misc/NEWS Modules/pyexpat.c description: 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) diff -r 426a7046cdb0 -r 74faca1ac59c Doc/library/pyexpat.rst --- a/Doc/library/pyexpat.rst Wed Mar 26 23:25:02 2014 -0700 +++ b/Doc/library/pyexpat.rst Thu Mar 27 16:38:32 2014 -0700 @@ -103,6 +103,10 @@ 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 @@ 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) diff -r 426a7046cdb0 -r 74faca1ac59c Lib/test/test_pyexpat.py --- a/Lib/test/test_pyexpat.py Wed Mar 26 23:25:02 2014 -0700 +++ b/Lib/test/test_pyexpat.py Thu Mar 27 16:38:32 2014 -0700 @@ -228,6 +228,17 @@ 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): diff -r 426a7046cdb0 -r 74faca1ac59c Misc/NEWS --- a/Misc/NEWS Wed Mar 26 23:25:02 2014 -0700 +++ b/Misc/NEWS Thu Mar 27 16:38:32 2014 -0700 @@ -246,6 +246,11 @@ - 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 ----------- diff -r 426a7046cdb0 -r 74faca1ac59c Modules/pyexpat.c --- a/Modules/pyexpat.c Wed Mar 26 23:25:02 2014 -0700 +++ b/Modules/pyexpat.c Thu Mar 27 16:38:32 2014 -0700 @@ -976,7 +976,7 @@ 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);