]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36433: fix confusing error messages in classmethoddescr_call (GH-12556)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 26 Mar 2019 09:47:08 +0000 (02:47 -0700)
committerGitHub <noreply@github.com>
Tue, 26 Mar 2019 09:47:08 +0000 (02:47 -0700)
https://bugs.python.org/issue36433
(cherry picked from commit 871309c775fd4d72048bfaa31affd54f9934f7dd)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
Lib/test/test_descr.py
Misc/NEWS.d/next/Core and Builtins/2019-03-26-17-23-02.bpo-36433.-8XzZf.rst [new file with mode: 0644]
Objects/descrobject.c

index 0e7728ebf2d7a2d9f7bf1debfa95be4854b7f716..d9541b9133f1ab7e730f031ed0a6fe8b8268cb35 100644 (file)
@@ -1592,12 +1592,27 @@ order (MRO) for bases """
         self.assertEqual(x2, SubSpam)
         self.assertEqual(a2, a1)
         self.assertEqual(d2, d1)
-        with self.assertRaises(TypeError):
+
+        with self.assertRaises(TypeError) as cm:
             spam_cm()
-        with self.assertRaises(TypeError):
+        self.assertEqual(
+            str(cm.exception),
+            "descriptor 'classmeth' of 'xxsubtype.spamlist' "
+            "object needs an argument")
+
+        with self.assertRaises(TypeError) as cm:
             spam_cm(spam.spamlist())
-        with self.assertRaises(TypeError):
+        self.assertEqual(
+            str(cm.exception),
+            "descriptor 'classmeth' requires a type "
+            "but received a 'xxsubtype.spamlist' instance")
+
+        with self.assertRaises(TypeError) as cm:
             spam_cm(list)
+        self.assertEqual(
+            str(cm.exception),
+            "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' "
+            "but received 'list'")
 
     def test_staticmethods(self):
         # Testing static methods...
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-03-26-17-23-02.bpo-36433.-8XzZf.rst b/Misc/NEWS.d/next/Core and Builtins/2019-03-26-17-23-02.bpo-36433.-8XzZf.rst
new file mode 100644 (file)
index 0000000..6d1bd28
--- /dev/null
@@ -0,0 +1 @@
+Fixed TypeError message in classmethoddescr_call.
index 277fed995615d470cdc5e46473bf7bed026c6d92..370b7a75e8b4238da682e915a05702c5a3dff001 100644 (file)
@@ -313,20 +313,18 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
     if (!PyType_Check(self)) {
         PyErr_Format(PyExc_TypeError,
                      "descriptor '%V' requires a type "
-                     "but received a '%.100s'",
+                     "but received a '%.100s' instance",
                      descr_name((PyDescrObject *)descr), "?",
-                     PyDescr_TYPE(descr)->tp_name,
                      self->ob_type->tp_name);
         return NULL;
     }
     if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
         PyErr_Format(PyExc_TypeError,
-                     "descriptor '%V' "
-                     "requires a subtype of '%.100s' "
-                     "but received '%.100s",
+                     "descriptor '%V' requires a subtype of '%.100s' "
+                     "but received '%.100s'",
                      descr_name((PyDescrObject *)descr), "?",
                      PyDescr_TYPE(descr)->tp_name,
-                     self->ob_type->tp_name);
+                     ((PyTypeObject*)self)->tp_name);
         return NULL;
     }