bpo-34331: Fix incorrectly pluralized abstract class error message. (GH-8670)

This commit is contained in:
Daniel Andrade 2019-09-11 08:29:44 -07:00 committed by Stéphane Wirtel
parent 19f6940cd7
commit 4a12a178f4
3 changed files with 27 additions and 1 deletions

View File

@ -149,6 +149,25 @@ def test_factory(abc_ABCMeta, abc_get_cache_token):
self.assertEqual(D.foo(), 4) self.assertEqual(D.foo(), 4)
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): def test_abstractmethod_integration(self):
for abstractthing in [abc.abstractmethod, abc.abstractproperty, for abstractthing in [abc.abstractmethod, abc.abstractproperty,
abc.abstractclassmethod, abc.abstractclassmethod,

View File

@ -0,0 +1,2 @@
Use singular/plural noun in error message when instantiating an abstract
class with non-overriden abstract method(s).

View File

@ -3753,6 +3753,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *joined; PyObject *joined;
PyObject *comma; PyObject *comma;
_Py_static_string(comma_id, ", "); _Py_static_string(comma_id, ", ");
Py_ssize_t method_count;
/* Compute ", ".join(sorted(type.__abstractmethods__)) /* Compute ", ".join(sorted(type.__abstractmethods__))
into joined. */ into joined. */
@ -3773,14 +3774,18 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
joined = PyUnicode_Join(comma, sorted_methods); joined = PyUnicode_Join(comma, sorted_methods);
method_count = PyObject_Length(sorted_methods);
Py_DECREF(sorted_methods); Py_DECREF(sorted_methods);
if (joined == NULL) if (joined == NULL)
return NULL; return NULL;
if (method_count == -1)
return NULL;
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"Can't instantiate abstract class %s " "Can't instantiate abstract class %s "
"with abstract methods %U", "with abstract method%s %U",
type->tp_name, type->tp_name,
method_count > 1 ? "s" : "",
joined); joined);
Py_DECREF(joined); Py_DECREF(joined);
return NULL; return NULL;