summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_abc.py20
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/typeobject.c7
3 files changed, 28 insertions, 2 deletions
diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index f43123f..0fa40cb 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -60,6 +60,26 @@ class TestABC(unittest.TestCase):
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
diff --git a/Misc/NEWS b/Misc/NEWS
index d9d4a0e..5841761 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 3.1.3?
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.
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index ab942c0..d2eb8e2 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -320,8 +320,11 @@ type_set_module(PyTypeObject *type, PyObject *value, void *context)
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;