Skip to content

Commit

Permalink
bpo-34331: Fix incorrectly pluralized abstract class error message. (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
dangro authored and matrixise committed Sep 11, 2019
1 parent 19f6940 commit 4a12a17
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Lib/test/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ def foo(): return 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):
for abstractthing in [abc.abstractmethod, abc.abstractproperty,
abc.abstractclassmethod,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use singular/plural noun in error message when instantiating an abstract
class with non-overriden abstract method(s).
7 changes: 6 additions & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3753,6 +3753,7 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *joined;
PyObject *comma;
_Py_static_string(comma_id, ", ");
Py_ssize_t method_count;

/* Compute ", ".join(sorted(type.__abstractmethods__))
into joined. */
Expand All @@ -3773,14 +3774,18 @@ object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
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;
Expand Down

0 comments on commit 4a12a17

Please sign in to comment.