self.assertIs(obj.a, None)
self.assertEqual(obj.b, [])
+ def test_non_str_kwarg(self):
+ warn_msg = "got an unexpected keyword argument <object object"
+ with (
+ self.assertRaises(TypeError),
+ self.assertWarnsRegex(DeprecationWarning, warn_msg),
+ ):
+ ast.Name(**{object(): 'y'})
+
+ class FakeStr:
+ def __init__(self, value):
+ self.value = value
+
+ def __hash__(self):
+ return hash(self.value)
+
+ def __eq__(self, other):
+ return isinstance(other, str) and self.value == other
+
+ with self.assertRaisesRegex(TypeError, "got multiple values for argument"):
+ ast.Name("x", **{FakeStr('id'): 'y'})
+
@support.cpython_only
class ModuleStateTests(unittest.TestCase):
--- /dev/null
+Fix three crashes when non-string keyword arguments are supplied to objects
+in the :mod:`ast` module.
}
if (p == 0) {
PyErr_Format(PyExc_TypeError,
- "%.400s got multiple values for argument '%U'",
+ "%.400s got multiple values for argument %R",
Py_TYPE(self)->tp_name, key);
res = -1;
goto cleanup;
else if (contains == 0) {
if (PyErr_WarnFormat(
PyExc_DeprecationWarning, 1,
- "%.400s.__init__ got an unexpected keyword argument '%U'. "
+ "%.400s.__init__ got an unexpected keyword argument %R. "
"Support for arbitrary keyword arguments is deprecated "
"and will be removed in Python 3.15.",
Py_TYPE(self)->tp_name, key
}
if (p == 0) {
PyErr_Format(PyExc_TypeError,
- "%.400s got multiple values for argument '%U'",
+ "%.400s got multiple values for argument %R",
Py_TYPE(self)->tp_name, key);
res = -1;
goto cleanup;
else if (contains == 0) {
if (PyErr_WarnFormat(
PyExc_DeprecationWarning, 1,
- "%.400s.__init__ got an unexpected keyword argument '%U'. "
+ "%.400s.__init__ got an unexpected keyword argument %R. "
"Support for arbitrary keyword arguments is deprecated "
"and will be removed in Python 3.15.",
Py_TYPE(self)->tp_name, key