https://github.com/python/cpython/commit/20657fbdb14d50ca4ec115da0cbef155871d8d33
commit: 20657fbdb14d50ca4ec115da0cbef155871d8d33
branch: main
author: Victor Stinner <vstin...@python.org>
committer: vstinner <vstin...@python.org>
date: 2024-11-28T17:35:48+01:00
summary:

gh-127190: Fix local_setattro() error handling (#127366)

Don't make the assumption that the 'name' argument is a string. Use
repr() to format the 'name' argument instead.

files:
M Lib/test/test_threading_local.py
M Modules/_threadmodule.c

diff --git a/Lib/test/test_threading_local.py b/Lib/test/test_threading_local.py
index f0b829a978feb5..3a58afd8194a32 100644
--- a/Lib/test/test_threading_local.py
+++ b/Lib/test/test_threading_local.py
@@ -208,6 +208,21 @@ def test_threading_local_clear_race(self):
 
         _testcapi.join_temporary_c_thread()
 
+    @support.cpython_only
+    def test_error(self):
+        class Loop(self._local):
+            attr = 1
+
+        # Trick the "if name == '__dict__':" test of __setattr__()
+        # to always be true
+        class NameCompareTrue:
+            def __eq__(self, other):
+                return True
+
+        loop = Loop()
+        with self.assertRaisesRegex(AttributeError, 'Loop.*read-only'):
+            loop.__setattr__(NameCompareTrue(), 2)
+
 
 class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
     _local = _thread._local
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index f2a420ac1c589d..4a45445e2f62db 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1624,7 +1624,7 @@ local_setattro(localobject *self, PyObject *name, 
PyObject *v)
     }
     if (r == 1) {
         PyErr_Format(PyExc_AttributeError,
-                     "'%.100s' object attribute '%U' is read-only",
+                     "'%.100s' object attribute %R is read-only",
                      Py_TYPE(self)->tp_name, name);
         goto err;
     }

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to