]> granicus.if.org Git - python/commitdiff
Merged revisions 77157 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Wed, 30 Dec 2009 19:44:54 +0000 (19:44 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 30 Dec 2009 19:44:54 +0000 (19:44 +0000)
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 #7604)

  Also, add a note to the docs about the better behavior of T_OBJECT_EX as
  compared to T_OBJECT.
........

Doc/c-api/structures.rst
Lib/test/test_descr.py
Misc/NEWS
Python/structmember.c

index 87bd362494d287444a6b0d6a60d33e97686da5b6..9f6a5e2517deab34f9b2251eb4b8ca6d9041bd61 100644 (file)
@@ -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
index c31728ecb7a303ab8bdf84d8bc679da76af4af6a..c436eb641804f81451a04babc4f367c33d6cb50c 100644 (file)
@@ -1040,6 +1040,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):
index 923cd30a8194e93514cbf19321df6a2c31fc21fa..c3f3295d3211d0089f69a2943307a9b5714ad4fe 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #7604: Deleting an unset slotted attribute did not raise an
+  AttributeError.
+
 - Issue #7534: Fix handling of IEEE specials (infinities, nans,
   negative zero) in ** operator.  The behaviour now conforms to that
   described in C99 Annex F.
index 9f08a6baf8e857253d2e0c163585495efd925faf..8edc35410633db110e7a405cda26e195f5e2096a 100644 (file)
@@ -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)) {