]> granicus.if.org Git - python/commitdiff
don't segfault on deleting __abstractmethods__ #10892
authorBenjamin Peterson <benjamin@python.org>
Wed, 12 Jan 2011 15:34:01 +0000 (15:34 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 12 Jan 2011 15:34:01 +0000 (15:34 +0000)
Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index a81c2183761c040d8a3301867b5c67851a223962..914b249f7a480462428c305d7a329ddd480df84a 100644 (file)
@@ -4224,12 +4224,16 @@ order (MRO) for bases """
 
         self.assertRaises(AttributeError, getattr, EvilGetattribute(), "attr")
 
-    def test_type_has_no_abstractmethods(self):
+    def test_abstractmethods(self):
         # type pretends not to have __abstractmethods__.
         self.assertRaises(AttributeError, getattr, type, "__abstractmethods__")
         class meta(type):
             pass
         self.assertRaises(AttributeError, getattr, meta, "__abstractmethods__")
+        class X(object):
+            pass
+        with self.assertRaises(AttributeError):
+            del X.__abstractmethods__
 
 
 class DictProxyTests(unittest.TestCase):
index 262f982853624df0fe3ddfb7da6f0cab5a145295..ee69f507db32fdcebaac4fca71a7022e5df8182e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -8,6 +8,9 @@ What's New in Python 3.2 Release Candidate 1
 Core and Builtins
 -----------------
 
+- Issue #10892: Don't segfault when trying to delete __abstractmethods__ from a
+  class.
+
 - Issue #8020: Avoid a crash where the small objects allocator would read
   non-Python managed memory while it is being modified by another thread.
   Patch by Matt Bandy.
index 1fefe8414fe258bc2798fb67df04407b35328ca4..930297a7bb6db48178c8eb5348f72572d28dec93 100644 (file)
@@ -340,8 +340,17 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
        abc.ABCMeta.__new__, so this function doesn't do anything
        special to update subclasses.
     */
-    int res = PyDict_SetItemString(type->tp_dict,
-                                   "__abstractmethods__", value);
+    int res;
+    if (value != NULL) {
+        res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
+    }
+    else {
+        res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
+        if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
+            PyErr_Format(PyExc_AttributeError, "__abstractmethods__", value);
+            return -1;
+        }
+    }
     if (res == 0) {
         PyType_Modified(type);
         if (value && PyObject_IsTrue(value)) {