diff options
-rw-r--r-- | Doc/c-api/structures.rst | 5 | ||||
-rw-r--r-- | Lib/test/test_descr.py | 5 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Python/structmember.c | 20 |
4 files changed, 27 insertions, 6 deletions
diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst index 87bd362..9f6a5e2 100644 --- a/Doc/c-api/structures.rst +++ b/Doc/c-api/structures.rst @@ -273,7 +273,10 @@ definition with the same method name. :cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX` differ in that :cmacro:`T_OBJECT` returns ``None`` if the member is *NULL* and - :cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`. + :cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use + :cmacro:`T_OBJECT_EX` over :cmacro:`T_OBJECT` because :cmacro:`T_OBJECT_EX` + handles use of the :stmt:`del` statement on that attribute more correctly + than :cmacro:`T_OBJECT`. :attr:`flags` can be 0 for write and read access or :cmacro:`READONLY` for read-only access. Using :cmacro:`T_STRING` for :attr:`type` implies diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 17d96b2..a02fe39 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1044,6 +1044,11 @@ order (MRO) for bases """ del h self.assertEqual(s.getvalue(), '') + class X(object): + __slots__ = "a" + with self.assertRaises(AttributeError): + del X().a + def test_slots_special(self): # Testing __dict__ and __weakref__ in __slots__... class D(object): @@ -12,6 +12,9 @@ What's New in Python 3.1.2? Core and Builtins ----------------- +- Issue #7604: Deleting an unset slotted attribute did not raise an + AttributeError. + - Issue #7466: segmentation fault when the garbage collector is called in the middle of populating a tuple. Patch by Florent Xicluna. diff --git a/Python/structmember.c b/Python/structmember.c index 9f08a6b..8edc354 100644 --- a/Python/structmember.c +++ b/Python/structmember.c @@ -104,17 +104,27 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) { PyObject *oldv; + addr += l->offset; + if ((l->flags & READONLY) || l->type == T_STRING) { PyErr_SetString(PyExc_AttributeError, "readonly attribute"); return -1; } - if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) { - PyErr_SetString(PyExc_TypeError, - "can't delete numeric/char attribute"); - return -1; + if (v == NULL) { + if (l->type == T_OBJECT_EX) { + /* Check if the attribute is set. */ + if (*(PyObject **)addr == NULL) { + PyErr_SetString(PyExc_AttributeError, l->name); + return -1; + } + } + else if (l->type != T_OBJECT) { + PyErr_SetString(PyExc_TypeError, + "can't delete numeric/char attribute"); + return -1; + } } - addr += l->offset; switch (l->type) { case T_BOOL:{ if (!PyBool_Check(v)) { |