bpo-36433: fix confusing error messages in classmethoddescr_call (GH-… · python/cpython@871309c (original) (raw)

File tree

3 files changed

lines changed

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -1597,12 +1597,27 @@ class SubSpam(spam.spamlist): pass
1597 1597 self.assertEqual(x2, SubSpam)
1598 1598 self.assertEqual(a2, a1)
1599 1599 self.assertEqual(d2, d1)
1600 -with self.assertRaises(TypeError):
1600 +
1601 +with self.assertRaises(TypeError) as cm:
1601 1602 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:
1603 1609 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:
1605 1616 spam_cm(list)
1617 +self.assertEqual(
1618 +str(cm.exception),
1619 +"descriptor 'classmeth' requires a subtype of 'xxsubtype.spamlist' "
1620 +"but received 'list'")
1606 1621
1607 1622 def test_staticmethods(self):
1608 1623 # Testing static methods...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 +Fixed TypeError message in classmethoddescr_call.
Original file line number Diff line number Diff line change
@@ -315,20 +315,18 @@ classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
315 315 if (!PyType_Check(self)) {
316 316 PyErr_Format(PyExc_TypeError,
317 317 "descriptor '%V' requires a type "
318 -"but received a '%.100s'",
318 +"but received a '%.100s' instance",
319 319 descr_name((PyDescrObject *)descr), "?",
320 -PyDescr_TYPE(descr)->tp_name,
321 320 self->ob_type->tp_name);
322 321 return NULL;
323 322 }
324 323 if (!PyType_IsSubtype((PyTypeObject *)self, PyDescr_TYPE(descr))) {
325 324 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'",
329 327 descr_name((PyDescrObject *)descr), "?",
330 328 PyDescr_TYPE(descr)->tp_name,
331 -self->ob_type->tp_name);
329 +((PyTypeObject*)self)->tp_name);
332 330 return NULL;
333 331 }
334 332