Skip to content

Commit c68be6b

Browse files
vstinnersrinivasreddy
authored andcommitted
pythongh-127190: Fix local_setattro() error handling (python#127366)
Don't make the assumption that the 'name' argument is a string. Use repr() to format the 'name' argument instead.
1 parent b7deb0c commit c68be6b

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
@@ -1624,7 +1624,7 @@ local_setattro(localobject *self, PyObject *name, PyObject *v)
16241624
}
16251625
if (r == 1) {
16261626
PyErr_Format(PyExc_AttributeError,
1627-
"'%.100s' object attribute '%U' is read-only",
1627+
"'%.100s' object attribute %R is read-only",
16281628
Py_TYPE(self)->tp_name, name);
16291629
goto err;
16301630
}

0 commit comments

Comments
 (0)