diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-06-06 17:04:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-06 17:04:47 (GMT) |
commit | 83a9ba442662c2a030b45955f3dd24ff4b24bb61 (patch) | |
tree | 022cecd992bd8af026d0ada3ca8142187db420fc /Python | |
parent | f7ed4d4e83f5d9e85e244a1cbc460f26436ab24d (diff) | |
download | cpython-83a9ba442662c2a030b45955f3dd24ff4b24bb61.zip cpython-83a9ba442662c2a030b45955f3dd24ff4b24bb61.tar.gz cpython-83a9ba442662c2a030b45955f3dd24ff4b24bb61.tar.bz2 |
bpo-40870: Invalidate usage of some constants with ast.Name (GH-20649)
(cherry picked from commit 68874a8502da440a1dc4746cf73262648b870aee)
Co-authored-by: Batuhan Taskaya <isidentical@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ast.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Python/ast.c b/Python/ast.c index 594879b..0a999fc 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -22,6 +22,25 @@ 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) { Py_ssize_t i; @@ -199,6 +218,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: |