Skip to content

Commit 79ba471

Browse files
bpo-31655: Validate keyword names in SimpleNamespace constructor. (#3909)
1 parent 28f7136 commit 79ba471

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

Lib/test/test_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,8 @@ def test_constructor(self):
10691069

10701070
with self.assertRaises(TypeError):
10711071
types.SimpleNamespace(1, 2, 3)
1072+
with self.assertRaises(TypeError):
1073+
types.SimpleNamespace(**{1: 2})
10721074

10731075
self.assertEqual(len(ns1.__dict__), 0)
10741076
self.assertEqual(vars(ns1), {})

Objects/namespaceobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds)
4444
PyErr_Format(PyExc_TypeError, "no positional arguments expected");
4545
return -1;
4646
}
47-
if (kwds == NULL)
47+
if (kwds == NULL) {
4848
return 0;
49+
}
50+
if (!PyArg_ValidateKeywordArguments(kwds)) {
51+
return -1;
52+
}
4953
return PyDict_Update(ns->ns_dict, kwds);
5054
}
5155

0 commit comments

Comments
 (0)