Skip to content

Commit 7ae9a47

Browse files
miss-islingtonJelleZijlstravstinner
authored
[3.14] gh-144169: Fix three crashes in AST objects with non-str kwargs (GH-144178) (#144227)
gh-144169: Fix three crashes in AST objects with non-str kwargs (GH-144178) (cherry picked from commit 639c1ad) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent ca6142d commit 7ae9a47

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

Lib/test/test_ast/test_ast.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,13 @@ def test_replace_reject_unknown_instance_fields(self):
14191419
self.assertIs(node.ctx, context)
14201420
self.assertRaises(AttributeError, getattr, node, 'unknown')
14211421

1422+
def test_replace_non_str_kwarg(self):
1423+
node = ast.Name(id="x")
1424+
errmsg = "got an unexpected keyword argument <object object"
1425+
with self.assertRaisesRegex(TypeError, errmsg):
1426+
node.__replace__(**{object(): "y"})
1427+
1428+
14221429
class ASTHelpers_Test(unittest.TestCase):
14231430
maxDiff = None
14241431

@@ -3281,6 +3288,27 @@ class _AllFieldTypes(ast.AST):
32813288
self.assertIs(obj.a, None)
32823289
self.assertEqual(obj.b, [])
32833290

3291+
def test_non_str_kwarg(self):
3292+
warn_msg = "got an unexpected keyword argument <object object"
3293+
with (
3294+
self.assertRaises(TypeError),
3295+
self.assertWarnsRegex(DeprecationWarning, warn_msg),
3296+
):
3297+
ast.Name(**{object(): 'y'})
3298+
3299+
class FakeStr:
3300+
def __init__(self, value):
3301+
self.value = value
3302+
3303+
def __hash__(self):
3304+
return hash(self.value)
3305+
3306+
def __eq__(self, other):
3307+
return isinstance(other, str) and self.value == other
3308+
3309+
with self.assertRaisesRegex(TypeError, "got multiple values for argument"):
3310+
ast.Name("x", **{FakeStr('id'): 'y'})
3311+
32843312

32853313
@support.cpython_only
32863314
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def visitModule(self, mod):
942942
}
943943
if (p == 0) {
944944
PyErr_Format(PyExc_TypeError,
945-
"%.400s got multiple values for argument '%U'",
945+
"%.400s got multiple values for argument %R",
946946
Py_TYPE(self)->tp_name, key);
947947
res = -1;
948948
goto cleanup;
@@ -965,7 +965,7 @@ def visitModule(self, mod):
965965
else if (contains == 0) {
966966
if (PyErr_WarnFormat(
967967
PyExc_DeprecationWarning, 1,
968-
"%.400s.__init__ got an unexpected keyword argument '%U'. "
968+
"%.400s.__init__ got an unexpected keyword argument %R. "
969969
"Support for arbitrary keyword arguments is deprecated "
970970
"and will be removed in Python 3.15.",
971971
Py_TYPE(self)->tp_name, key
@@ -1207,7 +1207,7 @@ def visitModule(self, mod):
12071207
if (rc == 0) {
12081208
PyErr_Format(PyExc_TypeError,
12091209
"%.400s.__replace__ got an unexpected keyword "
1210-
"argument '%U'.", Py_TYPE(self)->tp_name, key);
1210+
"argument %R.", Py_TYPE(self)->tp_name, key);
12111211
Py_DECREF(expecting);
12121212
return -1;
12131213
}

Python/Python-ast.c

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

0 commit comments

Comments
 (0)