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 @@ -1443,6 +1443,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 @@ -3304,6 +3311,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);
Comment on lines +945 to 946
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are modifying the error message, you must use the safer %T format:

Suggested change
"%.400s got multiple values for argument %R",
Py_TYPE(self)->tp_name, key);
"%T got multiple values for argument %R",
self, key);

Same remark for the two other changes below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this changes the error message to include the module name:

$ python3.14 -Wall -c 'import ast; ast.Name(idx=3)'
<string>:1: DeprecationWarning: Name.__init__ got an unexpected keyword argument 'idx'. Support for arbitrary keyword arguments is deprecated and will be removed in Python 3.15.
<string>:1: DeprecationWarning: Name.__init__ missing 1 required positional argument: 'id'. This will become an error in Python 3.15.
$ ./python.exe -Wall -c 'import ast; ast.Name(idx=3)'
<string>:1: DeprecationWarning: ast.Name.__init__ got an unexpected keyword argument 'idx'. Support for arbitrary keyword arguments is deprecated and will be removed in Python 3.15.
<string>:1: DeprecationWarning: ast.Name.__init__ missing 1 required positional argument: 'id'. This will become an error in Python 3.15.

I feel like that's a change we should only make in 3.15, not in the bugfix releases.

Why is %T safer?

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