Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 533849a

Browse files
committed
Merged revisions 77161 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ................ r77161 | benjamin.peterson | 2009-12-30 13:44:54 -0600 (Wed, 30 Dec 2009) | 12 lines Merged revisions 77157 via svnmerge from svn+ssh://[email protected]/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 #7604) Also, add a note to the docs about the better behavior of T_OBJECT_EX as compared to T_OBJECT. ........ ................
1 parent 078d2c9 commit 533849a

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
@@ -1044,6 +1044,11 @@ def __del__(self_):
10441044
del h
10451045
self.assertEqual(s.getvalue(), '')
10461046

1047+
class X(object):
1048+
__slots__ = "a"
1049+
with self.assertRaises(AttributeError):
1050+
del X().a
1051+
10471052
def test_slots_special(self):
10481053
# Testing __dict__ and __weakref__ in __slots__...
10491054
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.1.2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7604: Deleting an unset slotted attribute did not raise an
16+
AttributeError.
17+
1518
- Issue #7466: segmentation fault when the garbage collector is called
1619
in the middle of populating a tuple. Patch by Florent Xicluna.
1720

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)