bpo-34331: Fix incorrectly pluralized abstract class error message. (GH-8670)
authorDaniel Andrade <dangro@users.noreply.github.com>
Wed, 11 Sep 2019 15:29:44 +0000 (08:29 -0700)
committerStéphane Wirtel <stephane@wirtel.be>
Wed, 11 Sep 2019 15:29:44 +0000 (17:29 +0200)
Lib/test/test_abc.py
Misc/NEWS.d/next/C API/2018-08-04-00-59-44.bpo-34331.iaUkmU.rst [new file with mode: 0644]
Objects/typeobject.c

index 9f5afb241aea3a16d6bf3acb4f75b929ce0acf3b..000e5838e3c712b78ee0565bf9b5e80b674d3f91 100644 (file)
@@ -149,6 +149,25 @@ def test_factory(abc_ABCMeta, abc_get_cache_token):
             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,
diff --git a/Misc/NEWS.d/next/C API/2018-08-04-00-59-44.bpo-34331.iaUkmU.rst b/Misc/NEWS.d/next/C API/2018-08-04-00-59-44.bpo-34331.iaUkmU.rst
new file mode 100644 (file)
index 0000000..e45e091
--- /dev/null
@@ -0,0 +1,2 @@
+Use singular/plural noun in error message when instantiating an abstract
+class with non-overriden abstract method(s).
index 7575e5580bf98c4d6db2099ff3c3c205b80c5f52..dfdac9e2e4f56c0e5531ae065107b220e241b424 100644 (file)
@@ -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. */
@@ -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;