mirror of
https://github.com/python/cpython.git
synced 2025-07-30 06:34:15 +00:00
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.
This commit is contained in:
parent
b7953f00b1
commit
0f02d3933b
4 changed files with 27 additions and 6 deletions
|
@ -281,7 +281,10 @@ definition with the same method name.
|
||||||
|
|
||||||
:cmacro:`T_OBJECT` and :cmacro:`T_OBJECT_EX` differ in that
|
: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` 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
|
: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
|
read-only access. Using :cmacro:`T_STRING` for :attr:`type` implies
|
||||||
|
|
|
@ -1166,6 +1166,11 @@ order (MRO) for bases """
|
||||||
del h
|
del h
|
||||||
self.assertEqual(s.getvalue(), '')
|
self.assertEqual(s.getvalue(), '')
|
||||||
|
|
||||||
|
class X(object):
|
||||||
|
__slots__ = "a"
|
||||||
|
with self.assertRaises(AttributeError):
|
||||||
|
del X().a
|
||||||
|
|
||||||
def test_slots_special(self):
|
def test_slots_special(self):
|
||||||
# Testing __dict__ and __weakref__ in __slots__...
|
# Testing __dict__ and __weakref__ in __slots__...
|
||||||
class D(object):
|
class D(object):
|
||||||
|
|
|
@ -12,6 +12,9 @@ What's New in Python 2.7 alpha 2?
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #7604: Deleting an unset slotted attribute did not raise an
|
||||||
|
AttributeError.
|
||||||
|
|
||||||
- Issue #7534: Fix handling of IEEE specials (infinities, nans,
|
- Issue #7534: Fix handling of IEEE specials (infinities, nans,
|
||||||
negative zero) in ** operator. The behaviour now conforms to that
|
negative zero) in ** operator. The behaviour now conforms to that
|
||||||
described in C99 Annex F.
|
described in C99 Annex F.
|
||||||
|
|
|
@ -170,6 +170,8 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
|
||||||
{
|
{
|
||||||
PyObject *oldv;
|
PyObject *oldv;
|
||||||
|
|
||||||
|
addr += l->offset;
|
||||||
|
|
||||||
if ((l->flags & READONLY) || l->type == T_STRING)
|
if ((l->flags & READONLY) || l->type == T_STRING)
|
||||||
{
|
{
|
||||||
PyErr_SetString(PyExc_TypeError, "readonly attribute");
|
PyErr_SetString(PyExc_TypeError, "readonly attribute");
|
||||||
|
@ -179,12 +181,20 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
|
||||||
PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
|
PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) {
|
if (v == NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
if (l->type == T_OBJECT_EX) {
|
||||||
"can't delete numeric/char attribute");
|
/* Check if the attribute is set. */
|
||||||
return -1;
|
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) {
|
switch (l->type) {
|
||||||
case T_BOOL:{
|
case T_BOOL:{
|
||||||
if (!PyBool_Check(v)) {
|
if (!PyBool_Check(v)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue