[Python-checkins] bpo-42576: Raise TypeError when passing in keyword arguments to GenericAlias (GH-23656)

miss-islington webhook-mailer at python.org
Sat Dec 5 11:24:42 EST 2020

commit: d5c029b1a9b47737efb8966f804d28b99a2de239
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2020-12-05T08:24:38-08:00

bpo-42576: Raise TypeError when passing in keyword arguments to GenericAlias (GH-23656)

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
(cherry picked from commit 804d6893b801e8f30318afc38c20d4d0e6161db3)

Co-authored-by: kj <28750310+Fidget-Spinner at users.noreply.github.com>

A Misc/NEWS.d/next/Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst
M Lib/test/test_genericalias.py
M Objects/genericaliasobject.c

diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py
index 912fb33af1a21..c113e538248e9 100644
--- a/Lib/test/test_genericalias.py
+++ b/Lib/test/test_genericalias.py
@@ -302,6 +302,11 @@ def test_weakref(self):
                 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__":
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
index 0000000000000..7290b47dcc15c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst	
@@ -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.
diff --git a/Objects/genericaliasobject.c b/Objects/genericaliasobject.c
index 4b6c8c6ed8d1f..c5a81a5c1aab2 100644
--- a/Objects/genericaliasobject.c
+++ b/Objects/genericaliasobject.c
@@ -566,7 +566,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)) {

More information about the Python-checkins mailing list