From af4f903c1f8b0fc532a53a6528bda0ef797266e4 Mon Sep 17 00:00:00 2001 From: da-woods Date: Fri, 24 May 2024 07:48:03 +0100 Subject: [PATCH 1/7] Fix error messages for deleting/setting type attributes Deleting is always an error and doesn't depend on mutability. Setting is tested after deleting. --- Objects/typeobject.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 11f9c570ac4971..8bc83fdb4eefc4 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1125,15 +1125,16 @@ 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; } From c18398163d14d456eb79c66eaa5d8c8bd503c7d9 Mon Sep 17 00:00:00 2001 From: da-woods Date: Fri, 24 May 2024 07:59:05 +0100 Subject: [PATCH 2/7] Appease linter --- Objects/typeobject.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 8bc83fdb4eefc4..2d75ac9bdcccea 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1125,7 +1125,6 @@ static PyMemberDef type_members[] = { static int check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name) { - if (!value) { PyErr_Format(PyExc_TypeError, "cannot delete '%s' attribute of type '%s'", From 11ff423f2e783a29355123321368aab1679034d5 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 24 May 2024 07:02:49 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst 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 00000000000000..b8076fedda3c4f --- /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. From 20668a61bcda5904fca71573c583d210f2b9725b Mon Sep 17 00:00:00 2001 From: da-woods Date: Fri, 24 May 2024 17:38:46 +0100 Subject: [PATCH 4/7] Update test with new message --- Lib/test/test_descr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index c3f292467a6738..de75c4ea1dae36 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4855,7 +4855,7 @@ class X: 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): From f69e70b8cc20545b89a5c29d48c532e13529f9a2 Mon Sep 17 00:00:00 2001 From: da-woods Date: Thu, 18 Sep 2025 13:48:09 +0100 Subject: [PATCH 5/7] try removing blank line From 4e8ee58dc894fbe7402af80a356fc522bc87bd2c Mon Sep 17 00:00:00 2001 From: da-woods Date: Thu, 18 Sep 2025 18:28:50 +0100 Subject: [PATCH 6/7] Move news file --- .../2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/{Core and Builtins => Core_and_Builtins}/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst (100%) 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 similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst rename to Misc/NEWS.d/next/Core_and_Builtins/2024-05-24-07-02-47.gh-issue-119494.x3KUMC.rst From d0a3897f1ccc67d1485c184af818e86a7f44b76e Mon Sep 17 00:00:00 2001 From: da-woods Date: Thu, 18 Sep 2025 19:09:06 +0100 Subject: [PATCH 7/7] Update test_descr --- Lib/test/test_descr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 054991b3d437a2..14f94285d3f3c2 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -4078,7 +4078,7 @@ class E(D): 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__ = ()