summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2017-09-14 23:13:21 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-09-14 23:13:21 (GMT)
commit49caab46f687eb201898fb6c2c40d47bdcb0e58b (patch)
tree4fc89fa36e1b325e5f2e2a9ae1e63cbe6a597e49
parent1658ec07577ef9696cea76fcf7fac2da18403ec5 (diff)
downloadcpython-49caab46f687eb201898fb6c2c40d47bdcb0e58b.zip
cpython-49caab46f687eb201898fb6c2c40d47bdcb0e58b.tar.gz
cpython-49caab46f687eb201898fb6c2c40d47bdcb0e58b.tar.bz2
[3.6] bpo-31455: Fix an assertion failure in ElementTree.XMLParser(). (GH-3545) (#3585)
* Avoid calling "PyObject_GetAttrString()" (and potentially executing user code) with a live exception set. * Ignore only AttributeError on attribute lookups in ElementTree.XMLParser() and propagate all other exceptions. (cherry picked from commit c8d8e15bfc24abeeaaf3d8be9073276b0c011cdf)
-rw-r--r--Lib/test/test_xml_etree.py25
-rw-r--r--Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31544.beTh6t.rst2
-rw-r--r--Modules/_elementtree.c35
3 files changed, 60 insertions, 2 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 15af1fc..ad0ffb3 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -2476,6 +2476,31 @@ class TreeBuilderTest(unittest.TestCase):
('html', '-//W3C//DTD XHTML 1.0 Transitional//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
+ def test_builder_lookup_errors(self):
+ class RaisingBuilder:
+ def __init__(self, raise_in=None, what=ValueError):
+ self.raise_in = raise_in
+ self.what = what
+
+ def __getattr__(self, name):
+ if name == self.raise_in:
+ raise self.what(self.raise_in)
+ def handle(*args):
+ pass
+ return handle
+
+ ET.XMLParser(target=RaisingBuilder())
+ # cET also checks for 'close' and 'doctype', PyET does it only at need
+ for event in ('start', 'data', 'end', 'comment', 'pi'):
+ with self.assertRaisesRegex(ValueError, event):
+ ET.XMLParser(target=RaisingBuilder(event))
+
+ ET.XMLParser(target=RaisingBuilder(what=AttributeError))
+ for event in ('start', 'data', 'end', 'comment', 'pi'):
+ parser = ET.XMLParser(target=RaisingBuilder(event, what=AttributeError))
+ parser.feed(self.sample1)
+ self.assertIsNone(parser.close())
+
class XMLParserTest(unittest.TestCase):
sample1 = b'<file><line>22</line></file>'
diff --git a/Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31544.beTh6t.rst b/Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31544.beTh6t.rst
new file mode 100644
index 0000000..9ea3599
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2017-09-13-19-55-35.bpo-31544.beTh6t.rst
@@ -0,0 +1,2 @@
+The C accelerator module of ElementTree ignored exceptions raised when
+looking up TreeBuilder target methods in XMLParser().
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index ccf5e6a..cf3e687 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -3224,6 +3224,18 @@ xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)self;
}
+static int
+ignore_attribute_error(PyObject *value)
+{
+ if (value == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ return -1;
+ }
+ PyErr_Clear();
+ }
+ return 0;
+}
+
/*[clinic input]
_elementtree.XMLParser.__init__
@@ -3270,14 +3282,33 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html,
self->target = target;
self->handle_start = PyObject_GetAttrString(target, "start");
+ if (ignore_attribute_error(self->handle_start)) {
+ return -1;
+ }
self->handle_data = PyObject_GetAttrString(target, "data");
+ if (ignore_attribute_error(self->handle_data)) {
+ return -1;
+ }
self->handle_end = PyObject_GetAttrString(target, "end");
+ if (ignore_attribute_error(self->handle_end)) {
+ return -1;
+ }
self->handle_comment = PyObject_GetAttrString(target, "comment");
+ if (ignore_attribute_error(self->handle_comment)) {
+ return -1;
+ }
self->handle_pi = PyObject_GetAttrString(target, "pi");
+ if (ignore_attribute_error(self->handle_pi)) {
+ return -1;
+ }
self->handle_close = PyObject_GetAttrString(target, "close");
+ if (ignore_attribute_error(self->handle_close)) {
+ return -1;
+ }
self->handle_doctype = PyObject_GetAttrString(target, "doctype");
-
- PyErr_Clear();
+ if (ignore_attribute_error(self->handle_doctype)) {
+ return -1;
+ }
/* configure parser */
EXPAT(SetUserData)(self->parser, self);