]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-119494: Fix error messages for deleting/setting type attributes (#119495)
authorda-woods <dw-git@d-woods.co.uk>
Thu, 18 Sep 2025 21:08:49 +0000 (22:08 +0100)
committerGitHub <noreply@github.com>
Thu, 18 Sep 2025 21:08:49 +0000 (22:08 +0100)
Lib/test/test_descr.py
Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst [new file with mode: 0644]
Objects/typeobject.c

index 39b835b03fc599a463f514be4f0b3cb7e1f1e677..14f94285d3f3c27dbe8a3fde4df29748dab2ac51 100644 (file)
@@ -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 (file)
index 0000000..b8076fe
--- /dev/null
@@ -0,0 +1 @@
+Exception text when trying to delete attributes of types was clarified.
index 0a222a5384f67be68194452ebfaaa7bbe130a0d5..7d03655e77a0bbc048d18dd06ce77200198f0f3e 100644 (file)
@@ -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;
     }