changeset: 77062:41a9d24d075e user: Eli Bendersky date: Sun May 20 06:33:29 2012 +0300 files: Lib/test/test_xml_etree.py Modules/_elementtree.c description: Issue #14849: setup Element data members to be assignable in subclasses diff -r 17efda0a2b2b -r 41a9d24d075e Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Sat May 19 21:25:41 2012 +0000 +++ b/Lib/test/test_xml_etree.py Sun May 20 06:33:29 2012 +0300 @@ -1914,6 +1914,10 @@ self.assertIsInstance(mye, MyElement) 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): class MyElement(ET.Element): def __init__(self, tag, attrib={}, **extra): diff -r 17efda0a2b2b -r 41a9d24d075e Modules/_elementtree.c --- a/Modules/_elementtree.c Sat May 19 21:25:41 2012 +0000 +++ b/Modules/_elementtree.c Sun May 20 06:33:29 2012 +0300 @@ -1640,16 +1640,15 @@ return res; } -static int -element_setattr(ElementObject* self, const char* name, PyObject* value) +static PyObject* +element_setattro(ElementObject* self, PyObject* nameobj, PyObject* value) { - if (value == NULL) { - PyErr_SetString( - PyExc_AttributeError, - "can't delete element attributes" - ); - return -1; - } + char *name = ""; + if (PyUnicode_Check(nameobj)) + name = _PyUnicode_AsString(nameobj); + + if (name == NULL) + return NULL; if (strcmp(name, "tag") == 0) { Py_DECREF(self->tag); @@ -1671,10 +1670,10 @@ Py_INCREF(self->extra->attrib); } else { PyErr_SetString(PyExc_AttributeError, name); - return -1; + return NULL; } - return 0; + return NULL; } static PySequenceMethods element_as_sequence = { @@ -1700,7 +1699,7 @@ (destructor)element_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ - (setattrfunc)element_setattr, /* tp_setattr */ + 0, /* tp_setattr */ 0, /* tp_reserved */ (reprfunc)element_repr, /* tp_repr */ 0, /* tp_as_number */ @@ -1710,7 +1709,7 @@ 0, /* tp_call */ 0, /* tp_str */ (getattrofunc)element_getattro, /* tp_getattro */ - 0, /* tp_setattro */ + (setattrofunc)element_setattro, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */

AltStyle によって変換されたページ (->オリジナル) /