Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144169: Fix three crashes in AST objects with non-str kwargs#144178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 2 commits intopython:mainfromJelleZijlstra:ast-str-kwarg
Jan 26, 2026
Merged
Show file tree
Hide file tree
Changes fromall 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

Some comments aren't visible on the classic Files Changed page.

28 changes: 28 additions & 0 deletionsLib/test/test_ast/test_ast.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsParser/asdl_c.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
MemberAuthor

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 DownExpand 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 deletionsPython/Python-ast.c
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2026 Movatter.jp