Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-127190: Fix local_setattro() error handling (GH-127366) #127368

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-127190: Fix local_setattro() error handling (GH-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 <vstinner@python.org>
  • Loading branch information
vstinner authored and miss-islington committed Nov 28, 2024
commit d34e204cd772106d6bb030a54c60ea09f4245964
15 changes: 15 additions & 0 deletions Lib/test/test_threading_local.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
@@ -961,7 +961,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);
return -1;
}