Skip to content

bpo-43908: check_set_special_type_attr() and type_set_annotations() now check for immutable flag #25743

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

Merged
merged 8 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4763,12 +4763,14 @@ class X:
"elephant"
X.__doc__ = "banana"
self.assertEqual(X.__doc__, "banana")

with self.assertRaises(TypeError) as cm:
type(list).__dict__["__doc__"].__set__(list, "blah")
self.assertIn("can't set list.__doc__", str(cm.exception))
self.assertIn("cannot set '__doc__' attribute of immutable type 'list'", str(cm.exception))

with self.assertRaises(TypeError) as cm:
type(X).__dict__["__doc__"].__delete__(X)
self.assertIn("can't delete X.__doc__", str(cm.exception))
self.assertIn("cannot delete '__doc__' attribute of immutable type 'X'", str(cm.exception))
self.assertEqual(X.__doc__, "banana")

def test_qualname(self):
Expand Down
18 changes: 11 additions & 7 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,16 @@ static PyMemberDef type_members[] = {
static int
check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name)
{
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) {
PyErr_Format(PyExc_TypeError,
"can't set %s.%s", type->tp_name, name);
"cannot set '%s' attribute of immutable type '%s'",
name, type->tp_name);
return 0;
}
if (!value) {
PyErr_Format(PyExc_TypeError,
"can't delete %s.%s", type->tp_name, name);
"cannot delete '%s' attribute of immutable type '%s'",
name, type->tp_name);
return 0;
}

Expand Down Expand Up @@ -973,8 +975,10 @@ type_get_annotations(PyTypeObject *type, void *context)
static int
type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
{
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(PyExc_TypeError, "can't set attributes of built-in/extension type '%s'", type->tp_name);
if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) {
PyErr_Format(PyExc_TypeError,
"cannot set '__annotations__' attribute of immutable type '%s'",
type->tp_name);
return -1;
}

Expand Down Expand Up @@ -3947,8 +3951,8 @@ type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
PyErr_Format(
PyExc_TypeError,
"can't set attributes of built-in/extension type '%s'",
type->tp_name);
"cannot set %R attribute of immutable type '%s'",
name, type->tp_name);
return -1;
}
if (PyUnicode_Check(name)) {
Expand Down