diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-23 06:42:25 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-23 06:42:25 (GMT) |
commit | b6aa5375d5a2f81370338357506034befe62aa31 (patch) | |
tree | 39832d710263d64a001307f8f23a7d9515eeacbf | |
parent | 8bc792a602311e426cbd69293627fdc9287a5c7b (diff) | |
download | cpython-b6aa5375d5a2f81370338357506034befe62aa31.zip cpython-b6aa5375d5a2f81370338357506034befe62aa31.tar.gz cpython-b6aa5375d5a2f81370338357506034befe62aa31.tar.bz2 |
Issue #25691: Fixed crash on deleting ElementTree.Element attributes.
-rw-r--r-- | Lib/test/test_xml_etree.py | 27 | ||||
-rw-r--r-- | Lib/test/test_xml_etree_c.py | 32 | ||||
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Modules/_elementtree.c | 6 |
4 files changed, 67 insertions, 0 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 9261592..8511cfe 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -244,6 +244,33 @@ class ElementTreeTest(unittest.TestCase): self.assertEqual(ET.XML, ET.fromstring) self.assertEqual(ET.PI, ET.ProcessingInstruction) + def test_set_attribute(self): + element = ET.Element('tag') + + self.assertEqual(element.tag, 'tag') + element.tag = 'Tag' + self.assertEqual(element.tag, 'Tag') + element.tag = 'TAG' + self.assertEqual(element.tag, 'TAG') + + self.assertIsNone(element.text) + element.text = 'Text' + self.assertEqual(element.text, 'Text') + element.text = 'TEXT' + self.assertEqual(element.text, 'TEXT') + + self.assertIsNone(element.tail) + element.tail = 'Tail' + self.assertEqual(element.tail, 'Tail') + element.tail = 'TAIL' + self.assertEqual(element.tail, 'TAIL') + + self.assertEqual(element.attrib, {}) + element.attrib = {'a': 'b', 'c': 'd'} + self.assertEqual(element.attrib, {'a': 'b', 'c': 'd'}) + element.attrib = {'A': 'B', 'C': 'D'} + self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'}) + def test_simpleops(self): # Basic method sanity checks. diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py index 816aa86..409ec6a 100644 --- a/Lib/test/test_xml_etree_c.py +++ b/Lib/test/test_xml_etree_c.py @@ -22,6 +22,38 @@ class MiscTests(unittest.TestCase): finally: data = None + def test_del_attribute(self): + element = cET.Element('tag') + + element.tag = 'TAG' + with self.assertRaises(AttributeError): + del element.tag + self.assertEqual(element.tag, 'TAG') + + with self.assertRaises(AttributeError): + del element.text + self.assertIsNone(element.text) + element.text = 'TEXT' + with self.assertRaises(AttributeError): + del element.text + self.assertEqual(element.text, 'TEXT') + + with self.assertRaises(AttributeError): + del element.tail + self.assertIsNone(element.tail) + element.tail = 'TAIL' + with self.assertRaises(AttributeError): + del element.tail + self.assertEqual(element.tail, 'TAIL') + + with self.assertRaises(AttributeError): + del element.attrib + self.assertEqual(element.attrib, {}) + element.attrib = {'A': 'B', 'C': 'D'} + with self.assertRaises(AttributeError): + del element.attrib + self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'}) + @unittest.skipUnless(cET, 'requires _elementtree') class TestAliasWorking(unittest.TestCase): @@ -106,6 +106,8 @@ Core and Builtins Library ------- +- Issue #25691: Fixed crash on deleting ElementTree.Element attributes. + - Issue #25624: ZipFile now always writes a ZIP_STORED header for directory entries. Patch by Dingyuan Wang. diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index da784a0..3b6ee89 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1852,6 +1852,12 @@ static int element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) { char *name = ""; + + if (value == NULL) { + PyErr_SetString(PyExc_AttributeError, + "can't delete attribute"); + return -1; + } if (PyUnicode_Check(nameobj)) name = _PyUnicode_AsString(nameobj); if (name == NULL) |