mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
[3.13] gh-127190: Fix local_setattro() error handling (GH-127366) (#127367)
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 20657fbdb1
)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
a442c87c4c
commit
5017c81824
2 changed files with 16 additions and 1 deletions
|
@ -208,6 +208,21 @@ class BaseLocalTest:
|
||||||
|
|
||||||
_testcapi.join_temporary_c_thread()
|
_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):
|
class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
|
||||||
_local = _thread._local
|
_local = _thread._local
|
||||||
|
|
|
@ -1688,7 +1688,7 @@ local_setattro(localobject *self, PyObject *name, PyObject *v)
|
||||||
}
|
}
|
||||||
if (r == 1) {
|
if (r == 1) {
|
||||||
PyErr_Format(PyExc_AttributeError,
|
PyErr_Format(PyExc_AttributeError,
|
||||||
"'%.100s' object attribute '%U' is read-only",
|
"'%.100s' object attribute %R is read-only",
|
||||||
Py_TYPE(self)->tp_name, name);
|
Py_TYPE(self)->tp_name, name);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue