mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
GH-106008: Make implicit boolean conversions explicit (GH-106003)
This commit is contained in:
parent
6e9f83d9ae
commit
7b2d94d875
20 changed files with 1728 additions and 1152 deletions
|
@ -2790,9 +2790,11 @@ static int compiler_addcompare(struct compiler *c, location loc,
|
|||
default:
|
||||
Py_UNREACHABLE();
|
||||
}
|
||||
/* cmp goes in top bits of the oparg, while the low bits are used by quickened
|
||||
* versions of this opcode to store the comparison mask. */
|
||||
ADDOP_I(c, loc, COMPARE_OP, (cmp << 4) | compare_masks[cmp]);
|
||||
// cmp goes in top three bits of the oparg, while the low four bits are used
|
||||
// by quickened versions of this opcode to store the comparison mask. The
|
||||
// fifth-lowest bit indicates whether the result should be converted to bool
|
||||
// and is set later):
|
||||
ADDOP_I(c, loc, COMPARE_OP, (cmp << 5) | compare_masks[cmp]);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -2858,10 +2860,12 @@ compiler_jump_if(struct compiler *c, location loc,
|
|||
ADDOP_I(c, LOC(e), SWAP, 2);
|
||||
ADDOP_I(c, LOC(e), COPY, 2);
|
||||
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i));
|
||||
ADDOP(c, LOC(e), TO_BOOL);
|
||||
ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
|
||||
}
|
||||
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
|
||||
ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, n));
|
||||
ADDOP(c, LOC(e), TO_BOOL);
|
||||
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
|
||||
NEW_JUMP_TARGET_LABEL(c, end);
|
||||
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
|
||||
|
@ -2885,6 +2889,7 @@ compiler_jump_if(struct compiler *c, location loc,
|
|||
|
||||
/* general implementation */
|
||||
VISIT(c, expr, e);
|
||||
ADDOP(c, LOC(e), TO_BOOL);
|
||||
ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
@ -4016,8 +4021,6 @@ unaryop(unaryop_ty op)
|
|||
switch (op) {
|
||||
case Invert:
|
||||
return UNARY_INVERT;
|
||||
case Not:
|
||||
return UNARY_NOT;
|
||||
case USub:
|
||||
return UNARY_NEGATIVE;
|
||||
default:
|
||||
|
@ -4247,6 +4250,7 @@ compiler_boolop(struct compiler *c, expr_ty e)
|
|||
for (i = 0; i < n; ++i) {
|
||||
VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
|
||||
ADDOP_I(c, loc, COPY, 1);
|
||||
ADDOP(c, loc, TO_BOOL);
|
||||
ADDOP_JUMP(c, loc, jumpi, end);
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
}
|
||||
|
@ -4554,6 +4558,7 @@ compiler_compare(struct compiler *c, expr_ty e)
|
|||
ADDOP_I(c, loc, COPY, 2);
|
||||
ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i));
|
||||
ADDOP_I(c, loc, COPY, 1);
|
||||
ADDOP(c, loc, TO_BOOL);
|
||||
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, cleanup);
|
||||
ADDOP(c, loc, POP_TOP);
|
||||
}
|
||||
|
@ -5789,6 +5794,7 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
|
|||
static int
|
||||
compiler_with_except_finish(struct compiler *c, jump_target_label cleanup) {
|
||||
NEW_JUMP_TARGET_LABEL(c, suppress);
|
||||
ADDOP(c, NO_LOCATION, TO_BOOL);
|
||||
ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress);
|
||||
ADDOP_I(c, NO_LOCATION, RERAISE, 2);
|
||||
|
||||
|
@ -6022,6 +6028,10 @@ compiler_visit_expr1(struct compiler *c, expr_ty e)
|
|||
if (e->v.UnaryOp.op == UAdd) {
|
||||
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_UNARY_POSITIVE);
|
||||
}
|
||||
else if (e->v.UnaryOp.op == Not) {
|
||||
ADDOP(c, loc, TO_BOOL);
|
||||
ADDOP(c, loc, UNARY_NOT);
|
||||
}
|
||||
else {
|
||||
ADDOP(c, loc, unaryop(e->v.UnaryOp.op));
|
||||
}
|
||||
|
@ -7197,6 +7207,7 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
|
|||
}
|
||||
VISIT(c, expr, value);
|
||||
ADDOP_COMPARE(c, LOC(p), Eq);
|
||||
ADDOP(c, LOC(p), TO_BOOL);
|
||||
RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
|
||||
return SUCCESS;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue