summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2023-05-19 16:04:47 (GMT)
committerGitHub <noreply@github.com>2023-05-19 16:04:47 (GMT)
commit8f1f3b9abdaa3e9d19aad22d6c310eb1f05ae5c2 (patch)
treef35bc1bd8e1af9e7a773058aac642cde1a6c5766 /Objects
parentdbe171e6098fbb96beed81db2c34f6428109e005 (diff)
downloadcpython-8f1f3b9abdaa3e9d19aad22d6c310eb1f05ae5c2.zip
cpython-8f1f3b9abdaa3e9d19aad22d6c310eb1f05ae5c2.tar.gz
cpython-8f1f3b9abdaa3e9d19aad22d6c310eb1f05ae5c2.tar.bz2
gh-104600: Make type.__type_params__ writable (#104634)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/typeobject.c42
1 files changed, 29 insertions, 13 deletions
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 624dc63..2fbcafe 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -1460,18 +1460,6 @@ type_get_annotations(PyTypeObject *type, void *context)
return annotations;
}
-static PyObject *
-type_get_type_params(PyTypeObject *type, void *context)
-{
- PyObject *params = PyDict_GetItem(lookup_tp_dict(type), &_Py_ID(__type_params__));
-
- if (params) {
- return Py_NewRef(params);
- }
-
- return PyTuple_New(0);
-}
-
static int
type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
{
@@ -1502,6 +1490,34 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
return result;
}
+static PyObject *
+type_get_type_params(PyTypeObject *type, void *context)
+{
+ PyObject *params = PyDict_GetItem(lookup_tp_dict(type), &_Py_ID(__type_params__));
+
+ if (params) {
+ return Py_NewRef(params);
+ }
+
+ return PyTuple_New(0);
+}
+
+static int
+type_set_type_params(PyTypeObject *type, PyObject *value, void *context)
+{
+ if (!check_set_special_type_attr(type, value, "__type_params__")) {
+ return -1;
+ }
+
+ PyObject *dict = lookup_tp_dict(type);
+ int result = PyDict_SetItem(dict, &_Py_ID(__type_params__), value);
+
+ if (result == 0) {
+ PyType_Modified(type);
+ }
+ return result;
+}
+
/*[clinic input]
type.__instancecheck__ -> bool
@@ -1548,7 +1564,7 @@ static PyGetSetDef type_getsets[] = {
{"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL},
{"__text_signature__", (getter)type_get_text_signature, NULL, NULL},
{"__annotations__", (getter)type_get_annotations, (setter)type_set_annotations, NULL},
- {"__type_params__", (getter)type_get_type_params, NULL, NULL},
+ {"__type_params__", (getter)type_get_type_params, (setter)type_set_type_params, NULL},
{0}
};