From 77e9384b6897b0f70519336b62d0d6f9b8faa1f8 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 26 Mar 2019 17:21:39 +0900 Subject: [PATCH 1/4] fix error message in classmethoddescr_call --- Lib/test/test_descr.py | 5 ++++- Objects/descrobject.c | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index e39fea615db350..9125dde1cd2c75 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1601,8 +1601,11 @@ class SubSpam(spam.spamlist): pass spam_cm() with self.assertRaises(TypeError): spam_cm(spam.spamlist()) - with self.assertRaises(TypeError): + with self.assertRaises(TypeError) as cm: spam_cm(list) + self.assertEqual(cm.exception.args[0], + "descriptor 'classmeth' requires a subtype of " + "'spamlist' but received 'list'") def test_staticmethods(self): # Testing static methods... diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 22546a563a51c0..de44ca74a6f1f0 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -325,10 +325,10 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyErr_Format(PyExc_TypeError, "descriptor '%V' " "requires a subtype of '%.100s' " - "but received '%.100s", + "but received '%.100s'", descr_name((PyDescrObject *)descr), "?", PyDescr_TYPE(descr)->tp_name, - self->ob_type->tp_name); + ((PyTypeObject*)self)->tp_name); return NULL; } From 563b9b89c85addb4a08c9c59409baef332874708 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 26 Mar 2019 17:30:10 +0900 Subject: [PATCH 2/4] fix more messages --- Lib/test/test_descr.py | 22 +++++++++++++++++----- Objects/descrobject.c | 7 +++---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 9125dde1cd2c75..131fecf77a239b 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1597,15 +1597,27 @@ class SubSpam(spam.spamlist): pass 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( + cm.exception.args[0], + "descriptor 'classmeth' of 'xxsubtype.spamlist' " + "object needs an argument") + + with self.assertRaises(TypeError) as cm: spam_cm(spam.spamlist()) + self.assertEqual( + cm.exception.args[0], + "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' " + "but received a 'xxsubtype.spamlist' instance") + with self.assertRaises(TypeError) as cm: spam_cm(list) - self.assertEqual(cm.exception.args[0], - "descriptor 'classmeth' requires a subtype of " - "'spamlist' but received 'list'") + self.assertEqual( + cm.exception.args[0], + "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' " + "but received 'list'") def test_staticmethods(self): # Testing static methods... diff --git a/Objects/descrobject.c b/Objects/descrobject.c index de44ca74a6f1f0..510a371f8f42a9 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -314,8 +314,8 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args, self = PyTuple_GET_ITEM(args, 0); if (!PyType_Check(self)) { PyErr_Format(PyExc_TypeError, - "descriptor '%V' requires a type " - "but received a '%.100s'", + "descriptor '%V' requires a subtype of '%.100s' " + "but received a '%.100s' instance", descr_name((PyDescrObject *)descr), "?", PyDescr_TYPE(descr)->tp_name, self->ob_type->tp_name); @@ -323,8 +323,7 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args, } if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) { PyErr_Format(PyExc_TypeError, - "descriptor '%V' " - "requires a subtype of '%.100s' " + "descriptor '%V' requires a subtype of '%.100s' " "but received '%.100s'", descr_name((PyDescrObject *)descr), "?", PyDescr_TYPE(descr)->tp_name, From 0b6d6d3e66c7270e10336af1798172cc406554f4 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 26 Mar 2019 17:30:18 +0900 Subject: [PATCH 3/4] add NEWS --- .../Core and Builtins/2019-03-26-17-23-02.bpo-36433.-8XzZf.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-03-26-17-23-02.bpo-36433.-8XzZf.rst 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 index 00000000000000..6d1bd288bda1f3 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-03-26-17-23-02.bpo-36433.-8XzZf.rst @@ -0,0 +1 @@ +Fixed TypeError message in classmethoddescr_call. From 8c447d5358943824e3ae710b3e074a6e29fc97e4 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Tue, 26 Mar 2019 18:05:46 +0900 Subject: [PATCH 4/4] fix based on Serhiy's comment --- Lib/test/test_descr.py | 8 ++++---- Objects/descrobject.c | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 131fecf77a239b..09eef8c56f30df 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1601,21 +1601,21 @@ class SubSpam(spam.spamlist): pass with self.assertRaises(TypeError) as cm: spam_cm() self.assertEqual( - cm.exception.args[0], + str(cm.exception), "descriptor 'classmeth' of 'xxsubtype.spamlist' " "object needs an argument") with self.assertRaises(TypeError) as cm: spam_cm(spam.spamlist()) self.assertEqual( - cm.exception.args[0], - "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' " + 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( - cm.exception.args[0], + str(cm.exception), "descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' " "but received 'list'") diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 510a371f8f42a9..ab4151ec93e039 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -314,10 +314,9 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args, self = PyTuple_GET_ITEM(args, 0); if (!PyType_Check(self)) { PyErr_Format(PyExc_TypeError, - "descriptor '%V' requires a subtype of '%.100s' " + "descriptor '%V' requires a type " "but received a '%.100s' instance", descr_name((PyDescrObject *)descr), "?", - PyDescr_TYPE(descr)->tp_name, self->ob_type->tp_name); return NULL; }