Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,13 @@ def test_replace_reject_unknown_instance_fields(self):
self.assertIs(node.ctx, context)
self.assertRaises(AttributeError, getattr, node, 'unknown')

def test_replace_non_str_kwarg(self):
node = ast.Name(id="x")
errmsg = "got an unexpected keyword argument <object object"
with self.assertRaisesRegex(TypeError, errmsg):
node.__replace__(**{object(): "y"})


class ASTHelpers_Test(unittest.TestCase):
maxDiff = None

Expand Down Expand Up @@ -3281,6 +3288,27 @@ class _AllFieldTypes(ast.AST):
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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix three crashes when non-string keyword arguments are supplied to objects
in the :mod:`ast` module.
6 changes: 3 additions & 3 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def visitModule(self, mod):
}
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;
Expand All @@ -965,7 +965,7 @@ def visitModule(self, mod):
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
Expand Down Expand Up @@ -1207,7 +1207,7 @@ def visitModule(self, mod):
if (rc == 0) {
PyErr_Format(PyExc_TypeError,
"%.400s.__replace__ got an unexpected keyword "
"argument '%U'.", Py_TYPE(self)->tp_name, key);
"argument %R.", Py_TYPE(self)->tp_name, key);
Py_DECREF(expecting);
return -1;
}
Expand Down
6 changes: 3 additions & 3 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading