mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
bpo-43908: check_set_special_type_attr() checks Py_TPFLAGS_IMMUTABLETYPE (GH-25743)
check_set_special_type_attr() and type_set_annotations() now check for immutable flag (Py_TPFLAGS_IMMUTABLETYPE). Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
9032cf5cb1
commit
64141382ec
2 changed files with 15 additions and 9 deletions
|
@ -466,14 +466,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;
|
||||
}
|
||||
|
||||
|
@ -978,8 +980,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;
|
||||
}
|
||||
|
||||
|
@ -3953,8 +3957,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)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue