From 2a5a327c46f8f3df1da28f424666f6f020d6de3a Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 25 Sep 2024 15:48:55 -0700 Subject: [PATCH 1/3] gh-119180: Disallow instantiation of ConstEvaluator objects --- Lib/test/test_type_params.py | 11 +++++++++++ Objects/typevarobject.c | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index dc0c0d0829f8d3..6a49f46b4b976b 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -1452,3 +1452,14 @@ def f[T: (int, str)](): pass self.assertEqual(annotationlib.call_evaluate_function(case.evaluate_constraints, annotationlib.Format.VALUE), (int, str)) self.assertEqual(annotationlib.call_evaluate_function(case.evaluate_constraints, annotationlib.Format.FORWARDREF), (int, str)) self.assertEqual(annotationlib.call_evaluate_function(case.evaluate_constraints, annotationlib.Format.SOURCE), '(int, str)') + + def test_const_evaluator(self): + T = TypeVar("T", bound=int) + self.assertEqual(repr(T.evaluate_bound), ">") + + ConstEvaluator = type(T.evaluate_bound) + + with self.assertRaisesRegex(TypeError, r"cannot create '_typing\._ConstEvaluator' instances"): + ConstEvaluator() + with self.assertRaisesRegex(TypeError, r"cannot set 'attribute' attribute of immutable type '_typing\._ConstEvaluator'"): + ConstEvaluator.attribute = 1 diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index d3656155fae330..43754dbc47c019 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -242,7 +242,8 @@ static PyType_Slot constevaluator_slots[] = { PyType_Spec constevaluator_spec = { .name = "_typing._ConstEvaluator", .basicsize = sizeof(constevaluatorobject), - .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE, + .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE + | Py_TPFLAGS_DISALLOW_INSTANTIATION, .slots = constevaluator_slots, }; From 6f62d20b795c9c4fe94fd9bdda168ea113248424 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 25 Sep 2024 15:55:59 -0700 Subject: [PATCH 2/3] Add comment --- Lib/test/test_type_params.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_type_params.py b/Lib/test/test_type_params.py index 6a49f46b4b976b..8c21553e410d8a 100644 --- a/Lib/test/test_type_params.py +++ b/Lib/test/test_type_params.py @@ -1460,6 +1460,6 @@ def test_const_evaluator(self): ConstEvaluator = type(T.evaluate_bound) with self.assertRaisesRegex(TypeError, r"cannot create '_typing\._ConstEvaluator' instances"): - ConstEvaluator() + ConstEvaluator() # This used to segfault. with self.assertRaisesRegex(TypeError, r"cannot set 'attribute' attribute of immutable type '_typing\._ConstEvaluator'"): ConstEvaluator.attribute = 1 From 3d6270630ccfd5b39d364c92a9857ac898ef22ec Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 25 Sep 2024 16:06:00 -0700 Subject: [PATCH 3/3] fix signature of repr() --- Objects/typevarobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 43754dbc47c019..09e9ab39364742 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -151,7 +151,7 @@ constevaluator_clear(PyObject *self) } static PyObject * -constevaluator_repr(PyObject *self, PyObject *repr) +constevaluator_repr(PyObject *self) { PyObject *value = ((constevaluatorobject *)self)->value; return PyUnicode_FromFormat("", value);