[Python-checkins] (no subject)

Stéphane Wirtel webhook-mailer at python.org
Wed Sep 11 11:29:48 EDT 2019




To: python-checkins at python.org
Subject:
 bpo-34331: Fix incorrectly pluralized abstract class error message. (GH-8670)
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

https://github.com/python/cpython/commit/4a12a178f4a6b9a59d97fecc727f2b6b28df=
c85f
commit: 4a12a178f4a6b9a59d97fecc727f2b6b28dfc85f
branch: master
author: Daniel Andrade <dangro at users.noreply.github.com>
committer: St=C3=A9phane Wirtel <stephane at wirtel.be>
date: 2019-09-11T17:29:44+02:00
summary:

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

files:
A Misc/NEWS.d/next/C API/2018-08-04-00-59-44.bpo-34331.iaUkmU.rst
M Lib/test/test_abc.py
M Objects/typeobject.c

diff --git a/Lib/test/test_abc.py b/Lib/test/test_abc.py
index 9f5afb241aea..000e5838e3c7 100644
--- a/Lib/test/test_abc.py
+++ b/Lib/test/test_abc.py
@@ -149,6 +149,25 @@ def foo(): return 4
             self.assertEqual(D.foo(), 4)
             self.assertEqual(D().foo(), 4)
=20
+        def test_object_new_with_one_abstractmethod(self):
+            class C(metaclass=3Dabc_ABCMeta):
+                @abc.abstractmethod
+                def method_one(self):
+                    pass
+            msg =3D r"class C with abstract method method_one"
+            self.assertRaisesRegex(TypeError, msg, C)
+
+        def test_object_new_with_many_abstractmethods(self):
+            class C(metaclass=3Dabc_ABCMeta):
+                @abc.abstractmethod
+                def method_one(self):
+                    pass
+                @abc.abstractmethod
+                def method_two(self):
+                    pass
+            msg =3D 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
index 000000000000..e45e0915dbf5
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2018-08-04-00-59-44.bpo-34331.iaUkmU.rst=09
@@ -0,0 +1,2 @@
+Use singular/plural noun in error message when instantiating an abstract
+class with non-overriden abstract method(s).
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 7575e5580bf9..dfdac9e2e4f5 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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;
=20
         /* Compute ", ".join(sorted(type.__abstractmethods__))
            into joined. */
@@ -3773,14 +3774,18 @@ object_new(PyTypeObject *type, PyObject *args, PyObje=
ct *kwds)
             return NULL;
         }
         joined =3D PyUnicode_Join(comma, sorted_methods);
+        method_count =3D PyObject_Length(sorted_methods);
         Py_DECREF(sorted_methods);
         if (joined =3D=3D NULL)
             return NULL;
+        if (method_count =3D=3D -1)
+            return NULL;
=20
         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;



More information about the Python-checkins mailing list