mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-110782: Fix crash when TypeVar is constructed with keyword args (GH-110784) (#110787)
gh-110782: Fix crash when TypeVar is constructed with keyword args (GH-110784)
(cherry picked from commit d2a536b170
)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
80f958529b
commit
27d5ea291c
3 changed files with 28 additions and 18 deletions
|
@ -554,6 +554,12 @@ class TypeVarTests(BaseTestCase):
|
||||||
vals[x] = cls(str(x))
|
vals[x] = cls(str(x))
|
||||||
del vals
|
del vals
|
||||||
|
|
||||||
|
def test_constructor(self):
|
||||||
|
T = TypeVar(name="T")
|
||||||
|
self.assertEqual(T.__name__, "T")
|
||||||
|
self.assertEqual(T.__constraints__, ())
|
||||||
|
self.assertIs(T.__bound__, None)
|
||||||
|
|
||||||
|
|
||||||
def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
|
def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
|
||||||
"""Renders templates with possible combinations of replacements.
|
"""Renders templates with possible combinations of replacements.
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix crash when :class:`typing.TypeVar` is constructed with a keyword
|
||||||
|
argument. Patch by Jelle Zijlstra.
|
|
@ -364,24 +364,26 @@ typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!PyTuple_CheckExact(constraints)) {
|
if (constraints != NULL) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
if (!PyTuple_CheckExact(constraints)) {
|
||||||
"constraints must be a tuple");
|
PyErr_SetString(PyExc_TypeError,
|
||||||
return NULL;
|
"constraints must be a tuple");
|
||||||
}
|
return NULL;
|
||||||
Py_ssize_t n_constraints = PyTuple_GET_SIZE(constraints);
|
}
|
||||||
if (n_constraints == 1) {
|
Py_ssize_t n_constraints = PyTuple_GET_SIZE(constraints);
|
||||||
PyErr_SetString(PyExc_TypeError,
|
if (n_constraints == 1) {
|
||||||
"A single constraint is not allowed");
|
PyErr_SetString(PyExc_TypeError,
|
||||||
Py_XDECREF(bound);
|
"A single constraint is not allowed");
|
||||||
return NULL;
|
Py_XDECREF(bound);
|
||||||
} else if (n_constraints == 0) {
|
return NULL;
|
||||||
constraints = NULL;
|
} else if (n_constraints == 0) {
|
||||||
} else if (bound != NULL) {
|
constraints = NULL;
|
||||||
PyErr_SetString(PyExc_TypeError,
|
} else if (bound != NULL) {
|
||||||
"Constraints cannot be combined with bound=...");
|
PyErr_SetString(PyExc_TypeError,
|
||||||
Py_XDECREF(bound);
|
"Constraints cannot be combined with bound=...");
|
||||||
return NULL;
|
Py_XDECREF(bound);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
PyObject *module = caller();
|
PyObject *module = caller();
|
||||||
if (module == NULL) {
|
if (module == NULL) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue