Skip to content

Commit d12362a

Browse files
committed
Merged revisions 77157 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r77157 | benjamin.peterson | 2009-12-30 13:34:10 -0600 (Wed, 30 Dec 2009) | 5 lines check if the attribute is set before deleting it with T_OBJECT_EX (fixes python#7604) Also, add a note to the docs about the better behavior of T_OBJECT_EX as compared to T_OBJECT. ........
1 parent 9abe1f1 commit d12362a

4 files changed

Lines changed: 27 additions & 6 deletions

File tree

Doc/c-api/structures.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,10 @@ definition with the same method name.
273273

274274
:cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX` differ in that
275275
:cmacro:`T_OBJECT` returns ``None`` if the member is *NULL* and
276-
:cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`.
276+
:cmacro:`T_OBJECT_EX` raises an :exc:`AttributeError`. Try to use
277+
:cmacro:`T_OBJECT_EX` over :cmacro:`T_OBJECT` because :cmacro:`T_OBJECT_EX`
278+
handles use of the :stmt:`del` statement on that attribute more correctly
279+
than :cmacro:`T_OBJECT`.
277280

278281
:attr:`flags` can be 0 for write and read access or :cmacro:`READONLY` for
279282
read-only access. Using :cmacro:`T_STRING` for :attr:`type` implies

Lib/test/test_descr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,11 @@ def __del__(self_):
10401040
del h
10411041
self.assertEqual(s.getvalue(), '')
10421042

1043+
class X(object):
1044+
__slots__ = "a"
1045+
with self.assertRaises(AttributeError):
1046+
del X().a
1047+
10431048
def test_slots_special(self):
10441049
# Testing __dict__ and __weakref__ in __slots__...
10451050
class D(object):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7604: Deleting an unset slotted attribute did not raise an
16+
AttributeError.
17+
1518
- Issue #7534: Fix handling of IEEE specials (infinities, nans,
1619
negative zero) in ** operator. The behaviour now conforms to that
1720
described in C99 Annex F.

Python/structmember.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,27 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
104104
{
105105
PyObject *oldv;
106106

107+
addr += l->offset;
108+
107109
if ((l->flags & READONLY) || l->type == T_STRING)
108110
{
109111
PyErr_SetString(PyExc_AttributeError, "readonly attribute");
110112
return -1;
111113
}
112-
if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) {
113-
PyErr_SetString(PyExc_TypeError,
114-
"can't delete numeric/char attribute");
115-
return -1;
114+
if (v == NULL) {
115+
if (l->type == T_OBJECT_EX) {
116+
/* Check if the attribute is set. */
117+
if (*(PyObject **)addr == NULL) {
118+
PyErr_SetString(PyExc_AttributeError, l->name);
119+
return -1;
120+
}
121+
}
122+
else if (l->type != T_OBJECT) {
123+
PyErr_SetString(PyExc_TypeError,
124+
"can't delete numeric/char attribute");
125+
return -1;
126+
}
116127
}
117-
addr += l->offset;
118128
switch (l->type) {
119129
case T_BOOL:{
120130
if (!PyBool_Check(v)) {

0 commit comments

Comments
 (0)