[Python-checkins] r85183 - in python/branches/release31-maint: Lib/test/test_abc.py Misc/NEWS Objects/typeobject.c

benjamin.peterson python-checkins at python.org
Sat Oct 2 20:04:55 CEST 2010


Author: benjamin.peterson
Date: Sat Oct  2 20:04:55 2010
New Revision: 85183

Log:
Merged revisions 85154,85182 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r85154 | benjamin.peterson | 2010-10-01 19:03:31 -0500 (Fri, 01 Oct 2010) | 1 line
  
  type.__abstractmethods__ should raise an AttributeError #10006
........
  r85182 | benjamin.peterson | 2010-10-02 12:55:47 -0500 (Sat, 02 Oct 2010) | 1 line
  
  add a test and a note about metaclasses now being abcs
........


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/test/test_abc.py
   python/branches/release31-maint/Misc/NEWS
   python/branches/release31-maint/Objects/typeobject.c

Modified: python/branches/release31-maint/Lib/test/test_abc.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_abc.py	(original)
+++ python/branches/release31-maint/Lib/test/test_abc.py	Sat Oct  2 20:04:55 2010
@@ -60,6 +60,26 @@
             self.assertRaises(TypeError, F)  # because bar is abstract now
             self.assertTrue(isabstract(F))
 
+    def test_type_has_no_abstractmethods(self):
+        # type pretends not to have __abstractmethods__.
+        self.assertRaises(AttributeError, getattr, type, "__abstractmethods__")
+        class meta(type):
+            pass
+        self.assertRaises(AttributeError, getattr, meta, "__abstractmethods__")
+
+    def test_metaclass_abc(self):
+        # Metaclasses can be ABCs, too.
+        class A(metaclass=abc.ABCMeta):
+            @abc.abstractmethod
+            def x(self):
+                pass
+        self.assertEqual(A.__abstractmethods__, {"x"})
+        class meta(type, A):
+            def x(self):
+                return 1
+        class C(metaclass=meta):
+            pass
+
     def test_registration_basics(self):
         class A(metaclass=abc.ABCMeta):
             pass

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Sat Oct  2 20:04:55 2010
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #10006: type.__abstractmethods__ now raises an AttributeError.  As a
+  result metaclasses can now be ABCs (see #9533).
+
 - Issue #9930: Remove bogus subtype check that was causing (e.g.)
   float.__rdiv__(2.0, 3) to return NotImplemented instead of the
   expected 1.5.

Modified: python/branches/release31-maint/Objects/typeobject.c
==============================================================================
--- python/branches/release31-maint/Objects/typeobject.c	(original)
+++ python/branches/release31-maint/Objects/typeobject.c	Sat Oct  2 20:04:55 2010
@@ -320,8 +320,11 @@
 static PyObject *
 type_abstractmethods(PyTypeObject *type, void *context)
 {
-    PyObject *mod = PyDict_GetItemString(type->tp_dict,
-                                         "__abstractmethods__");
+    PyObject *mod = NULL;
+    /* type its self has an __abstractmethods__ descriptor (this). Don't
+       return that. */
+    if (type != &PyType_Type)
+        mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");
     if (!mod) {
         PyErr_Format(PyExc_AttributeError, "__abstractmethods__");
         return NULL;


More information about the Python-checkins mailing list