]> granicus.if.org Git - python/commitdiff
Issue #25691: Fixed crash on deleting ElementTree.Element attributes.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 23 Nov 2015 06:42:25 +0000 (08:42 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 23 Nov 2015 06:42:25 +0000 (08:42 +0200)
Lib/test/test_xml_etree.py
Lib/test/test_xml_etree_c.py
Misc/NEWS
Modules/_elementtree.c

index 9261592189fed1194472f9da5a73466cc80e6f91..8511cfe1572254ebeec288f0df419a8e11775a0b 100644 (file)
@@ -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.
 
index 816aa8645d36d172665660d9ba698889a1432689..409ec6ab1bde9f26286ad39a33614fe9892b5417 100644 (file)
@@ -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):
index a13e1987a185530e870e55ddac52b91b62d487ae..635da46f022a0d1906400877b9c83b945356510d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.
 
index da784a05830530e00ee01bca2534ddde1fc2f544..3b6ee89adac48dd2e1b653adc2e622683bf9913f 100644 (file)
@@ -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)