mirror of
https://github.com/python/cpython.git
synced 2025-10-07 15:42:02 +00:00
bpo-31655: Validate keyword names in SimpleNamespace constructor. (#3909)
This commit is contained in:
parent
28f713601d
commit
79ba471488
2 changed files with 7 additions and 1 deletions
|
@ -1069,6 +1069,8 @@ class SimpleNamespaceTests(unittest.TestCase):
|
||||||
|
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
types.SimpleNamespace(1, 2, 3)
|
types.SimpleNamespace(1, 2, 3)
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
types.SimpleNamespace(**{1: 2})
|
||||||
|
|
||||||
self.assertEqual(len(ns1.__dict__), 0)
|
self.assertEqual(len(ns1.__dict__), 0)
|
||||||
self.assertEqual(vars(ns1), {})
|
self.assertEqual(vars(ns1), {})
|
||||||
|
|
|
@ -44,8 +44,12 @@ namespace_init(_PyNamespaceObject *ns, PyObject *args, PyObject *kwds)
|
||||||
PyErr_Format(PyExc_TypeError, "no positional arguments expected");
|
PyErr_Format(PyExc_TypeError, "no positional arguments expected");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (kwds == NULL)
|
if (kwds == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
if (!PyArg_ValidateKeywordArguments(kwds)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return PyDict_Update(ns->ns_dict, kwds);
|
return PyDict_Update(ns->ns_dict, kwds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue