Skip to content

Commit ac5c5d4

Browse files
authored
gh-119494: Fix error messages for deleting/setting type attributes (#119495)
1 parent 3eec897 commit ac5c5d4

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

Lib/test/test_descr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4078,7 +4078,7 @@ class E(D):
40784078
self.assertEqual(C2.__subclasses__(), [D])
40794079

40804080
with self.assertRaisesRegex(TypeError,
4081-
"cannot delete '__bases__' attribute of immutable type"):
4081+
"cannot delete '__bases__' attribute of type 'D'"):
40824082
del D.__bases__
40834083
with self.assertRaisesRegex(TypeError, 'can only assign non-empty tuple'):
40844084
D.__bases__ = ()
@@ -5062,7 +5062,7 @@ class X:
50625062

50635063
with self.assertRaises(TypeError) as cm:
50645064
type(X).__dict__["__doc__"].__delete__(X)
5065-
self.assertIn("cannot delete '__doc__' attribute of immutable type 'X'", str(cm.exception))
5065+
self.assertIn("cannot delete '__doc__' attribute of type 'X'", str(cm.exception))
50665066
self.assertEqual(X.__doc__, "banana")
50675067

50685068
def test_qualname(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Exception text when trying to delete attributes of types was clarified.

Objects/typeobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,15 +1465,15 @@ static PyMemberDef type_members[] = {
14651465
static int
14661466
check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name)
14671467
{
1468-
if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) {
1468+
if (!value) {
14691469
PyErr_Format(PyExc_TypeError,
1470-
"cannot set '%s' attribute of immutable type '%s'",
1470+
"cannot delete '%s' attribute of type '%s'",
14711471
name, type->tp_name);
14721472
return 0;
14731473
}
1474-
if (!value) {
1474+
if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) {
14751475
PyErr_Format(PyExc_TypeError,
1476-
"cannot delete '%s' attribute of immutable type '%s'",
1476+
"cannot set '%s' attribute of immutable type '%s'",
14771477
name, type->tp_name);
14781478
return 0;
14791479
}

0 commit comments

Comments
 (0)