From 4784b4d38846fd7f945592c1846f1cc016111f7d Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sat, 5 Dec 2020 22:36:43 +0800 Subject: [PATCH 1/2] raise typeerror when kwargs are passed into genericalias rather than crash --- .../Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst | 3 +++ Objects/genericaliasobject.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2020-12-05-22-34-47.bpo-42576.lEeEl7.rst 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 00000000000000..7290b47dcc15ca --- /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 6102e05c165c5d..51a12377b7e308 100644 --- a/Objects/genericaliasobject.c +++ b/Objects/genericaliasobject.c @@ -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)) { From f7a915982dfd6dde87a9319c9c1591f5559452ef Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sat, 5 Dec 2020 22:41:31 +0800 Subject: [PATCH 2/2] add tests --- Lib/test/test_genericalias.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 912fb33af1a21b..c113e538248e9c 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__": unittest.main()