summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-12-30 07:47:42 (GMT)
committerGitHub <noreply@github.com>2017-12-30 07:47:42 (GMT)
commit02b9ef27752ff4873c592ac3afe7e3410f715984 (patch)
tree835631cefca42ab1b676462c01865eed4657510c /Python
parentf111b3dcb414093a4efb9d74b69925e535ddc470 (diff)
downloadcpython-02b9ef27752ff4873c592ac3afe7e3410f715984.zip
cpython-02b9ef27752ff4873c592ac3afe7e3410f715984.tar.gz
cpython-02b9ef27752ff4873c592ac3afe7e3410f715984.tar.bz2
bpo-32439: Clean up the code for compiling comparison expressions. (#5029)
Diffstat (limited to 'Python')
-rw-r--r--Python/compile.c43
1 files changed, 20 insertions, 23 deletions
diff --git a/Python/compile.c b/Python/compile.c
index e5745d1..cd03916 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3420,35 +3420,32 @@ static int
compiler_compare(struct compiler *c, expr_ty e)
{
Py_ssize_t i, n;
- basicblock *cleanup = NULL;
- /* XXX the logic can be cleaned up for 1 or multiple comparisons */
VISIT(c, expr, e->v.Compare.left);
- n = asdl_seq_LEN(e->v.Compare.ops);
- assert(n > 0);
- if (n > 1) {
- cleanup = compiler_new_block(c);
+ assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
+ n = asdl_seq_LEN(e->v.Compare.ops) - 1;
+ if (n == 0) {
+ VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
+ ADDOP_I(c, COMPARE_OP,
+ cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
+ }
+ else {
+ basicblock *cleanup = compiler_new_block(c);
if (cleanup == NULL)
return 0;
- VISIT(c, expr,
- (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
- }
- for (i = 1; i < n; i++) {
- ADDOP(c, DUP_TOP);
- ADDOP(c, ROT_THREE);
- ADDOP_I(c, COMPARE_OP,
- cmpop((cmpop_ty)(asdl_seq_GET(
- e->v.Compare.ops, i - 1))));
- ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
- NEXT_BLOCK(c);
- if (i < (n - 1))
+ for (i = 0; i < n; i++) {
VISIT(c, expr,
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
- }
- VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
- ADDOP_I(c, COMPARE_OP,
- cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
- if (n > 1) {
+ ADDOP(c, DUP_TOP);
+ ADDOP(c, ROT_THREE);
+ ADDOP_I(c, COMPARE_OP,
+ cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
+ ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
+ NEXT_BLOCK(c);
+ }
+ VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
+ ADDOP_I(c, COMPARE_OP,
+ cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
basicblock *end = compiler_new_block(c);
if (end == NULL)
return 0;