mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-40870: Invalidate usage of some constants with ast.Name (GH-20649)
(cherry picked from commit 68874a8502
)
Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
This commit is contained in:
parent
18f1226884
commit
90ee51f1cd
3 changed files with 31 additions and 0 deletions
22
Python/ast.c
22
Python/ast.c
|
@ -21,6 +21,25 @@ static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
|
|||
static int validate_stmt(stmt_ty);
|
||||
static int validate_expr(expr_ty, expr_context_ty);
|
||||
|
||||
static int
|
||||
validate_name(PyObject *name)
|
||||
{
|
||||
assert(PyUnicode_Check(name));
|
||||
static const char * const forbidden[] = {
|
||||
"None",
|
||||
"True",
|
||||
"False",
|
||||
NULL
|
||||
};
|
||||
for (int i = 0; forbidden[i] != NULL; i++) {
|
||||
if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) {
|
||||
PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
validate_comprehension(asdl_seq *gens)
|
||||
{
|
||||
|
@ -173,6 +192,9 @@ validate_expr(expr_ty exp, expr_context_ty ctx)
|
|||
actual_ctx = exp->v.Starred.ctx;
|
||||
break;
|
||||
case Name_kind:
|
||||
if (!validate_name(exp->v.Name.id)) {
|
||||
return 0;
|
||||
}
|
||||
actual_ctx = exp->v.Name.ctx;
|
||||
break;
|
||||
case List_kind:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue