Commit 4a12a178 authored by Daniel Andrade's avatar Daniel Andrade Committed by Stéphane Wirtel

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

parent 19f6940c
...@@ -149,6 +149,25 @@ def test_factory(abc_ABCMeta, abc_get_cache_token): ...@@ -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,
......
Use singular/plural noun in error message when instantiating an abstract
class with non-overriden abstract method(s).
...@@ -3753,6 +3753,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -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) ...@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment