From: da-woods Date: Thu, 18 Sep 2025 21:08:49 +0000 (+0100) Subject: gh-119494: Fix error messages for deleting/setting type attributes (#119495) X-Git-Tag: v3.15.0a1~261 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ac5c5d42a2e542b3d036cdee80c6449157c03e96;p=thirdparty%2FPython%2Fcpython.git gh-119494: Fix error messages for deleting/setting type attributes (#119495) --- diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 39b835b03fc5..14f94285d3f3 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4078,7 +4078,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): self.assertEqual(C2.__subclasses__(), [D]) with self.assertRaisesRegex(TypeError, - "cannot delete '__bases__' attribute of immutable type"): + "cannot delete '__bases__' attribute of type 'D'"): del D.__bases__ with self.assertRaisesRegex(TypeError, 'can only assign non-empty tuple'): D.__bases__ = () @@ -5062,7 +5062,7 @@ class ClassPropertiesAndMethods(unittest.TestCase): with self.assertRaises(TypeError) as cm: type(X).__dict__["__doc__"].__delete__(X) - self.assertIn("cannot delete '__doc__' attribute of immutable type 'X'", str(cm.exception)) + self.assertIn("cannot delete '__doc__' attribute of type 'X'", str(cm.exception)) self.assertEqual(X.__doc__, "banana") def test_qualname(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst new file mode 100644 index 000000000000..b8076fedda3c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst @@ -0,0 +1 @@ +Exception text when trying to delete attributes of types was clarified. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 0a222a5384f6..7d03655e77a0 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1465,15 +1465,15 @@ static PyMemberDef type_members[] = { static int check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) { - if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) { + if (!value) { PyErr_Format(PyExc_TypeError, - "cannot set '%s' attribute of immutable type '%s'", + "cannot delete '%s' attribute of type '%s'", name, type->tp_name); return 0; } - if (!value) { + if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) { PyErr_Format(PyExc_TypeError, - "cannot delete '%s' attribute of immutable type '%s'", + "cannot set '%s' attribute of immutable type '%s'", name, type->tp_name); return 0; }