Skip to content

Commit

Permalink
pythongh-127190: Fix local_setattro() error handling
Browse files Browse the repository at this point in the history
Don't make the assumption that the 'name' argument is a string. Use
repr() to format the 'name' argument instead.
  • Loading branch information
vstinner committed Nov 28, 2024
1 parent db5c576 commit 77a860a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Lib/test/test_threading_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 77a860a

Please sign in to comment.