Skip to content

Commit 871309c

Browse files
methanemiss-islington
authored andcommitted
bpo-36433: fix confusing error messages in classmethoddescr_call (GH-12556)
https://bugs.python.org/issue36433
1 parent b4d8f28 commit 871309c

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

Lib/test/test_descr.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,12 +1597,27 @@ class SubSpam(spam.spamlist): pass
15971597
self.assertEqual(x2, SubSpam)
15981598
self.assertEqual(a2, a1)
15991599
self.assertEqual(d2, d1)
1600-
with self.assertRaises(TypeError):
1600+
1601+
with self.assertRaises(TypeError) as cm:
16011602
spam_cm()
1602-
with self.assertRaises(TypeError):
1603+
self.assertEqual(
1604+
str(cm.exception),
1605+
"descriptor 'classmeth' of 'xxsubtype.spamlist' "
1606+
"object needs an argument")
1607+
1608+
with self.assertRaises(TypeError) as cm:
16031609
spam_cm(spam.spamlist())
1604-
with self.assertRaises(TypeError):
1610+
self.assertEqual(
1611+
str(cm.exception),
1612+
"descriptor 'classmeth' requires a type "
1613+
"but received a 'xxsubtype.spamlist' instance")
1614+
1615+
with self.assertRaises(TypeError) as cm:
16051616
spam_cm(list)
1617+
self.assertEqual(
1618+
str(cm.exception),
1619+
"descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' "
1620+
"but received 'list'")
16061621

16071622
def test_staticmethods(self):
16081623
# Testing static methods...
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed TypeError message in classmethoddescr_call.

Objects/descrobject.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,20 +315,18 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
315315
if (!PyType_Check(self)) {
316316
PyErr_Format(PyExc_TypeError,
317317
"descriptor '%V' requires a type "
318-
"but received a '%.100s'",
318+
"but received a '%.100s' instance",
319319
descr_name((PyDescrObject *)descr), "?",
320-
PyDescr_TYPE(descr)->tp_name,
321320
self->ob_type->tp_name);
322321
return NULL;
323322
}
324323
if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
325324
PyErr_Format(PyExc_TypeError,
326-
"descriptor '%V' "
327-
"requires a subtype of '%.100s' "
328-
"but received '%.100s",
325+
"descriptor '%V' requires a subtype of '%.100s' "
326+
"but received '%.100s'",
329327
descr_name((PyDescrObject *)descr), "?",
330328
PyDescr_TYPE(descr)->tp_name,
331-
self->ob_type->tp_name);
329+
((PyTypeObject*)self)->tp_name);
332330
return NULL;
333331
}
334332

0 commit comments

Comments
 (0)