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/ast_opt.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/ast_opt.c')
-rw-r--r-- | Python/ast_opt.c | 61 |
1 files changed, 10 insertions, 51 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c index 5e57638..1f9cb77 100644 --- a/Python/ast_opt.c +++ b/Python/ast_opt.c @@ -5,47 +5,6 @@ #include "ast.h" -/* TODO: is_const and get_const_value are copied from Python/compile.c. - It should be deduped in the future. Maybe, we can include this file - from compile.c? -*/ -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(); - } -} - static int make_const(expr_ty node, PyObject *val, PyArena *arena) { @@ -81,7 +40,7 @@ fold_unaryop(expr_ty node, PyArena *arena, int optimize) { expr_ty arg = node->v.UnaryOp.operand; - if (!is_const(arg)) { + if (arg->kind != Constant_kind) { /* Fold not into comparison */ if (node->v.UnaryOp.op == Not && arg->kind == Compare_kind && asdl_seq_LEN(arg->v.Compare.ops) == 1) { @@ -123,7 +82,7 @@ fold_unaryop(expr_ty node, PyArena *arena, int optimize) [UAdd] = PyNumber_Positive, [USub] = PyNumber_Negative, }; - PyObject *newval = ops[node->v.UnaryOp.op](get_const_value(arg)); + PyObject *newval = ops[node->v.UnaryOp.op](arg->v.Constant.value); return make_const(node, newval, arena); } @@ -259,12 +218,12 @@ fold_binop(expr_ty node, PyArena *arena, int optimize) expr_ty lhs, rhs; lhs = node->v.BinOp.left; rhs = node->v.BinOp.right; - if (!is_const(lhs) || !is_const(rhs)) { + if (lhs->kind != Constant_kind || rhs->kind != Constant_kind) { return 1; } - PyObject *lv = get_const_value(lhs); - PyObject *rv = get_const_value(rhs); + PyObject *lv = lhs->v.Constant.value; + PyObject *rv = rhs->v.Constant.value; PyObject *newval; switch (node->v.BinOp.op) { @@ -316,7 +275,7 @@ make_const_tuple(asdl_seq *elts) { for (int i = 0; i < asdl_seq_LEN(elts); i++) { expr_ty e = (expr_ty)asdl_seq_GET(elts, i); - if (!is_const(e)) { + if (e->kind != Constant_kind) { return NULL; } } @@ -328,7 +287,7 @@ make_const_tuple(asdl_seq *elts) for (int i = 0; i < asdl_seq_LEN(elts); i++) { expr_ty e = (expr_ty)asdl_seq_GET(elts, i); - PyObject *v = get_const_value(e); + PyObject *v = e->v.Constant.value; Py_INCREF(v); PyTuple_SET_ITEM(newval, i, v); } @@ -357,16 +316,16 @@ fold_subscr(expr_ty node, PyArena *arena, int optimize) arg = node->v.Subscript.value; slice = node->v.Subscript.slice; if (node->v.Subscript.ctx != Load || - !is_const(arg) || + arg->kind != Constant_kind || /* TODO: handle other types of slices */ slice->kind != Index_kind || - !is_const(slice->v.Index.value)) + slice->v.Index.value->kind != Constant_kind) { return 1; } idx = slice->v.Index.value; - newval = PyObject_GetItem(get_const_value(arg), get_const_value(idx)); + newval = PyObject_GetItem(arg->v.Constant.value, idx->v.Constant.value); return make_const(node, newval, arena); } |