Skip to content

[3.8] bpo-36290: Fix keyword collision handling in AST node constructors (GH-12382) #20366

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2020
Merged
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
7 changes: 7 additions & 0 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@ def __instancecheck__(cls, inst):
return type.__instancecheck__(cls, inst)

def _new(cls, *args, **kwargs):
for key in kwargs:
if key not in cls._fields:
# arbitrary keyword arguments are accepted
continue
pos = cls._fields.index(key)
if pos < len(args):
raise TypeError(f"{cls.__name__} got multiple values for argument {key!r}")
if cls in _const_types:
return Constant(*args, **kwargs)
return Constant.__new__(cls, *args, **kwargs)
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,15 @@ def test_classattrs(self):
self.assertRaises(TypeError, ast.Num, 1, None, 2)
self.assertRaises(TypeError, ast.Num, 1, None, 2, lineno=0)

# Arbitrary keyword arguments are supported
self.assertEqual(ast.Constant(1, foo='bar').foo, 'bar')
self.assertEqual(ast.Num(1, foo='bar').foo, 'bar')

with self.assertRaisesRegex(TypeError, "Num got multiple values for argument 'n'"):
ast.Num(1, n=2)
with self.assertRaisesRegex(TypeError, "Constant got multiple values for argument 'value'"):
ast.Constant(1, value=2)

self.assertEqual(ast.Num(42).n, 42)
self.assertEqual(ast.Num(4.25).n, 4.25)
self.assertEqual(ast.Num(4.25j).n, 4.25j)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AST nodes are now raising :exc:`TypeError` on conflicting keyword arguments.
Patch contributed by Rémi Lapeyre.
27 changes: 24 additions & 3 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,9 @@ def visitModule(self, mod):
}
if (fields) {
numfields = PySequence_Size(fields);
if (numfields == -1)
if (numfields == -1) {
goto cleanup;
}
}

res = 0; /* if no error occurs, this stays 0 to the end */
Expand All @@ -687,15 +688,35 @@ def visitModule(self, mod):
}
res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
Py_DECREF(name);
if (res < 0)
if (res < 0) {
goto cleanup;
}
}
if (kw) {
i = 0; /* needed by PyDict_Next */
while (PyDict_Next(kw, &i, &key, &value)) {
int contains = PySequence_Contains(fields, key);
if (contains == -1) {
res = -1;
goto cleanup;
} else if (contains == 1) {
Py_ssize_t p = PySequence_Index(fields, key);
if (p == -1) {
res = -1;
goto cleanup;
}
if (p < PyTuple_GET_SIZE(args)) {
PyErr_Format(PyExc_TypeError,
"%.400s got multiple values for argument '%U'",
Py_TYPE(self)->tp_name, key);
res = -1;
goto cleanup;
}
}
res = PyObject_SetAttr(self, key, value);
if (res < 0)
if (res < 0) {
goto cleanup;
}
}
}
cleanup:
Expand Down
27 changes: 24 additions & 3 deletions Python/Python-ast.c

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