]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42576: Raise TypeError when passing in keyword arguments to GenericAlias (GH...
authorkj <28750310+Fidget-Spinner@users.noreply.github.com>
Sat, 5 Dec 2020 16:02:14 +0000 (23:02 +0700)
committerGitHub <noreply@github.com>
Sat, 5 Dec 2020 16:02:14 +0000 (08:02 -0800)
Use `_PyArg_NoKeywords` instead of `_PyArg_NoKwnames` when checking the `kwds` tuple when creating `GenericAlias`. This fixes an interpreter crash when passing in keyword arguments to `GenericAlias`'s constructor.

Needs backport to 3.9.

Automerge-Triggered-By: GH:gvanrossum
Lib/test/test_genericalias.py
Misc/NEWS.d/next/Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst [new file with mode: 0644]
Objects/genericaliasobject.c

index 912fb33af1a21b181f3bc97f5c44ee25c406df1a..c113e538248e9c1e988881009e6917933062fb44 100644 (file)
@@ -302,6 +302,11 @@ class BaseTest(unittest.TestCase):
                 alias = t[int]
                 self.assertEqual(ref(alias)(), alias)
 
+    def test_no_kwargs(self):
+        # bpo-42576
+        with self.assertRaises(TypeError):
+            GenericAlias(bad=float)
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst b/Misc/NEWS.d/next/Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst
new file mode 100644 (file)
index 0000000..7290b47
--- /dev/null
@@ -0,0 +1,3 @@
+``types.GenericAlias`` will now raise a ``TypeError`` when attempting to
+initialize with a keyword argument.  Previously, this would cause the
+interpreter to crash.  Patch by Ken Jin.
index 6102e05c165c5d617538e8d1ff8bfe57a2607f48..51a12377b7e30840c3ec5b10a699ed34e8860348 100644 (file)
@@ -567,7 +567,7 @@ static PyGetSetDef ga_properties[] = {
 static PyObject *
 ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    if (!_PyArg_NoKwnames("GenericAlias", kwds)) {
+    if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
         return NULL;
     }
     if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {