bpo-31655: Validate keyword names in SimpleNamespace constructor. (#3909)

This commit is contained in:
Serhiy Storchaka 2017-10-07 22:59:35 +03:00 committed by GitHub
parent 28f713601d
commit 79ba471488
2 changed files with 7 additions and 1 deletions

View File

@ -1069,6 +1069,8 @@ class SimpleNamespaceTests(unittest.TestCase):
with self.assertRaises(TypeError):
types.SimpleNamespace(1, 2, 3)
with self.assertRaises(TypeError):
types.SimpleNamespace(**{1: 2})
self.assertEqual(len(ns1.__dict__), 0)
self.assertEqual(vars(ns1), {})

View File

@ -44,8 +44,12 @@ namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds)
PyErr_Format(PyExc_TypeError, "no positional arguments expected");
return -1;
}
if (kwds == NULL)
if (kwds == NULL) {
return 0;
}
if (!PyArg_ValidateKeywordArguments(kwds)) {
return -1;
}
return PyDict_Update(ns->ns_dict, kwds);
}