diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-09-27 14:42:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-27 14:42:37 (GMT) |
commit | 3f22811fef73aec848d961593d95fa877f77ecbf (patch) | |
tree | 025ca176b2993e8d85a0961f994794c3f9801032 /Python/compile.c | |
parent | a94ee12c26aa8dd7dce01373779df8055aff765b (diff) | |
download | cpython-3f22811fef73aec848d961593d95fa877f77ecbf.zip cpython-3f22811fef73aec848d961593d95fa877f77ecbf.tar.gz cpython-3f22811fef73aec848d961593d95fa877f77ecbf.tar.bz2 |
bpo-32892: Use ast.Constant instead of specific constant AST types. (GH-9445)
Diffstat (limited to 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 64 |
1 files changed, 6 insertions, 58 deletions
diff --git a/Python/compile.c b/Python/compile.c index 3a45804..096b762 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1398,43 +1398,6 @@ compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) } \ } -static int -is_const(expr_ty e) -{ - switch (e->kind) { - case Constant_kind: - case Num_kind: - case Str_kind: - case Bytes_kind: - case Ellipsis_kind: - case NameConstant_kind: - return 1; - default: - return 0; - } -} - -static PyObject * -get_const_value(expr_ty e) -{ - switch (e->kind) { - case Constant_kind: - return e->v.Constant.value; - case Num_kind: - return e->v.Num.n; - case Str_kind: - return e->v.Str.s; - case Bytes_kind: - return e->v.Bytes.s; - case Ellipsis_kind: - return Py_Ellipsis; - case NameConstant_kind: - return e->v.NameConstant.value; - default: - Py_UNREACHABLE(); - } -} - /* Search if variable annotations are present statically in a block. */ static int @@ -2568,7 +2531,7 @@ static int compiler_return(struct compiler *c, stmt_ty s) { int preserve_tos = ((s->v.Return.value != NULL) && - !is_const(s->v.Return.value)); + (s->v.Return.value->kind != Constant_kind)); if (c->u->u_ste->ste_type != FunctionBlock) return compiler_error(c, "'return' outside function"); if (s->v.Return.value != NULL && @@ -3054,7 +3017,7 @@ compiler_visit_stmt_expr(struct compiler *c, expr_ty value) return 1; } - if (is_const(value)) { + if (value->kind == Constant_kind) { /* ignore constant statement */ return 1; } @@ -3502,7 +3465,7 @@ are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) Py_ssize_t i; for (i = begin; i < end; i++) { expr_ty key = (expr_ty)asdl_seq_GET(seq, i); - if (key == NULL || !is_const(key)) + if (key == NULL || key->kind != Constant_kind) return 0; } return 1; @@ -3522,7 +3485,7 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end return 0; } for (i = begin; i < end; i++) { - key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); + key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; Py_INCREF(key); PyTuple_SET_ITEM(keys, i - begin, key); } @@ -4244,8 +4207,8 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k) static int expr_constant(expr_ty e) { - if (is_const(e)) { - return PyObject_IsTrue(get_const_value(e)); + if (e->kind == Constant_kind) { + return PyObject_IsTrue(e->v.Constant.value); } return -1; } @@ -4505,25 +4468,10 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Constant_kind: ADDOP_LOAD_CONST(c, e->v.Constant.value); break; - case Num_kind: - ADDOP_LOAD_CONST(c, e->v.Num.n); - break; - case Str_kind: - ADDOP_LOAD_CONST(c, e->v.Str.s); - break; case JoinedStr_kind: return compiler_joined_str(c, e); case FormattedValue_kind: return compiler_formatted_value(c, e); - case Bytes_kind: - ADDOP_LOAD_CONST(c, e->v.Bytes.s); - break; - case Ellipsis_kind: - ADDOP_LOAD_CONST(c, Py_Ellipsis); - break; - case NameConstant_kind: - ADDOP_LOAD_CONST(c, e->v.NameConstant.value); - break; /* The following exprs can be assignment targets. */ case Attribute_kind: if (e->v.Attribute.ctx != AugStore) |