Skip to content

Commit 7d175ca

Browse files
[3.12] pythongh-127190: Fix local_setattro() error handling (pythonGH-127366) (python#127368)
pythongh-127190: Fix local_setattro() error handling (pythonGH-127366) Don't make the assumption that the 'name' argument is a string. Use repr() to format the 'name' argument instead. (cherry picked from commit 20657fb) Co-authored-by: Victor Stinner <[email protected]>
1 parent f4c9f39 commit 7d175ca

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/test/test_threading_local.py

+15
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ def test_threading_local_clear_race(self):
208208

209209
_testcapi.join_temporary_c_thread()
210210

211+
@support.cpython_only
212+
def test_error(self):
213+
class Loop(self._local):
214+
attr = 1
215+
216+
# Trick the "if name == '__dict__':" test of __setattr__()
217+
# to always be true
218+
class NameCompareTrue:
219+
def __eq__(self, other):
220+
return True
221+
222+
loop = Loop()
223+
with self.assertRaisesRegex(AttributeError, 'Loop.*read-only'):
224+
loop.__setattr__(NameCompareTrue(), 2)
225+
211226

212227
class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
213228
_local = _thread._local

Modules/_threadmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ local_setattro(localobject *self, PyObject *name, PyObject *v)
961961
}
962962
if (r == 1) {
963963
PyErr_Format(PyExc_AttributeError,
964-
"'%.100s' object attribute '%U' is read-only",
964+
"'%.100s' object attribute %R is read-only",
965965
Py_TYPE(self)->tp_name, name);
966966
return -1;
967967
}

0 commit comments

Comments
 (0)