self.assertEqual(D.foo(), 4)
self.assertEqual(D().foo(), 4)
+ def test_object_new_with_one_abstractmethod(self):
+ class C(metaclass=abc_ABCMeta):
+ @abc.abstractmethod
+ def method_one(self):
+ pass
+ msg = r"class C with abstract method method_one"
+ self.assertRaisesRegex(TypeError, msg, C)
+
+ def test_object_new_with_many_abstractmethods(self):
+ class C(metaclass=abc_ABCMeta):
+ @abc.abstractmethod
+ def method_one(self):
+ pass
+ @abc.abstractmethod
+ def method_two(self):
+ pass
+ msg = r"class C with abstract methods method_one, method_two"
+ self.assertRaisesRegex(TypeError, msg, C)
+
def test_abstractmethod_integration(self):
for abstractthing in [abc.abstractmethod, abc.abstractproperty,
abc.abstractclassmethod,
PyObject *joined;
PyObject *comma;
_Py_static_string(comma_id, ", ");
+ Py_ssize_t method_count;
/* Compute ", ".join(sorted(type.__abstractmethods__))
into joined. */
return NULL;
}
joined = PyUnicode_Join(comma, sorted_methods);
+ method_count = PyObject_Length(sorted_methods);
Py_DECREF(sorted_methods);
if (joined == NULL)
return NULL;
+ if (method_count == -1)
+ return NULL;
PyErr_Format(PyExc_TypeError,
"Can't instantiate abstract class %s "
- "with abstract methods %U",
+ "with abstract method%s %U",
type->tp_name,
+ method_count > 1 ? "s" : "",
joined);
Py_DECREF(joined);
return NULL;