]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-110782: Fix crash when TypeVar is constructed with keyword args (GH-110784...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 12 Oct 2023 23:24:37 +0000 (01:24 +0200)
committerGitHub <noreply@github.com>
Thu, 12 Oct 2023 23:24:37 +0000 (23:24 +0000)
gh-110782: Fix crash when TypeVar is constructed with keyword args (GH-110784)
(cherry picked from commit d2a536b1706d4a79303b7ac53684bb82eac2de23)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Lib/test/test_typing.py
Misc/NEWS.d/next/Core and Builtins/2023-10-12-15-03-24.gh-issue-110782.EqzIzi.rst [new file with mode: 0644]
Objects/typevarobject.c

index 03f03fea3b04d46fa03ae5759cfe48e4f1d807a1..957d7afe7ca4cfcd7803f9ca0cf4671ce6e6590a 100644 (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.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-12-15-03-24.gh-issue-110782.EqzIzi.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-12-15-03-24.gh-issue-110782.EqzIzi.rst
new file mode 100644 (file)
index 0000000..6eddcc0
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash when :class:`typing.TypeVar` is constructed with a keyword
+argument. Patch by Jelle Zijlstra.
index 069e1d98ea49788d3d5161cdc074ec1def4a098b..db9c2191d60090933b215a8e4396f147e9113aad 100644 (file)
@@ -364,24 +364,26 @@ typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
         }
     }
 
-    if (!PyTuple_CheckExact(constraints)) {
-        PyErr_SetString(PyExc_TypeError,
-                        "constraints must be a tuple");
-        return NULL;
-    }
-    Py_ssize_t n_constraints = PyTuple_GET_SIZE(constraints);
-    if (n_constraints == 1) {
-        PyErr_SetString(PyExc_TypeError,
-                        "A single constraint is not allowed");
-        Py_XDECREF(bound);
-        return NULL;
-    } else if (n_constraints == 0) {
-        constraints = NULL;
-    } else if (bound != NULL) {
-        PyErr_SetString(PyExc_TypeError,
-                        "Constraints cannot be combined with bound=...");
-        Py_XDECREF(bound);
-        return NULL;
+    if (constraints != NULL) {
+        if (!PyTuple_CheckExact(constraints)) {
+            PyErr_SetString(PyExc_TypeError,
+                            "constraints must be a tuple");
+            return NULL;
+        }
+        Py_ssize_t n_constraints = PyTuple_GET_SIZE(constraints);
+        if (n_constraints == 1) {
+            PyErr_SetString(PyExc_TypeError,
+                            "A single constraint is not allowed");
+            Py_XDECREF(bound);
+            return NULL;
+        } else if (n_constraints == 0) {
+            constraints = NULL;
+        } else if (bound != NULL) {
+            PyErr_SetString(PyExc_TypeError,
+                            "Constraints cannot be combined with bound=...");
+            Py_XDECREF(bound);
+            return NULL;
+        }
     }
     PyObject *module = caller();
     if (module == NULL) {