8000 bpo-40000: Improve AST validation for invalid constant nodes by isidentical · Pull Request #19055 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40000: Improve AST validation for invalid constant nodes #19055

New 8000 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 5 commits into from
Mar 19, 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
9 changes: 9 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@ def test_invalid_identifier(self):
compile(m, "<test>", "exec")
self.assertIn("identifier must be of type str", str(cm.exception))

def test_invalid_constant(self):
for invalid_constant in int, (1, 2, int), frozenset((1, 2, int)):
e = ast.Expression(body=ast.Constant(invalid_constant))
ast.fix_missing_locations(e)
with self.assertRaisesRegex(
TypeError, "invalid type in Constant: type"
):
compile(e, "<test>", "eval")

def test_empty_yield_from(self):
# Issue 16546: yield from value is not optional.
empty_yield_from = ast.parse("def f():\n yield from g()")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improved error messages for validation of ``ast.Constant`` nodes. Patch by
Batuhan Taskaya.
8 changes: 5 additions & 3 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ validate_constant(PyObject *value)
return 1;
}

if (!PyErr_Occurred()) {
PyErr_Format(PyExc_TypeError,
"got an invalid type in Constant: %s",
_PyType_Name(Py_TYPE(value)));
}
return 0;
}

Expand Down Expand Up @@ -265,9 +270,6 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
validate_keywords(exp->v.Call.keywords);
case Constant_kind:
if (!validate_constant(exp->v.Constant.value)) {
PyErr_Format(PyExc_TypeError,
"got an invalid type in Constant: %s",
_PyType_Name(Py_TYPE(exp->v.Constant.value)));
return 0;
}
return 1;
Expand Down
0