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
This commit is contained in:
kj 2020-12-05 23:02:14 +07:00 committed by GitHub
parent da3d2abe6b
commit 804d6893b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 1 deletions

View File

@ -302,6 +302,11 @@ class BaseTest(unittest.TestCase):
alias = t[int] alias = t[int]
self.assertEqual(ref(alias)(), alias) self.assertEqual(ref(alias)(), alias)
def test_no_kwargs(self):
# bpo-42576
with self.assertRaises(TypeError):
GenericAlias(bad=float)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -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.

View File

@ -567,7 +567,7 @@ static PyGetSetDef ga_properties[] = {
static PyObject * static PyObject *
ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ga_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
if (!_PyArg_NoKwnames("GenericAlias", kwds)) { if (!_PyArg_NoKeywords("GenericAlias", kwds)) {
return NULL; return NULL;
} }
if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) { if (!_PyArg_CheckPositional("GenericAlias", PyTuple_GET_SIZE(args), 2, 2)) {