Issue #14849: setup Element data members to be assignable in subclasses

This commit is contained in:
Eli Bendersky 2012-05-20 06:33:29 +03:00
parent 77a1cf1622
commit b20df95827
2 changed files with 16 additions and 13 deletions

View file

@ -1914,6 +1914,10 @@ class ElementTreeTest(unittest.TestCase):
self.assertIsInstance(mye, MyElement) self.assertIsInstance(mye, MyElement)
self.assertEqual(mye.tag, 'foo') self.assertEqual(mye.tag, 'foo')
# test that attribute assignment works (issue 14849)
mye.text = "joe"
self.assertEqual(mye.text, "joe")
def test_Element_subclass_constructor(self): def test_Element_subclass_constructor(self):
class MyElement(ET.Element): class MyElement(ET.Element):
def __init__(self, tag, attrib={}, **extra): def __init__(self, tag, attrib={}, **extra):

View file

@ -1640,16 +1640,15 @@ element_getattro(ElementObject* self, PyObject* nameobj)
return res; return res;
} }
static int static PyObject*
element_setattr(ElementObject* self, const char* name, PyObject* value) element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value)
{ {
if (value == NULL) { char *name = "";
PyErr_SetString( if (PyUnicode_Check(nameobj))
PyExc_AttributeError, name = _PyUnicode_AsString(nameobj);
"can't delete element attributes"
); if (name == NULL)
return -1; return NULL;
}
if (strcmp(name, "tag") == 0) { if (strcmp(name, "tag") == 0) {
Py_DECREF(self->tag); Py_DECREF(self->tag);
@ -1671,10 +1670,10 @@ element_setattr(ElementObject* self, const char* name, PyObject* value)
Py_INCREF(self->extra->attrib); Py_INCREF(self->extra->attrib);
} else { } else {
PyErr_SetString(PyExc_AttributeError, name); PyErr_SetString(PyExc_AttributeError, name);
return -1; return NULL;
} }
return 0; return NULL;
} }
static PySequenceMethods element_as_sequence = { static PySequenceMethods element_as_sequence = {
@ -1700,7 +1699,7 @@ static PyTypeObject Element_Type = {
(destructor)element_dealloc, /* tp_dealloc */ (destructor)element_dealloc, /* tp_dealloc */
0, /* tp_print */ 0, /* tp_print */
0, /* tp_getattr */ 0, /* tp_getattr */
(setattrfunc)element_setattr, /* tp_setattr */ 0, /* tp_setattr */
0, /* tp_reserved */ 0, /* tp_reserved */
(reprfunc)element_repr, /* tp_repr */ (reprfunc)element_repr, /* tp_repr */
0, /* tp_as_number */ 0, /* tp_as_number */
@ -1710,7 +1709,7 @@ static PyTypeObject Element_Type = {
0, /* tp_call */ 0, /* tp_call */
0, /* tp_str */ 0, /* tp_str */
(getattrofunc)element_getattro, /* tp_getattro */ (getattrofunc)element_getattro, /* tp_getattro */
0, /* tp_setattro */ (setattrofunc)element_setattro, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
/* tp_flags */ /* tp_flags */