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

Skip to content

Commit e7d532f

Browse files
committed
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)
1 parent 2542b66 commit e7d532f

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

Doc/library/pyexpat.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ The :mod:`xml.parsers.expat` module contains two functions:
100100
http://www.python.org/ns/ elem1
101101
elem2
102102

103+
Due to limitations in the ``Expat`` library used by :mod:`pyexpat`,
104+
the :class:`xmlparser` instance returned can only be used to parse a single
105+
XML document. Call ``ParserCreate`` for each document to provide unique
106+
parser instances.
107+
103108

104109
.. seealso::
105110

@@ -119,7 +124,9 @@ XMLParser Objects
119124

120125
Parses the contents of the string *data*, calling the appropriate handler
121126
functions to process the parsed data. *isfinal* must be true on the final call
122-
to this method. *data* can be the empty string at any time.
127+
to this method; it allows the parsing of a single file in fragments,
128+
not the submission of multiple files.
129+
*data* can be the empty string at any time.
123130

124131

125132
.. method:: xmlparser.ParseFile(file)

Lib/test/test_pyexpat.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ def test_parse_file(self):
236236
operations = out.out
237237
self._verify_parse_output(operations)
238238

239+
def test_parse_again(self):
240+
parser = expat.ParserCreate()
241+
file = BytesIO(data)
242+
parser.ParseFile(file)
243+
# Issue 6676: ensure a meaningful exception is raised when attempting
244+
# to parse more than one XML document per xmlparser instance,
245+
# a limitation of the Expat library.
246+
with self.assertRaises(expat.error) as cm:
247+
parser.ParseFile(file)
248+
self.assertEqual(expat.ErrorString(cm.exception.code),
249+
expat.errors.XML_ERROR_FINISHED)
250+
239251
class NamespaceSeparatorTest(unittest.TestCase):
240252
def test_legal(self):
241253
# Tests that make sure we get errors when the namespace_separator value

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Library
8989
- Issue #20817: Fix inspect.getcallargs() to fail correctly if more
9090
than 3 arguments are missing. Patch by Jeremiah Lowin.
9191

92+
- Issue #6676: Ensure a meaningful exception is raised when attempting
93+
to parse more than one XML document per pyexpat xmlparser instance.
94+
(Original patches by Hirokazu Yamamoto and Amaury Forgeot d'Arc, with
95+
suggested wording by David Gutteridge)
96+
9297
Documentation
9398
-------------
9499

Modules/pyexpat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
908908
void *buf = XML_GetBuffer(self->itself, BUF_SIZE);
909909
if (buf == NULL) {
910910
Py_XDECREF(readmethod);
911-
return PyErr_NoMemory();
911+
return get_parse_result(self, 0);
912912
}
913913

914914
bytes_read = readinst(buf, BUF_SIZE, readmethod);

0 commit comments

Comments
 (0)