gh-110782: Fix crash when TypeVar is constructed with keyword args (#110784)

This commit is contained in:
Jelle Zijlstra 2023-10-12 16:00:12 -07:00 committed by GitHub
parent 2c472a87c7
commit d2a536b170
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 18 deletions

View File

@ -554,6 +554,12 @@ class TypeVarTests(BaseTestCase):
vals[x] = cls(str(x))
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]]:
"""Renders templates with possible combinations of replacements.

View File

@ -0,0 +1,2 @@
Fix crash when :class:`typing.TypeVar` is constructed with a keyword
argument. Patch by Jelle Zijlstra.

View File

@ -364,6 +364,7 @@ typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
}
}
if (constraints != NULL) {
if (!PyTuple_CheckExact(constraints)) {
PyErr_SetString(PyExc_TypeError,
"constraints must be a tuple");
@ -383,6 +384,7 @@ typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
Py_XDECREF(bound);
return NULL;
}
}
PyObject *module = caller();
if (module == NULL) {
Py_XDECREF(bound);