Skip to content

Commit bc92e78

Browse files
[3.13] pythongh-144169: Fix three crashes in AST objects with non-str kwargs (pythonGH-144178) (python#144260)
(cherry picked from commit 639c1ad) Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Victor Stinner <[email protected]>
1 parent b5129b8 commit bc92e78

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,6 +3141,27 @@ class _AllFieldTypes(ast.AST):
31413141
self.assertIs(obj.a, None)
31423142
self.assertEqual(obj.b, [])
31433143

3144+
def test_non_str_kwarg(self):
3145+
warn_msg = "got an unexpected keyword argument <object object"
3146+
with (
3147+
self.assertRaises(TypeError),
3148+
self.assertWarnsRegex(DeprecationWarning, warn_msg),
3149+
):
3150+
ast.Name(**{object(): 'y'})
3151+
3152+
class FakeStr:
3153+
def __init__(self, value):
3154+
self.value = value
3155+
3156+
def __hash__(self):
3157+
return hash(self.value)
3158+
3159+
def __eq__(self, other):
3160+
return isinstance(other, str) and self.value == other
3161+
3162+
with self.assertRaisesRegex(TypeError, "got multiple values for argument"):
3163+
ast.Name("x", **{FakeStr('id'): 'y'})
3164+
31443165

31453166
@support.cpython_only
31463167
class ModuleStateTests(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix three crashes when non-string keyword arguments are supplied to objects
2+
in the :mod:`ast` module.

Parser/asdl_c.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def visitModule(self, mod):
939939
}
940940
if (p == 0) {
941941
PyErr_Format(PyExc_TypeError,
942-
"%.400s got multiple values for argument '%U'",
942+
"%.400s got multiple values for argument %R",
943943
Py_TYPE(self)->tp_name, key);
944944
res = -1;
945945
goto cleanup;
@@ -962,7 +962,7 @@ def visitModule(self, mod):
962962
else if (contains == 0) {
963963
if (PyErr_WarnFormat(
964964
PyExc_DeprecationWarning, 1,
965-
"%.400s.__init__ got an unexpected keyword argument '%U'. "
965+
"%.400s.__init__ got an unexpected keyword argument %R. "
966966
"Support for arbitrary keyword arguments is deprecated "
967967
"and will be removed in Python 3.15.",
968968
Py_TYPE(self)->tp_name, key

Python/Python-ast.c

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)