From 97d929faaf6294ad6ea96801c1562fffbdabd8cc Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 15:41:18 +0000 Subject: [PATCH 01/93] compiler_setup returns SUCCESS/ERROR --- Python/compile.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index d6ed6941ac1ecd..7a15c2e7483848 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -55,6 +55,19 @@ */ #define STACK_USE_GUIDELINE 30 +#define SUCCESS 0 +#define ERROR -1 + +#define RETURN_IF_ERROR(X) \ + if ((X) == -1) { \ + return -1; \ + } + +#define RETURN_NULL_IF_ERROR(X) \ + if ((X) == -1) { \ + return NULL; \ + } + /* If we exceed this limit, it should * be considered a compiler bug. * Currently it should be impossible @@ -610,18 +623,18 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename, { c->c_const_cache = PyDict_New(); if (!c->c_const_cache) { - return 0; + return ERROR; } c->c_stack = PyList_New(0); if (!c->c_stack) { - return 0; + return ERROR; } c->c_filename = Py_NewRef(filename); c->c_arena = arena; if (!_PyFuture_FromAST(mod, filename, &c->c_future)) { - return 0; + return ERROR; } int merged = c->c_future.ff_features | flags.cf_flags; c->c_future.ff_features = merged; @@ -635,16 +648,16 @@ compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename, state.ff_features = merged; if (!_PyAST_Optimize(mod, arena, &state)) { - return 0; + return ERROR; } c->c_st = _PySymtable_Build(mod, filename, &c->c_future); if (c->c_st == NULL) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_SystemError, "no symtable"); } - return 0; + return ERROR; } - return 1; + return SUCCESS; } static struct compiler* @@ -656,7 +669,7 @@ new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags, if (c == NULL) { return NULL; } - if (!compiler_setup(c, mod, filename, flags, optimize, arena)) { + if (compiler_setup(c, mod, filename, flags, optimize, arena) < 0) { compiler_free(c); return NULL; } From 0e1414158bd7b62132678bbde9952478583d7e79 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 15:56:37 +0000 Subject: [PATCH 02/93] cfg_builder_init returns SUCCESS/ERROR --- Python/compile.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7a15c2e7483848..7ab315400171c3 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -813,11 +813,12 @@ cfg_builder_init(cfg_builder *g) { g->g_block_list = NULL; basicblock *block = cfg_builder_new_block(g); - if (block == NULL) - return 0; + if (block == NULL) { + return ERROR; + } g->g_curblock = g->g_entryblock = block; g->g_current_label = NO_LABEL; - return 1; + return SUCCESS; } static void @@ -1800,7 +1801,7 @@ compiler_enter_scope(struct compiler *c, identifier name, c->c_nestlevel++; cfg_builder *g = CFG_BUILDER(c); - if (!cfg_builder_init(g)) { + if (cfg_builder_init(g) < 0) { return 0; } From 5d81ad09fba57bbd338c1a40877776f4c24d0d29 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:00:36 +0000 Subject: [PATCH 03/93] compiler_set_qualname returns SUCCESS/ERROR --- Python/compile.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7ab315400171c3..1d96131d0ec6ee 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -876,8 +876,10 @@ compiler_set_qualname(struct compiler *c) || u->u_scope_type == COMPILER_SCOPE_CLASS) { assert(u->u_name); mangled = _Py_Mangle(parent->u_private, u->u_name); - if (!mangled) - return 0; + if (!mangled) { + return ERROR; + } + scope = _PyST_GetScope(parent->u_ste, mangled); Py_DECREF(mangled); assert(scope != GLOBAL_IMPLICIT); @@ -893,8 +895,9 @@ compiler_set_qualname(struct compiler *c) _Py_DECLARE_STR(dot_locals, ".<locals>"); base = PyUnicode_Concat(parent->u_qualname, &_Py_STR(dot_locals)); - if (base == NULL) - return 0; + if (base == NULL) { + return ERROR; + } } else { base = Py_NewRef(parent->u_qualname); @@ -906,18 +909,20 @@ compiler_set_qualname(struct compiler *c) _Py_DECLARE_STR(dot, "."); name = PyUnicode_Concat(base, &_Py_STR(dot)); Py_DECREF(base); - if (name == NULL) - return 0; + if (name == NULL) { + return ERROR; + } PyUnicode_Append(&name, u->u_name); - if (name == NULL) - return 0; + if (name == NULL) { + return ERROR; + } } else { name = Py_NewRef(u->u_name); } u->u_qualname = name; - return 1; + return SUCCESS; } static jump_target_label @@ -1809,8 +1814,9 @@ compiler_enter_scope(struct compiler *c, identifier name, loc.lineno = 0; } else { - if (!compiler_set_qualname(c)) + if (compiler_set_qualname(c) < 0){ return 0; + } } ADDOP_I(c, loc, RESUME, 0); From 6d56a1b153d0662075b19967d65db2ef80dd16b7 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:06:25 +0000 Subject: [PATCH 04/93] basicblock_addop returns SUCCESS/ERROR --- Python/compile.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 1d96131d0ec6ee..3b3675d759d2d1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1337,7 +1337,7 @@ basicblock_addop(basicblock *b, int opcode, int oparg, location loc) int off = basicblock_next_instr(b); if (off < 0) { - return 0; + return ERROR; } struct instr *i = &b->b_instr[off]; i->i_opcode = opcode; @@ -1345,7 +1345,7 @@ basicblock_addop(basicblock *b, int opcode, int oparg, location loc) i->i_target = NULL; i->i_loc = loc; - return 1; + return SUCCESS; } static bool @@ -1379,7 +1379,7 @@ cfg_builder_addop(cfg_builder *g, int opcode, int oparg, location loc) if (cfg_builder_maybe_start_new_block(g) != 0) { return -1; } - return basicblock_addop(g->g_curblock, opcode, oparg, loc); + return basicblock_addop(g->g_curblock, opcode, oparg, loc) == SUCCESS ? 1 : 0; } static int From 3b872153b8cb80494b523d5d9dcbef47416df605 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:10:03 +0000 Subject: [PATCH 05/93] cfg_builder_addop returns SUCCESS/ERROR --- Python/compile.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 3b3675d759d2d1..a46868389bfb3f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1379,14 +1379,14 @@ cfg_builder_addop(cfg_builder *g, int opcode, int oparg, location loc) if (cfg_builder_maybe_start_new_block(g) != 0) { return -1; } - return basicblock_addop(g->g_curblock, opcode, oparg, loc) == SUCCESS ? 1 : 0; + return basicblock_addop(g->g_curblock, opcode, oparg, loc); } static int cfg_builder_addop_noarg(cfg_builder *g, int opcode, location loc) { assert(!HAS_ARG(opcode)); - return cfg_builder_addop(g, opcode, 0, loc); + return cfg_builder_addop(g, opcode, 0, loc) == SUCCESS ? 1 : 0; } static Py_ssize_t @@ -1595,7 +1595,7 @@ cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, location loc) EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); - return cfg_builder_addop(g, opcode, oparg_, loc); + return cfg_builder_addop(g, opcode, oparg_, loc) == SUCCESS ? 1 : 0; } static int @@ -1604,7 +1604,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, { assert(IS_LABEL(target)); assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode)); - return cfg_builder_addop(g, opcode, target.id, loc); + return cfg_builder_addop(g, opcode, target.id, loc) == SUCCESS ? 1 : 0; } @@ -10012,7 +10012,7 @@ instructions_to_cfg(PyObject *instructions, cfg_builder *g) if (PyErr_Occurred()) { return -1; } - if (!cfg_builder_addop(g, opcode, oparg, loc)) { + if (cfg_builder_addop(g, opcode, oparg, loc) < 0) { return -1; } } From 829ed2dd47c26ebd4bd5734496de4900830563b3 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:12:55 +0000 Subject: [PATCH 06/93] cfg_builder_addop_noarg returns SUCCESS/ERROR --- Python/compile.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index a46868389bfb3f..de19082689afab 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1386,7 +1386,7 @@ static int cfg_builder_addop_noarg(cfg_builder *g, int opcode, location loc) { assert(!HAS_ARG(opcode)); - return cfg_builder_addop(g, opcode, 0, loc) == SUCCESS ? 1 : 0; + return cfg_builder_addop(g, opcode, 0, loc); } static Py_ssize_t @@ -1609,12 +1609,12 @@ cfg_builder_addop_j(cfg_builder *g, location loc, #define ADDOP(C, LOC, OP) { \ - if (!cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC))) \ + if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) \ return 0; \ } #define ADDOP_IN_SCOPE(C, LOC, OP) { \ - if (!cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC))) { \ + if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) { \ compiler_exit_scope(c); \ return 0; \ } \ @@ -6396,7 +6396,7 @@ emit_and_reset_fail_pop(struct compiler *c, location loc, } while (--pc->fail_pop_size) { USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]); - if (!cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, loc)) { + if (cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, loc) < 0) { pc->fail_pop_size = 0; PyObject_Free(pc->fail_pop); pc->fail_pop = NULL; @@ -6908,7 +6908,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) // Need to NULL this for the PyObject_Free call in the error block. old_pc.fail_pop = NULL; // No match. Pop the remaining copy of the subject and fail: - if (!cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) || + if ((cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) < 0) || !jump_to_fail_pop(c, LOC(p), pc, JUMP)) { goto error; } From 8be8f0b2511f4588040591cd8da59eabc43d44f9 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:16:25 +0000 Subject: [PATCH 07/93] cfg_builder_addop_i returns SUCCESS/ERROR --- Python/compile.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index de19082689afab..bdddc38865ed02 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1541,9 +1541,10 @@ static int compiler_addop_load_const(struct compiler *c, location loc, PyObject *o) { Py_ssize_t arg = compiler_add_const(c, o); - if (arg < 0) + if (arg < 0) { return 0; - return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc); + } + return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc) == SUCCESS ? 1 : 0; } static int @@ -1553,7 +1554,7 @@ compiler_addop_o(struct compiler *c, location loc, Py_ssize_t arg = dict_add_o(dict, o); if (arg < 0) return 0; - return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc); + return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc) == SUCCESS ? 1 : 0; } static int @@ -1577,7 +1578,7 @@ compiler_addop_name(struct compiler *c, location loc, arg <<= 1; arg |= 1; } - return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc); + return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc) == SUCCESS ? 1 : 0; } /* Add an opcode with an integer argument. @@ -1595,7 +1596,7 @@ cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, location loc) EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); - return cfg_builder_addop(g, opcode, oparg_, loc) == SUCCESS ? 1 : 0; + return cfg_builder_addop(g, opcode, oparg_, loc); } static int @@ -1653,7 +1654,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_I(C, LOC, OP, O) { \ - if (!cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC))) \ + if (cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC)) < 0) \ return 0; \ } @@ -4343,7 +4344,7 @@ compiler_nameop(struct compiler *c, location loc, if (op == LOAD_GLOBAL) { arg <<= 1; } - return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, loc); + return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, loc) == SUCCESS ? 1 : 0; } static int @@ -6833,7 +6834,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) pc->fail_pop = NULL; pc->fail_pop_size = 0; pc->on_top = 0; - if (!cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, LOC(alt)) || + if ((cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, LOC(alt)) < 0) || !compiler_pattern(c, alt, pc)) { goto error; } From 9374ac03d65176521b8755a388b3d272f6ef8974 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:19:56 +0000 Subject: [PATCH 08/93] compiler_addop_name returns SUCCESS/ERROR --- Python/compile.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index bdddc38865ed02..7b671667855839 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1564,12 +1564,14 @@ compiler_addop_name(struct compiler *c, location loc, Py_ssize_t arg; PyObject *mangled = _Py_Mangle(c->u->u_private, o); - if (!mangled) - return 0; + if (!mangled) { + return ERROR; + } arg = dict_add_o(dict, mangled); Py_DECREF(mangled); - if (arg < 0) - return 0; + if (arg < 0) { + return ERROR; + } if (opcode == LOAD_ATTR) { arg <<= 1; } @@ -1578,12 +1580,10 @@ compiler_addop_name(struct compiler *c, location loc, arg <<= 1; arg |= 1; } - return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc) == SUCCESS ? 1 : 0; + return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc); } -/* Add an opcode with an integer argument. - Returns 0 on failure, 1 on success. -*/ +/* Add an opcode with an integer argument */ static int cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, location loc) { @@ -1649,7 +1649,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_NAME(C, LOC, OP, O, TYPE) { \ - if (!compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O))) \ + if (compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) \ return 0; \ } From 9d21d49e17541fbf2f2c60b3dd13c54581e67ca0 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:23:29 +0000 Subject: [PATCH 09/93] compiler_addop_o and compiler_addop_load_const returns SUCCESS/ERROR --- Python/compile.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7b671667855839..f46c2d23eff049 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1542,9 +1542,9 @@ compiler_addop_load_const(struct compiler *c, location loc, PyObject *o) { Py_ssize_t arg = compiler_add_const(c, o); if (arg < 0) { - return 0; + return ERROR; } - return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc) == SUCCESS ? 1 : 0; + return cfg_builder_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc); } static int @@ -1552,9 +1552,10 @@ compiler_addop_o(struct compiler *c, location loc, int opcode, PyObject *dict, PyObject *o) { Py_ssize_t arg = dict_add_o(dict, o); - if (arg < 0) - return 0; - return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc) == SUCCESS ? 1 : 0; + if (arg < 0) { + return ERROR; + } + return cfg_builder_addop_i(CFG_BUILDER(c), opcode, arg, loc); } static int @@ -1622,7 +1623,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_LOAD_CONST(C, LOC, O) { \ - if (!compiler_addop_load_const((C), (LOC), (O))) \ + if (compiler_addop_load_const((C), (LOC), (O)) < 0) \ return 0; \ } @@ -1632,7 +1633,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, if (__new_const == NULL) { \ return 0; \ } \ - if (!compiler_addop_load_const((C), (LOC), __new_const)) { \ + if (compiler_addop_load_const((C), (LOC), __new_const) < 0) { \ Py_DECREF(__new_const); \ return 0; \ } \ @@ -1641,7 +1642,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, #define ADDOP_N(C, LOC, OP, O, TYPE) { \ assert(!HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \ - if (!compiler_addop_o((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O))) { \ + if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) { \ Py_DECREF((O)); \ return 0; \ } \ From a67c6128757da39e178869bd928df3893fff2fd8 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 16:43:36 +0000 Subject: [PATCH 10/93] cfg_builder_addop_j returns SUCCESS/ERROR --- Python/compile.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index f46c2d23eff049..4ab3f07526111c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1606,7 +1606,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, { assert(IS_LABEL(target)); assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode)); - return cfg_builder_addop(g, opcode, target.id, loc) == SUCCESS ? 1 : 0; + return cfg_builder_addop(g, opcode, target.id, loc); } @@ -1660,7 +1660,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_JUMP(C, LOC, OP, O) { \ - if (!cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O))) \ + if (cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O)) < 0) \ return 0; \ } @@ -6898,7 +6898,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) } } assert(control); - if (!cfg_builder_addop_j(CFG_BUILDER(c), LOC(alt), JUMP, end) || + if ((cfg_builder_addop_j(CFG_BUILDER(c), LOC(alt), JUMP, end) < 0) || !emit_and_reset_fail_pop(c, LOC(alt), pc)) { goto error; From 844b78fb8b8b088d2df4dc898794b3bb3a486ac0 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:16:55 +0000 Subject: [PATCH 11/93] prepend macros with _ --- Python/compile.c | 1176 +++++++++++++++++++++++----------------------- 1 file changed, 588 insertions(+), 588 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 4ab3f07526111c..27efd2740aede5 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1610,25 +1610,25 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } -#define ADDOP(C, LOC, OP) { \ +#define _ADDOP(C, LOC, OP) { \ if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) \ return 0; \ } -#define ADDOP_IN_SCOPE(C, LOC, OP) { \ +#define _ADDOP_IN_SCOPE(C, LOC, OP) { \ if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) { \ compiler_exit_scope(c); \ return 0; \ } \ } -#define ADDOP_LOAD_CONST(C, LOC, O) { \ +#define _ADDOP_LOAD_CONST(C, LOC, O) { \ if (compiler_addop_load_const((C), (LOC), (O)) < 0) \ return 0; \ } -/* Same as ADDOP_LOAD_CONST, but steals a reference. */ -#define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \ +/* Same as _ADDOP_LOAD_CONST, but steals a reference. */ +#define _ADDOP_LOAD_CONST_NEW(C, LOC, O) { \ PyObject *__new_const = (O); \ if (__new_const == NULL) { \ return 0; \ @@ -1640,8 +1640,8 @@ cfg_builder_addop_j(cfg_builder *g, location loc, Py_DECREF(__new_const); \ } -#define ADDOP_N(C, LOC, OP, O, TYPE) { \ - assert(!HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \ +#define _ADDOP_N(C, LOC, OP, O, TYPE) { \ + assert(!HAS_CONST(OP)); /* use _ADDOP_LOAD_CONST_NEW */ \ if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) { \ Py_DECREF((O)); \ return 0; \ @@ -1649,58 +1649,58 @@ cfg_builder_addop_j(cfg_builder *g, location loc, Py_DECREF((O)); \ } -#define ADDOP_NAME(C, LOC, OP, O, TYPE) { \ +#define _ADDOP_NAME(C, LOC, OP, O, TYPE) { \ if (compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) \ return 0; \ } -#define ADDOP_I(C, LOC, OP, O) { \ +#define _ADDOP_I(C, LOC, OP, O) { \ if (cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC)) < 0) \ return 0; \ } -#define ADDOP_JUMP(C, LOC, OP, O) { \ +#define _ADDOP_JUMP(C, LOC, OP, O) { \ if (cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O)) < 0) \ return 0; \ } -#define ADDOP_COMPARE(C, LOC, CMP) { \ +#define _ADDOP_COMPARE(C, LOC, CMP) { \ if (!compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))) \ return 0; \ } -#define ADDOP_BINARY(C, LOC, BINOP) \ +#define _ADDOP_BINARY(C, LOC, BINOP) \ RETURN_IF_FALSE(addop_binary((C), (LOC), (BINOP), false)) -#define ADDOP_INPLACE(C, LOC, BINOP) \ +#define _ADDOP_INPLACE(C, LOC, BINOP) \ RETURN_IF_FALSE(addop_binary((C), (LOC), (BINOP), true)) -#define ADD_YIELD_FROM(C, LOC, await) \ +#define _ADD_YIELD_FROM(C, LOC, await) \ RETURN_IF_FALSE(compiler_add_yield_from((C), (LOC), (await))) -#define POP_EXCEPT_AND_RERAISE(C, LOC) \ +#define _POP_EXCEPT_AND_RERAISE(C, LOC) \ RETURN_IF_FALSE(compiler_pop_except_and_reraise((C), (LOC))) -#define ADDOP_YIELD(C, LOC) \ +#define _ADDOP_YIELD(C, LOC) \ RETURN_IF_FALSE(addop_yield((C), (LOC))) -/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use +/* _VISIT and _VISIT_SEQ takes an ASDL type as their second argument. They use the ASDL name to synthesize the name of the C type and the visit function. */ -#define VISIT(C, TYPE, V) {\ +#define _VISIT(C, TYPE, V) {\ if (!compiler_visit_ ## TYPE((C), (V))) \ return 0; \ } -#define VISIT_IN_SCOPE(C, TYPE, V) {\ +#define _VISIT_IN_SCOPE(C, TYPE, V) {\ if (!compiler_visit_ ## TYPE((C), (V))) { \ compiler_exit_scope(c); \ return 0; \ } \ } -#define VISIT_SEQ(C, TYPE, SEQ) { \ +#define _VISIT_SEQ(C, TYPE, SEQ) { \ int _i; \ asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ @@ -1710,7 +1710,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } \ } -#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ +#define _VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ int _i; \ asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ @@ -1820,7 +1820,7 @@ compiler_enter_scope(struct compiler *c, identifier name, return 0; } } - ADDOP_I(c, loc, RESUME, 0); + _ADDOP_I(c, loc, RESUME, 0); if (u->u_scope_type == COMPILER_SCOPE_MODULE) { loc.lineno = -1; @@ -1960,10 +1960,10 @@ compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label blo static int compiler_call_exit_with_nones(struct compiler *c, location loc) { - ADDOP_LOAD_CONST(c, loc, Py_None); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADDOP_I(c, loc, CALL, 2); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADDOP_I(c, loc, CALL, 2); return 1; } @@ -1975,17 +1975,17 @@ compiler_add_yield_from(struct compiler *c, location loc, int await) NEW_JUMP_TARGET_LABEL(c, exit); USE_LABEL(c, send); - ADDOP_JUMP(c, loc, SEND, exit); + _ADDOP_JUMP(c, loc, SEND, exit); // Set up a virtual try/except to handle when StopIteration is raised during // a close or throw call. The only way YIELD_VALUE raises if they do! - ADDOP_JUMP(c, loc, SETUP_FINALLY, fail); - ADDOP_I(c, loc, YIELD_VALUE, 0); - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP_I(c, loc, RESUME, await ? 3 : 2); - ADDOP_JUMP(c, loc, JUMP_NO_INTERRUPT, send); + _ADDOP_JUMP(c, loc, SETUP_FINALLY, fail); + _ADDOP_I(c, loc, YIELD_VALUE, 0); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP_I(c, loc, RESUME, await ? 3 : 2); + _ADDOP_JUMP(c, loc, JUMP_NO_INTERRUPT, send); USE_LABEL(c, fail); - ADDOP(c, loc, CLEANUP_THROW); + _ADDOP(c, loc, CLEANUP_THROW); USE_LABEL(c, exit); return 1; @@ -2001,9 +2001,9 @@ compiler_pop_except_and_reraise(struct compiler *c, location loc) * (exception_unwind clears the stack) */ - ADDOP_I(c, loc, COPY, 3); - ADDOP(c, loc, POP_EXCEPT); - ADDOP_I(c, loc, RERAISE, 1); + _ADDOP_I(c, loc, COPY, 3); + _ADDOP(c, loc, POP_EXCEPT); + _ADDOP_I(c, loc, RERAISE, 1); return 1; } @@ -2026,25 +2026,25 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, case FOR_LOOP: /* Pop the iterator */ if (preserve_tos) { - ADDOP_I(c, *ploc, SWAP, 2); + _ADDOP_I(c, *ploc, SWAP, 2); } - ADDOP(c, *ploc, POP_TOP); + _ADDOP(c, *ploc, POP_TOP); return 1; case TRY_EXCEPT: - ADDOP(c, *ploc, POP_BLOCK); + _ADDOP(c, *ploc, POP_BLOCK); return 1; case FINALLY_TRY: /* This POP_BLOCK gets the line number of the unwinding statement */ - ADDOP(c, *ploc, POP_BLOCK); + _ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { if (!compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL)) { return 0; } } /* Emit the finally block */ - VISIT_SEQ(c, stmt, info->fb_datum); + _VISIT_SEQ(c, stmt, info->fb_datum); if (preserve_tos) { compiler_pop_fblock(c, POP_VALUE, NO_LABEL); } @@ -2056,32 +2056,32 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, case FINALLY_END: if (preserve_tos) { - ADDOP_I(c, *ploc, SWAP, 2); + _ADDOP_I(c, *ploc, SWAP, 2); } - ADDOP(c, *ploc, POP_TOP); /* exc_value */ + _ADDOP(c, *ploc, POP_TOP); /* exc_value */ if (preserve_tos) { - ADDOP_I(c, *ploc, SWAP, 2); + _ADDOP_I(c, *ploc, SWAP, 2); } - ADDOP(c, *ploc, POP_BLOCK); - ADDOP(c, *ploc, POP_EXCEPT); + _ADDOP(c, *ploc, POP_BLOCK); + _ADDOP(c, *ploc, POP_EXCEPT); return 1; case WITH: case ASYNC_WITH: *ploc = LOC((stmt_ty)info->fb_datum); - ADDOP(c, *ploc, POP_BLOCK); + _ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { - ADDOP_I(c, *ploc, SWAP, 2); + _ADDOP_I(c, *ploc, SWAP, 2); } if(!compiler_call_exit_with_nones(c, *ploc)) { return 0; } if (info->fb_type == ASYNC_WITH) { - ADDOP_I(c, *ploc, GET_AWAITABLE, 2); - ADDOP_LOAD_CONST(c, *ploc, Py_None); - ADD_YIELD_FROM(c, *ploc, 1); + _ADDOP_I(c, *ploc, GET_AWAITABLE, 2); + _ADDOP_LOAD_CONST(c, *ploc, Py_None); + _ADD_YIELD_FROM(c, *ploc, 1); } - ADDOP(c, *ploc, POP_TOP); + _ADDOP(c, *ploc, POP_TOP); /* The exit block should appear to execute after the * statement causing the unwinding, so make the unwinding * instruction artificial */ @@ -2090,15 +2090,15 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, case HANDLER_CLEANUP: { if (info->fb_datum) { - ADDOP(c, *ploc, POP_BLOCK); + _ADDOP(c, *ploc, POP_BLOCK); } if (preserve_tos) { - ADDOP_I(c, *ploc, SWAP, 2); + _ADDOP_I(c, *ploc, SWAP, 2); } - ADDOP(c, *ploc, POP_BLOCK); - ADDOP(c, *ploc, POP_EXCEPT); + _ADDOP(c, *ploc, POP_BLOCK); + _ADDOP(c, *ploc, POP_EXCEPT); if (info->fb_datum) { - ADDOP_LOAD_CONST(c, *ploc, Py_None); + _ADDOP_LOAD_CONST(c, *ploc, Py_None); compiler_nameop(c, *ploc, info->fb_datum, Store); compiler_nameop(c, *ploc, info->fb_datum, Del); } @@ -2106,9 +2106,9 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, } case POP_VALUE: { if (preserve_tos) { - ADDOP_I(c, *ploc, SWAP, 2); + _ADDOP_I(c, *ploc, SWAP, 2); } - ADDOP(c, *ploc, POP_TOP); + _ADDOP(c, *ploc, POP_TOP); return 1; } } @@ -2165,7 +2165,7 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts) } /* Every annotated class and module should have __annotations__. */ if (find_ann(stmts)) { - ADDOP(c, loc, SETUP_ANNOTATIONS); + _ADDOP(c, loc, SETUP_ANNOTATIONS); } if (!asdl_seq_LEN(stmts)) return 1; @@ -2176,13 +2176,13 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts) i = 1; st = (stmt_ty)asdl_seq_GET(stmts, 0); assert(st->kind == Expr_kind); - VISIT(c, expr, st->v.Expr.value); + _VISIT(c, expr, st->v.Expr.value); if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store)) return 0; } } for (; i < asdl_seq_LEN(stmts); i++) - VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); + _VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); return 1; } @@ -2204,13 +2204,13 @@ compiler_codegen(struct compiler *c, mod_ty mod) break; case Interactive_kind: if (find_ann(mod->v.Interactive.body)) { - ADDOP(c, loc, SETUP_ANNOTATIONS); + _ADDOP(c, loc, SETUP_ANNOTATIONS); } c->c_interactive = 1; - VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); + _VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); break; case Expression_kind: - VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); + _VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); break; default: PyErr_Format(PyExc_SystemError, @@ -2317,13 +2317,13 @@ compiler_make_closure(struct compiler *c, location loc, Py_DECREF(freevars); return 0; } - ADDOP_I(c, loc, LOAD_CLOSURE, arg); + _ADDOP_I(c, loc, LOAD_CLOSURE, arg); } flags |= 0x08; - ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars); + _ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars); } - ADDOP_LOAD_CONST(c, loc, (PyObject*)co); - ADDOP_I(c, loc, MAKE_FUNCTION, flags); + _ADDOP_LOAD_CONST(c, loc, (PyObject*)co); + _ADDOP_I(c, loc, MAKE_FUNCTION, flags); return 1; } @@ -2336,7 +2336,7 @@ compiler_decorators(struct compiler *c, asdl_expr_seq* decos) return 1; for (i = 0; i < asdl_seq_LEN(decos); i++) { - VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); } return 1; } @@ -2349,7 +2349,7 @@ compiler_apply_decorators(struct compiler *c, asdl_expr_seq* decos) for (Py_ssize_t i = asdl_seq_LEN(decos) - 1; i > -1; i--) { location loc = LOC((expr_ty)asdl_seq_GET(decos, i)); - ADDOP_I(c, loc, CALL, 0); + _ADDOP_I(c, loc, CALL, 0); } return 1; } @@ -2397,8 +2397,8 @@ compiler_visit_kwonlydefaults(struct compiler *c, location loc, Py_ssize_t default_count = PyList_GET_SIZE(keys); PyObject *keys_tuple = PyList_AsTuple(keys); Py_DECREF(keys); - ADDOP_LOAD_CONST_NEW(c, loc, keys_tuple); - ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, default_count); + _ADDOP_LOAD_CONST_NEW(c, loc, keys_tuple); + _ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, default_count); assert(default_count > 0); return 1; } @@ -2415,7 +2415,7 @@ static int compiler_visit_annexpr(struct compiler *c, expr_ty annotation) { location loc = LOC(annotation); - ADDOP_LOAD_CONST_NEW(c, loc, _PyAST_ExprAsUnicode(annotation)); + _ADDOP_LOAD_CONST_NEW(c, loc, _PyAST_ExprAsUnicode(annotation)); return 1; } @@ -2430,11 +2430,11 @@ compiler_visit_argannotation(struct compiler *c, identifier id, if (!mangled) { return 0; } - ADDOP_LOAD_CONST(c, loc, mangled); + _ADDOP_LOAD_CONST(c, loc, mangled); Py_DECREF(mangled); if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) { - VISIT(c, annexpr, annotation); + _VISIT(c, annexpr, annotation); } else { if (annotation->kind == Starred_kind) { @@ -2442,11 +2442,11 @@ compiler_visit_argannotation(struct compiler *c, identifier id, // Do [annotation_value] = [*Ts]. // (Note that in theory we could end up here even for an argument // other than *args, but in practice the grammar doesn't allow it.) - VISIT(c, expr, annotation->v.Starred.value); - ADDOP_I(c, loc, UNPACK_SEQUENCE, (Py_ssize_t) 1); + _VISIT(c, expr, annotation->v.Starred.value); + _ADDOP_I(c, loc, UNPACK_SEQUENCE, (Py_ssize_t) 1); } else { - VISIT(c, expr, annotation); + _VISIT(c, expr, annotation); } } *annotations_len += 2; @@ -2503,7 +2503,7 @@ compiler_visit_annotations(struct compiler *c, location loc, } if (annotations_len) { - ADDOP_I(c, loc, BUILD_TUPLE, annotations_len); + _ADDOP_I(c, loc, BUILD_TUPLE, annotations_len); return 1; } @@ -2514,8 +2514,8 @@ static int compiler_visit_defaults(struct compiler *c, arguments_ty args, location loc) { - VISIT_SEQ(c, expr, args->defaults); - ADDOP_I(c, loc, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); + _VISIT_SEQ(c, expr, args->defaults); + _ADDOP_I(c, loc, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); return 1; } @@ -2624,11 +2624,11 @@ wrap_in_stopiteration_handler(struct compiler *c) return 0; } - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - ADDOP(c, NO_LOCATION, RETURN_VALUE); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP(c, NO_LOCATION, RETURN_VALUE); USE_LABEL(c, handler); - ADDOP(c, NO_LOCATION, STOPITERATION_ERROR); - ADDOP_I(c, NO_LOCATION, RERAISE, 1); + _ADDOP(c, NO_LOCATION, STOPITERATION_ERROR); + _ADDOP_I(c, NO_LOCATION, RERAISE, 1); return 1; } @@ -2710,7 +2710,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { - VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); + _VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); } if (c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator) { if (!wrap_in_stopiteration_handler(c)) { @@ -2785,7 +2785,7 @@ compiler_class(struct compiler *c, stmt_ty s) return 0; } assert(c->u->u_qualname); - ADDOP_LOAD_CONST(c, loc, c->u->u_qualname); + _ADDOP_LOAD_CONST(c, loc, c->u->u_qualname); if (!compiler_nameop(c, loc, &_Py_ID(__qualname__), Store)) { compiler_exit_scope(c); return 0; @@ -2805,8 +2805,8 @@ compiler_class(struct compiler *c, stmt_ty s) return 0; } assert(i == 0); - ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i); - ADDOP_I(c, NO_LOCATION, COPY, 1); + _ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i); + _ADDOP_I(c, NO_LOCATION, COPY, 1); if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store)) { compiler_exit_scope(c); return 0; @@ -2815,9 +2815,9 @@ compiler_class(struct compiler *c, stmt_ty s) else { /* No methods referenced __class__, so just return None */ assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); } - ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE); + _ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE); /* create the code object */ co = assemble(c, 1); } @@ -2828,8 +2828,8 @@ compiler_class(struct compiler *c, stmt_ty s) location loc = LOC(s); /* 2. load the 'build_class' function */ - ADDOP(c, loc, PUSH_NULL); - ADDOP(c, loc, LOAD_BUILD_CLASS); + _ADDOP(c, loc, PUSH_NULL); + _ADDOP(c, loc, LOAD_BUILD_CLASS); /* 3. load a function (or closure) made from the code object */ if (!compiler_make_closure(c, loc, co, 0, NULL)) { @@ -2839,7 +2839,7 @@ compiler_class(struct compiler *c, stmt_ty s) Py_DECREF(co); /* 4. load class name */ - ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name); + _ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name); /* 5. generate the rest of the code for the call */ if (!compiler_call_helper(c, loc, 2, @@ -2921,21 +2921,21 @@ static int compiler_addcompare(struct compiler *c, location loc, cmp = Py_GE; break; case Is: - ADDOP_I(c, loc, IS_OP, 0); + _ADDOP_I(c, loc, IS_OP, 0); return 1; case IsNot: - ADDOP_I(c, loc, IS_OP, 1); + _ADDOP_I(c, loc, IS_OP, 1); return 1; case In: - ADDOP_I(c, loc, CONTAINS_OP, 0); + _ADDOP_I(c, loc, CONTAINS_OP, 0); return 1; case NotIn: - ADDOP_I(c, loc, CONTAINS_OP, 1); + _ADDOP_I(c, loc, CONTAINS_OP, 1); return 1; default: Py_UNREACHABLE(); } - ADDOP_I(c, loc, COMPARE_OP, cmp); + _ADDOP_I(c, loc, COMPARE_OP, cmp); return 1; } @@ -2984,7 +2984,7 @@ compiler_jump_if(struct compiler *c, location loc, if (!compiler_jump_if(c, loc, e->v.IfExp.body, next, cond)) { return 0; } - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, next2); if (!compiler_jump_if(c, loc, e->v.IfExp.orelse, next, cond)) { @@ -3001,25 +3001,25 @@ compiler_jump_if(struct compiler *c, location loc, return 0; } NEW_JUMP_TARGET_LABEL(c, cleanup); - VISIT(c, expr, e->v.Compare.left); + _VISIT(c, expr, e->v.Compare.left); for (Py_ssize_t i = 0; i < n; i++) { - VISIT(c, expr, + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); - 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_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_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); + _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_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_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); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, cleanup); - ADDOP(c, LOC(e), POP_TOP); + _ADDOP(c, LOC(e), POP_TOP); if (!cond) { - ADDOP_JUMP(c, NO_LOCATION, JUMP, next); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, next); } USE_LABEL(c, end); @@ -3034,8 +3034,8 @@ compiler_jump_if(struct compiler *c, location loc, } /* general implementation */ - VISIT(c, expr, e); - ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); + _VISIT(c, expr, e); + _ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); return 1; } @@ -3049,11 +3049,11 @@ compiler_ifexp(struct compiler *c, expr_ty e) if (!compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0)) { return 0; } - VISIT(c, expr, e->v.IfExp.body); - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _VISIT(c, expr, e->v.IfExp.body); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, next); - VISIT(c, expr, e->v.IfExp.orelse); + _VISIT(c, expr, e->v.IfExp.orelse); USE_LABEL(c, end); return 1; @@ -3090,13 +3090,13 @@ compiler_lambda(struct compiler *c, expr_ty e) c->u->u_argcount = asdl_seq_LEN(args->args); c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); - VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); + _VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); if (c->u->u_ste->ste_generator) { co = assemble(c, 0); } else { location loc = LOCATION(e->lineno, e->lineno, 0, 0); - ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); + _ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); co = assemble(c, 1); } qualname = Py_NewRef(c->u->u_qualname); @@ -3133,12 +3133,12 @@ compiler_if(struct compiler *c, stmt_ty s) if (!compiler_jump_if(c, LOC(s), s->v.If.test, next, 0)) { return 0; } - VISIT_SEQ(c, stmt, s->v.If.body); + _VISIT_SEQ(c, stmt, s->v.If.body); if (asdl_seq_LEN(s->v.If.orelse)) { - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, next); - VISIT_SEQ(c, stmt, s->v.If.orelse); + _VISIT_SEQ(c, stmt, s->v.If.orelse); } USE_LABEL(c, end); @@ -3157,24 +3157,24 @@ compiler_for(struct compiler *c, stmt_ty s) if (!compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL)) { return 0; } - VISIT(c, expr, s->v.For.iter); - ADDOP(c, loc, GET_ITER); + _VISIT(c, expr, s->v.For.iter); + _ADDOP(c, loc, GET_ITER); USE_LABEL(c, start); - ADDOP_JUMP(c, loc, FOR_ITER, cleanup); + _ADDOP_JUMP(c, loc, FOR_ITER, cleanup); USE_LABEL(c, body); - VISIT(c, expr, s->v.For.target); - VISIT_SEQ(c, stmt, s->v.For.body); + _VISIT(c, expr, s->v.For.target); + _VISIT_SEQ(c, stmt, s->v.For.body); /* Mark jump as artificial */ - ADDOP_JUMP(c, NO_LOCATION, JUMP, start); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, start); USE_LABEL(c, cleanup); - ADDOP(c, NO_LOCATION, END_FOR); + _ADDOP(c, NO_LOCATION, END_FOR); compiler_pop_fblock(c, FOR_LOOP, start); - VISIT_SEQ(c, stmt, s->v.For.orelse); + _VISIT_SEQ(c, stmt, s->v.For.orelse); USE_LABEL(c, end); return 1; @@ -3195,25 +3195,25 @@ compiler_async_for(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, except); NEW_JUMP_TARGET_LABEL(c, end); - VISIT(c, expr, s->v.AsyncFor.iter); - ADDOP(c, loc, GET_AITER); + _VISIT(c, expr, s->v.AsyncFor.iter); + _ADDOP(c, loc, GET_AITER); USE_LABEL(c, start); if (!compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL)) { return 0; } /* SETUP_FINALLY to guard the __anext__ call */ - ADDOP_JUMP(c, loc, SETUP_FINALLY, except); - ADDOP(c, loc, GET_ANEXT); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); - ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ + _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + _ADDOP(c, loc, GET_ANEXT); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); + _ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ /* Success block for __anext__ */ - VISIT(c, expr, s->v.AsyncFor.target); - VISIT_SEQ(c, stmt, s->v.AsyncFor.body); + _VISIT(c, expr, s->v.AsyncFor.target); + _VISIT_SEQ(c, stmt, s->v.AsyncFor.body); /* Mark jump as artificial */ - ADDOP_JUMP(c, NO_LOCATION, JUMP, start); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, start); compiler_pop_fblock(c, FOR_LOOP, start); @@ -3223,10 +3223,10 @@ compiler_async_for(struct compiler *c, stmt_ty s) /* Use same line number as the iterator, * as the END_ASYNC_FOR succeeds the `for`, not the body. */ loc = LOC(s->v.AsyncFor.iter); - ADDOP(c, loc, END_ASYNC_FOR); + _ADDOP(c, loc, END_ASYNC_FOR); /* `else` block */ - VISIT_SEQ(c, stmt, s->v.For.orelse); + _VISIT_SEQ(c, stmt, s->v.For.orelse); USE_LABEL(c, end); return 1; @@ -3249,7 +3249,7 @@ compiler_while(struct compiler *c, stmt_ty s) } USE_LABEL(c, body); - VISIT_SEQ(c, stmt, s->v.While.body); + _VISIT_SEQ(c, stmt, s->v.While.body); if (!compiler_jump_if(c, LOC(s), s->v.While.test, body, 1)) { return 0; } @@ -3258,7 +3258,7 @@ compiler_while(struct compiler *c, stmt_ty s) USE_LABEL(c, anchor); if (s->v.While.orelse) { - VISIT_SEQ(c, stmt, s->v.While.orelse); + _VISIT_SEQ(c, stmt, s->v.While.orelse); } USE_LABEL(c, end); @@ -3281,28 +3281,28 @@ compiler_return(struct compiler *c, stmt_ty s) } if (preserve_tos) { - VISIT(c, expr, s->v.Return.value); + _VISIT(c, expr, s->v.Return.value); } else { /* Emit instruction with line number for return value */ if (s->v.Return.value != NULL) { loc = LOC(s->v.Return.value); - ADDOP(c, loc, NOP); + _ADDOP(c, loc, NOP); } } if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) { loc = LOC(s); - ADDOP(c, loc, NOP); + _ADDOP(c, loc, NOP); } if (!compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL)) return 0; if (s->v.Return.value == NULL) { - ADDOP_LOAD_CONST(c, loc, Py_None); + _ADDOP_LOAD_CONST(c, loc, Py_None); } else if (!preserve_tos) { - ADDOP_LOAD_CONST(c, loc, s->v.Return.value->v.Constant.value); + _ADDOP_LOAD_CONST(c, loc, s->v.Return.value->v.Constant.value); } - ADDOP(c, loc, RETURN_VALUE); + _ADDOP(c, loc, RETURN_VALUE); return 1; } @@ -3312,7 +3312,7 @@ compiler_break(struct compiler *c, location loc) { struct fblockinfo *loop = NULL; /* Emit instruction with line number */ - ADDOP(c, loc, NOP); + _ADDOP(c, loc, NOP); if (!compiler_unwind_fblock_stack(c, &loc, 0, &loop)) { return 0; } @@ -3322,7 +3322,7 @@ compiler_break(struct compiler *c, location loc) if (!compiler_unwind_fblock(c, &loc, loop, 0)) { return 0; } - ADDOP_JUMP(c, loc, JUMP, loop->fb_exit); + _ADDOP_JUMP(c, loc, JUMP, loop->fb_exit); return 1; } @@ -3331,14 +3331,14 @@ compiler_continue(struct compiler *c, location loc) { struct fblockinfo *loop = NULL; /* Emit instruction with line number */ - ADDOP(c, loc, NOP); + _ADDOP(c, loc, NOP); if (!compiler_unwind_fblock_stack(c, &loc, 0, &loop)) { return 0; } if (loop == NULL) { return compiler_error(c, loc, "'continue' not properly in loop"); } - ADDOP_JUMP(c, loc, JUMP, loop->fb_block); + _ADDOP_JUMP(c, loc, JUMP, loop->fb_block); return 1; } @@ -3395,7 +3395,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup); /* `try` block */ - ADDOP_JUMP(c, loc, SETUP_FINALLY, end); + _ADDOP_JUMP(c, loc, SETUP_FINALLY, end); USE_LABEL(c, body); if (!compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.Try.finalbody)) @@ -3405,30 +3405,30 @@ compiler_try_finally(struct compiler *c, stmt_ty s) return 0; } else { - VISIT_SEQ(c, stmt, s->v.Try.body); + _VISIT_SEQ(c, stmt, s->v.Try.body); } - ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_BLOCK); compiler_pop_fblock(c, FINALLY_TRY, body); - VISIT_SEQ(c, stmt, s->v.Try.finalbody); + _VISIT_SEQ(c, stmt, s->v.Try.finalbody); - ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); /* `finally` block */ USE_LABEL(c, end); loc = NO_LOCATION; - ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - ADDOP(c, loc, PUSH_EXC_INFO); + _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + _ADDOP(c, loc, PUSH_EXC_INFO); if (!compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL)) return 0; - VISIT_SEQ(c, stmt, s->v.Try.finalbody); + _VISIT_SEQ(c, stmt, s->v.Try.finalbody); loc = location_of_last_executing_statement(s->v.Try.finalbody); compiler_pop_fblock(c, FINALLY_END, end); - ADDOP_I(c, loc, RERAISE, 0); + _ADDOP_I(c, loc, RERAISE, 0); USE_LABEL(c, cleanup); - POP_EXCEPT_AND_RERAISE(c, loc); + _POP_EXCEPT_AND_RERAISE(c, loc); USE_LABEL(c, exit); return 1; @@ -3444,7 +3444,7 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, exit); NEW_JUMP_TARGET_LABEL(c, cleanup); /* `try` block */ - ADDOP_JUMP(c, loc, SETUP_FINALLY, end); + _ADDOP_JUMP(c, loc, SETUP_FINALLY, end); USE_LABEL(c, body); if (!compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.TryStar.finalbody)) { @@ -3456,31 +3456,31 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s) } } else { - VISIT_SEQ(c, stmt, s->v.TryStar.body); + _VISIT_SEQ(c, stmt, s->v.TryStar.body); } - ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_BLOCK); compiler_pop_fblock(c, FINALLY_TRY, body); - VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); + _VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); - ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); /* `finally` block */ USE_LABEL(c, end); loc = NO_LOCATION; - ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - ADDOP(c, loc, PUSH_EXC_INFO); + _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + _ADDOP(c, loc, PUSH_EXC_INFO); if (!compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL)) { return 0; } - VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); + _VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); loc = location_of_last_executing_statement(s->v.Try.finalbody); compiler_pop_fblock(c, FINALLY_END, end); - ADDOP_I(c, loc, RERAISE, 0); + _ADDOP_I(c, loc, RERAISE, 0); USE_LABEL(c, cleanup); - POP_EXCEPT_AND_RERAISE(c, loc); + _POP_EXCEPT_AND_RERAISE(c, loc); USE_LABEL(c, exit); return 1; @@ -3526,24 +3526,24 @@ compiler_try_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, end); NEW_JUMP_TARGET_LABEL(c, cleanup); - ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); USE_LABEL(c, body); if (!compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL)) return 0; - VISIT_SEQ(c, stmt, s->v.Try.body); + _VISIT_SEQ(c, stmt, s->v.Try.body); compiler_pop_fblock(c, TRY_EXCEPT, body); - ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_BLOCK); if (s->v.Try.orelse && asdl_seq_LEN(s->v.Try.orelse)) { - VISIT_SEQ(c, stmt, s->v.Try.orelse); + _VISIT_SEQ(c, stmt, s->v.Try.orelse); } - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); n = asdl_seq_LEN(s->v.Try.handlers); USE_LABEL(c, except); - ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); - ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); + _ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); + _ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); /* Runtime will push a block here, so we need to account for that */ if (!compiler_push_fblock(c, loc, EXCEPTION_HANDLER, NO_LABEL, NO_LABEL, NULL)) return 0; @@ -3557,9 +3557,9 @@ compiler_try_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, next_except); except = next_except; if (handler->v.ExceptHandler.type) { - VISIT(c, expr, handler->v.ExceptHandler.type); - ADDOP(c, loc, CHECK_EXC_MATCH); - ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, except); + _VISIT(c, expr, handler->v.ExceptHandler.type); + _ADDOP(c, loc, CHECK_EXC_MATCH); + _ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, except); } if (handler->v.ExceptHandler.name) { NEW_JUMP_TARGET_LABEL(c, cleanup_end); @@ -3579,7 +3579,7 @@ compiler_try_except(struct compiler *c, stmt_ty s) */ /* second try: */ - ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); + _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); USE_LABEL(c, cleanup_body); if (!compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, @@ -3588,50 +3588,50 @@ compiler_try_except(struct compiler *c, stmt_ty s) } /* second # body */ - VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); + _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); /* name = None; del name; # Mark as artificial */ - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP(c, NO_LOCATION, POP_EXCEPT); - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_EXCEPT); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); /* except: */ USE_LABEL(c, cleanup_end); /* name = None; del name; # artificial */ - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); - ADDOP_I(c, NO_LOCATION, RERAISE, 1); + _ADDOP_I(c, NO_LOCATION, RERAISE, 1); } else { NEW_JUMP_TARGET_LABEL(c, cleanup_body); - ADDOP(c, loc, POP_TOP); /* exc_value */ + _ADDOP(c, loc, POP_TOP); /* exc_value */ USE_LABEL(c, cleanup_body); if (!compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, NULL)) return 0; - VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); + _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP(c, NO_LOCATION, POP_EXCEPT); - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_EXCEPT); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); } USE_LABEL(c, except); } /* artificial */ compiler_pop_fblock(c, EXCEPTION_HANDLER, NO_LABEL); - ADDOP_I(c, NO_LOCATION, RERAISE, 0); + _ADDOP_I(c, NO_LOCATION, RERAISE, 0); USE_LABEL(c, cleanup); - POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); + _POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); USE_LABEL(c, end); return 1; @@ -3698,22 +3698,22 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup); NEW_JUMP_TARGET_LABEL(c, reraise_star); - ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); USE_LABEL(c, body); if (!compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL)) { return 0; } - VISIT_SEQ(c, stmt, s->v.TryStar.body); + _VISIT_SEQ(c, stmt, s->v.TryStar.body); compiler_pop_fblock(c, TRY_EXCEPT, body); - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP_JUMP(c, NO_LOCATION, JUMP, orelse); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, orelse); Py_ssize_t n = asdl_seq_LEN(s->v.TryStar.handlers); USE_LABEL(c, except); - ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); - ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); + _ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); + _ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); /* Runtime will push a block here, so we need to account for that */ if (!compiler_push_fblock(c, loc, EXCEPTION_GROUP_HANDLER, NO_LABEL, NO_LABEL, "except handler")) { @@ -3732,7 +3732,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) [exc] COPY 1 [orig, exc] */ - ADDOP_I(c, loc, COPY, 1); + _ADDOP_I(c, loc, COPY, 1); /* create empty list for exceptions raised/reraise in the except* blocks */ /* @@ -3740,16 +3740,16 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) [orig, exc, []] SWAP 2 [orig, [], exc] */ - ADDOP_I(c, loc, BUILD_LIST, 0); - ADDOP_I(c, loc, SWAP, 2); + _ADDOP_I(c, loc, BUILD_LIST, 0); + _ADDOP_I(c, loc, SWAP, 2); } if (handler->v.ExceptHandler.type) { - VISIT(c, expr, handler->v.ExceptHandler.type); - ADDOP(c, loc, CHECK_EG_MATCH); - ADDOP_I(c, loc, COPY, 1); - ADDOP_JUMP(c, loc, POP_JUMP_IF_NOT_NONE, handle_match); - ADDOP(c, loc, POP_TOP); // match - ADDOP_JUMP(c, loc, JUMP, except); + _VISIT(c, expr, handler->v.ExceptHandler.type); + _ADDOP(c, loc, CHECK_EG_MATCH); + _ADDOP_I(c, loc, COPY, 1); + _ADDOP_JUMP(c, loc, POP_JUMP_IF_NOT_NONE, handle_match); + _ADDOP(c, loc, POP_TOP); // match + _ADDOP_JUMP(c, loc, JUMP, except); } USE_LABEL(c, handle_match); @@ -3761,7 +3761,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store); } else { - ADDOP(c, loc, POP_TOP); // match + _ADDOP(c, loc, POP_TOP); // match } /* @@ -3775,7 +3775,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) del name */ /* second try: */ - ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); + _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); USE_LABEL(c, cleanup_body); if (!compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, handler->v.ExceptHandler.name)) { @@ -3783,38 +3783,38 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) } /* second # body */ - VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); + _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); /* name = None; del name; # artificial */ - ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_BLOCK); if (handler->v.ExceptHandler.name) { - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); } - ADDOP_JUMP(c, NO_LOCATION, JUMP, except); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, except); /* except: */ USE_LABEL(c, cleanup_end); /* name = None; del name; # artificial */ if (handler->v.ExceptHandler.name) { - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); } /* add exception raised to the res list */ - ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc - ADDOP(c, NO_LOCATION, POP_TOP); // lasti - ADDOP_JUMP(c, NO_LOCATION, JUMP, except); + _ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc + _ADDOP(c, NO_LOCATION, POP_TOP); // lasti + _ADDOP_JUMP(c, NO_LOCATION, JUMP, except); USE_LABEL(c, except); if (i == n - 1) { /* Add exc to the list (if not None it's the unhandled part of the EG) */ - ADDOP_I(c, NO_LOCATION, LIST_APPEND, 1); - ADDOP_JUMP(c, NO_LOCATION, JUMP, reraise_star); + _ADDOP_I(c, NO_LOCATION, LIST_APPEND, 1); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, reraise_star); } } /* artificial */ @@ -3822,27 +3822,27 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, reraise); USE_LABEL(c, reraise_star); - ADDOP(c, NO_LOCATION, PREP_RERAISE_STAR); - ADDOP_I(c, NO_LOCATION, COPY, 1); - ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise); + _ADDOP(c, NO_LOCATION, PREP_RERAISE_STAR); + _ADDOP_I(c, NO_LOCATION, COPY, 1); + _ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise); /* Nothing to reraise */ - ADDOP(c, NO_LOCATION, POP_TOP); - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP(c, NO_LOCATION, POP_EXCEPT); - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP(c, NO_LOCATION, POP_TOP); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_EXCEPT); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, reraise); - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP_I(c, NO_LOCATION, SWAP, 2); - ADDOP(c, NO_LOCATION, POP_EXCEPT); - ADDOP_I(c, NO_LOCATION, RERAISE, 0); + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP_I(c, NO_LOCATION, SWAP, 2); + _ADDOP(c, NO_LOCATION, POP_EXCEPT); + _ADDOP_I(c, NO_LOCATION, RERAISE, 0); USE_LABEL(c, cleanup); - POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); + _POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); USE_LABEL(c, orelse); - VISIT_SEQ(c, stmt, s->v.TryStar.orelse); + _VISIT_SEQ(c, stmt, s->v.TryStar.orelse); USE_LABEL(c, end); return 1; @@ -3892,17 +3892,17 @@ compiler_import_as(struct compiler *c, location loc, attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); if (!attr) return 0; - ADDOP_N(c, loc, IMPORT_FROM, attr, names); + _ADDOP_N(c, loc, IMPORT_FROM, attr, names); if (dot == -1) { break; } - ADDOP_I(c, loc, SWAP, 2); - ADDOP(c, loc, POP_TOP); + _ADDOP_I(c, loc, SWAP, 2); + _ADDOP(c, loc, POP_TOP); } if (!compiler_nameop(c, loc, asname, Store)) { return 0; } - ADDOP(c, loc, POP_TOP); + _ADDOP(c, loc, POP_TOP); return 1; } return compiler_nameop(c, loc, asname, Store); @@ -3926,9 +3926,9 @@ compiler_import(struct compiler *c, stmt_ty s) alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); int r; - ADDOP_LOAD_CONST(c, loc, zero); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADDOP_NAME(c, loc, IMPORT_NAME, alias->name, names); + _ADDOP_LOAD_CONST(c, loc, zero); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADDOP_NAME(c, loc, IMPORT_NAME, alias->name, names); if (alias->asname) { r = compiler_import_as(c, loc, alias->name, alias->asname); @@ -3960,7 +3960,7 @@ compiler_from_import(struct compiler *c, stmt_ty s) { Py_ssize_t n = asdl_seq_LEN(s->v.ImportFrom.names); - ADDOP_LOAD_CONST_NEW(c, LOC(s), PyLong_FromLong(s->v.ImportFrom.level)); + _ADDOP_LOAD_CONST_NEW(c, LOC(s), PyLong_FromLong(s->v.ImportFrom.level)); PyObject *names = PyTuple_New(n); if (!names) { @@ -3981,14 +3981,14 @@ compiler_from_import(struct compiler *c, stmt_ty s) return compiler_error(c, LOC(s), "from __future__ imports must occur " "at the beginning of the file"); } - ADDOP_LOAD_CONST_NEW(c, LOC(s), names); + _ADDOP_LOAD_CONST_NEW(c, LOC(s), names); if (s->v.ImportFrom.module) { - ADDOP_NAME(c, LOC(s), IMPORT_NAME, s->v.ImportFrom.module, names); + _ADDOP_NAME(c, LOC(s), IMPORT_NAME, s->v.ImportFrom.module, names); } else { _Py_DECLARE_STR(empty, ""); - ADDOP_NAME(c, LOC(s), IMPORT_NAME, &_Py_STR(empty), names); + _ADDOP_NAME(c, LOC(s), IMPORT_NAME, &_Py_STR(empty), names); } for (Py_ssize_t i = 0; i < n; i++) { alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); @@ -3996,11 +3996,11 @@ compiler_from_import(struct compiler *c, stmt_ty s) if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { assert(n == 1); - ADDOP(c, LOC(s), IMPORT_STAR); + _ADDOP(c, LOC(s), IMPORT_STAR); return 1; } - ADDOP_NAME(c, LOC(s), IMPORT_FROM, alias->name, names); + _ADDOP_NAME(c, LOC(s), IMPORT_FROM, alias->name, names); store_name = alias->name; if (alias->asname) { store_name = alias->asname; @@ -4011,7 +4011,7 @@ compiler_from_import(struct compiler *c, stmt_ty s) } } /* remove imported module */ - ADDOP(c, LOC(s), POP_TOP); + _ADDOP(c, LOC(s), POP_TOP); return 1; } @@ -4038,12 +4038,12 @@ compiler_assert(struct compiler *c, stmt_ty s) if (!compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1)) { return 0; } - ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR); + _ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR); if (s->v.Assert.msg) { - VISIT(c, expr, s->v.Assert.msg); - ADDOP_I(c, LOC(s), CALL, 0); + _VISIT(c, expr, s->v.Assert.msg); + _ADDOP_I(c, LOC(s), CALL, 0); } - ADDOP_I(c, LOC(s), RAISE_VARARGS, 1); + _ADDOP_I(c, LOC(s), RAISE_VARARGS, 1); USE_LABEL(c, end); return 1; @@ -4053,19 +4053,19 @@ static int compiler_stmt_expr(struct compiler *c, location loc, expr_ty value) { if (c->c_interactive && c->c_nestlevel <= 1) { - VISIT(c, expr, value); - ADDOP(c, loc, PRINT_EXPR); + _VISIT(c, expr, value); + _ADDOP(c, loc, PRINT_EXPR); return 1; } if (value->kind == Constant_kind) { /* ignore constant statement */ - ADDOP(c, loc, NOP); + _ADDOP(c, loc, NOP); return 1; } - VISIT(c, expr, value); - ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */ + _VISIT(c, expr, value); + _ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */ return 1; } @@ -4081,17 +4081,17 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case Return_kind: return compiler_return(c, s); case Delete_kind: - VISIT_SEQ(c, expr, s->v.Delete.targets) + _VISIT_SEQ(c, expr, s->v.Delete.targets) break; case Assign_kind: { Py_ssize_t n = asdl_seq_LEN(s->v.Assign.targets); - VISIT(c, expr, s->v.Assign.value); + _VISIT(c, expr, s->v.Assign.value); for (Py_ssize_t i = 0; i < n; i++) { if (i < n - 1) { - ADDOP_I(c, LOC(s), COPY, 1); + _ADDOP_I(c, LOC(s), COPY, 1); } - VISIT(c, expr, + _VISIT(c, expr, (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); } break; @@ -4112,14 +4112,14 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) { Py_ssize_t n = 0; if (s->v.Raise.exc) { - VISIT(c, expr, s->v.Raise.exc); + _VISIT(c, expr, s->v.Raise.exc); n++; if (s->v.Raise.cause) { - VISIT(c, expr, s->v.Raise.cause); + _VISIT(c, expr, s->v.Raise.cause); n++; } } - ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n); + _ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n); break; } case Try_kind: @@ -4141,7 +4141,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) } case Pass_kind: { - ADDOP(c, LOC(s), NOP); + _ADDOP(c, LOC(s), NOP); break; } case Break_kind: @@ -4234,7 +4234,7 @@ addop_binary(struct compiler *c, location loc, operator_ty binop, inplace ? "inplace" : "binary", binop); return 0; } - ADDOP_I(c, loc, BINARY_OP, oparg); + _ADDOP_I(c, loc, BINARY_OP, oparg); return 1; } @@ -4242,10 +4242,10 @@ addop_binary(struct compiler *c, location loc, operator_ty binop, static int addop_yield(struct compiler *c, location loc) { if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) { - ADDOP(c, loc, ASYNC_GEN_WRAP); + _ADDOP(c, loc, ASYNC_GEN_WRAP); } - ADDOP_I(c, loc, YIELD_VALUE, 0); - ADDOP_I(c, loc, RESUME, 1); + _ADDOP_I(c, loc, YIELD_VALUE, 0); + _ADDOP_I(c, loc, RESUME, 1); return 1; } @@ -4318,7 +4318,7 @@ compiler_nameop(struct compiler *c, location loc, case Store: op = STORE_FAST; break; case Del: op = DELETE_FAST; break; } - ADDOP_N(c, loc, op, mangled, varnames); + _ADDOP_N(c, loc, op, mangled, varnames); return 1; case OP_GLOBAL: switch (ctx) { @@ -4366,13 +4366,13 @@ compiler_boolop(struct compiler *c, expr_ty e) n = asdl_seq_LEN(s) - 1; assert(n >= 0); for (i = 0; i < n; ++i) { - VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); - ADDOP_JUMP(c, loc, jumpi, end); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); + _ADDOP_JUMP(c, loc, jumpi, end); NEW_JUMP_TARGET_LABEL(c, next); USE_LABEL(c, next); } - VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); USE_LABEL(c, end); return 1; @@ -4395,7 +4395,7 @@ starunpack_helper(struct compiler *c, location loc, PyTuple_SET_ITEM(folded, i, Py_NewRef(val)); } if (tuple && !pushed) { - ADDOP_LOAD_CONST_NEW(c, loc, folded); + _ADDOP_LOAD_CONST_NEW(c, loc, folded); } else { if (add == SET_ADD) { Py_SETREF(folded, PyFrozenSet_New(folded)); @@ -4403,11 +4403,11 @@ starunpack_helper(struct compiler *c, location loc, return 0; } } - ADDOP_I(c, loc, build, pushed); - ADDOP_LOAD_CONST_NEW(c, loc, folded); - ADDOP_I(c, loc, extend, 1); + _ADDOP_I(c, loc, build, pushed); + _ADDOP_LOAD_CONST_NEW(c, loc, folded); + _ADDOP_I(c, loc, extend, 1); if (tuple) { - ADDOP(c, loc, LIST_TO_TUPLE); + _ADDOP(c, loc, LIST_TO_TUPLE); } } return 1; @@ -4425,40 +4425,40 @@ starunpack_helper(struct compiler *c, location loc, if (!seen_star && !big) { for (Py_ssize_t i = 0; i < n; i++) { expr_ty elt = asdl_seq_GET(elts, i); - VISIT(c, expr, elt); + _VISIT(c, expr, elt); } if (tuple) { - ADDOP_I(c, loc, BUILD_TUPLE, n+pushed); + _ADDOP_I(c, loc, BUILD_TUPLE, n+pushed); } else { - ADDOP_I(c, loc, build, n+pushed); + _ADDOP_I(c, loc, build, n+pushed); } return 1; } int sequence_built = 0; if (big) { - ADDOP_I(c, loc, build, pushed); + _ADDOP_I(c, loc, build, pushed); sequence_built = 1; } for (Py_ssize_t i = 0; i < n; i++) { expr_ty elt = asdl_seq_GET(elts, i); if (elt->kind == Starred_kind) { if (sequence_built == 0) { - ADDOP_I(c, loc, build, i+pushed); + _ADDOP_I(c, loc, build, i+pushed); sequence_built = 1; } - VISIT(c, expr, elt->v.Starred.value); - ADDOP_I(c, loc, extend, 1); + _VISIT(c, expr, elt->v.Starred.value); + _ADDOP_I(c, loc, extend, 1); } else { - VISIT(c, expr, elt); + _VISIT(c, expr, elt); if (sequence_built) { - ADDOP_I(c, loc, add, 1); + _ADDOP_I(c, loc, add, 1); } } } assert(sequence_built); if (tuple) { - ADDOP(c, loc, LIST_TO_TUPLE); + _ADDOP(c, loc, LIST_TO_TUPLE); } return 1; } @@ -4476,7 +4476,7 @@ unpack_helper(struct compiler *c, location loc, asdl_expr_seq *elts) return compiler_error(c, loc, "too many expressions in " "star-unpacking assignment"); - ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); + _ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); seen_star = 1; } else if (elt->kind == Starred_kind) { @@ -4485,7 +4485,7 @@ unpack_helper(struct compiler *c, location loc, asdl_expr_seq *elts) } } if (!seen_star) { - ADDOP_I(c, loc, UNPACK_SEQUENCE, n); + _ADDOP_I(c, loc, UNPACK_SEQUENCE, n); } return 1; } @@ -4497,7 +4497,7 @@ assignment_helper(struct compiler *c, location loc, asdl_expr_seq *elts) RETURN_IF_FALSE(unpack_helper(c, loc, elts)); for (Py_ssize_t i = 0; i < n; i++) { expr_ty elt = asdl_seq_GET(elts, i); - VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); + _VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); } return 1; } @@ -4515,7 +4515,7 @@ compiler_list(struct compiler *c, expr_ty e) BUILD_LIST, LIST_APPEND, LIST_EXTEND, 0); } else - VISIT_SEQ(c, expr, elts); + _VISIT_SEQ(c, expr, elts); return 1; } @@ -4532,7 +4532,7 @@ compiler_tuple(struct compiler *c, expr_ty e) BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1); } else - VISIT_SEQ(c, expr, elts); + _VISIT_SEQ(c, expr, elts); return 1; } @@ -4565,7 +4565,7 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end location loc = LOC(e); if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) { for (i = begin; i < end; i++) { - VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); } keys = PyTuple_New(n); if (keys == NULL) { @@ -4575,22 +4575,22 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key)); } - ADDOP_LOAD_CONST_NEW(c, loc, keys); - ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); + _ADDOP_LOAD_CONST_NEW(c, loc, keys); + _ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); return 1; } if (big) { - ADDOP_I(c, loc, BUILD_MAP, 0); + _ADDOP_I(c, loc, BUILD_MAP, 0); } for (i = begin; i < end; i++) { - VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); - VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); if (big) { - ADDOP_I(c, loc, MAP_ADD, 1); + _ADDOP_I(c, loc, MAP_ADD, 1); } } if (!big) { - ADDOP_I(c, loc, BUILD_MAP, n); + _ADDOP_I(c, loc, BUILD_MAP, n); } return 1; } @@ -4613,17 +4613,17 @@ compiler_dict(struct compiler *c, expr_ty e) return 0; } if (have_dict) { - ADDOP_I(c, loc, DICT_UPDATE, 1); + _ADDOP_I(c, loc, DICT_UPDATE, 1); } have_dict = 1; elements = 0; } if (have_dict == 0) { - ADDOP_I(c, loc, BUILD_MAP, 0); + _ADDOP_I(c, loc, BUILD_MAP, 0); have_dict = 1; } - VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); - ADDOP_I(c, loc, DICT_UPDATE, 1); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); + _ADDOP_I(c, loc, DICT_UPDATE, 1); } else { if (elements*2 > STACK_USE_GUIDELINE) { @@ -4631,7 +4631,7 @@ compiler_dict(struct compiler *c, expr_ty e) return 0; } if (have_dict) { - ADDOP_I(c, loc, DICT_UPDATE, 1); + _ADDOP_I(c, loc, DICT_UPDATE, 1); } have_dict = 1; elements = 0; @@ -4646,12 +4646,12 @@ compiler_dict(struct compiler *c, expr_ty e) return 0; } if (have_dict) { - ADDOP_I(c, loc, DICT_UPDATE, 1); + _ADDOP_I(c, loc, DICT_UPDATE, 1); } have_dict = 1; } if (!have_dict) { - ADDOP_I(c, loc, BUILD_MAP, 0); + _ADDOP_I(c, loc, BUILD_MAP, 0); } return 1; } @@ -4665,31 +4665,31 @@ compiler_compare(struct compiler *c, expr_ty e) if (!check_compare(c, e)) { return 0; } - VISIT(c, expr, e->v.Compare.left); + _VISIT(c, expr, e->v.Compare.left); 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_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, 0)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); + _ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, 0)); } else { NEW_JUMP_TARGET_LABEL(c, cleanup); for (i = 0; i < n; i++) { - VISIT(c, expr, + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); - ADDOP_I(c, loc, SWAP, 2); - ADDOP_I(c, loc, COPY, 2); - ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i)); - ADDOP_JUMP(c, loc, JUMP_IF_FALSE_OR_POP, cleanup); + _ADDOP_I(c, loc, SWAP, 2); + _ADDOP_I(c, loc, COPY, 2); + _ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i)); + _ADDOP_JUMP(c, loc, JUMP_IF_FALSE_OR_POP, cleanup); } - VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); - ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, n)); + _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); + _ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, n)); NEW_JUMP_TARGET_LABEL(c, end); - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, cleanup); - ADDOP_I(c, loc, SWAP, 2); - ADDOP(c, loc, POP_TOP); + _ADDOP_I(c, loc, SWAP, 2); + _ADDOP(c, loc, POP_TOP); USE_LABEL(c, end); } @@ -4900,20 +4900,20 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e) } } /* Alright, we can optimize the code. */ - VISIT(c, expr, meth->v.Attribute.value); + _VISIT(c, expr, meth->v.Attribute.value); location loc = LOC(meth); loc = update_start_location_to_match_attr(c, loc, meth); - ADDOP_NAME(c, loc, LOAD_METHOD, meth->v.Attribute.attr, names); - VISIT_SEQ(c, expr, e->v.Call.args); + _ADDOP_NAME(c, loc, LOAD_METHOD, meth->v.Attribute.attr, names); + _VISIT_SEQ(c, expr, e->v.Call.args); if (kwdsl) { - VISIT_SEQ(c, keyword, kwds); + _VISIT_SEQ(c, keyword, kwds); if (!compiler_call_simple_kw_helper(c, loc, kwds, kwdsl)) { return 0; }; } loc = update_start_location_to_match_attr(c, LOC(e), meth); - ADDOP_I(c, loc, CALL, argsl + kwdsl); + _ADDOP_I(c, loc, CALL, argsl + kwdsl); return 1; } @@ -4955,8 +4955,8 @@ compiler_call(struct compiler *c, expr_ty e) return 0; } location loc = LOC(e->v.Call.func); - ADDOP(c, loc, PUSH_NULL); - VISIT(c, expr, e->v.Call.func); + _ADDOP(c, loc, PUSH_NULL); + _VISIT(c, expr, e->v.Call.func); loc = LOC(e); return compiler_call_helper(c, loc, 0, e->v.Call.args, @@ -4970,19 +4970,19 @@ compiler_joined_str(struct compiler *c, expr_ty e) Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); if (value_count > STACK_USE_GUIDELINE) { _Py_DECLARE_STR(empty, ""); - ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty))); - ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names); - ADDOP_I(c, loc, BUILD_LIST, 0); + _ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty))); + _ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names); + _ADDOP_I(c, loc, BUILD_LIST, 0); for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { - VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); - ADDOP_I(c, loc, LIST_APPEND, 1); + _VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); + _ADDOP_I(c, loc, LIST_APPEND, 1); } - ADDOP_I(c, loc, CALL, 1); + _ADDOP_I(c, loc, CALL, 1); } else { - VISIT_SEQ(c, expr, e->v.JoinedStr.values); + _VISIT_SEQ(c, expr, e->v.JoinedStr.values); if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { - ADDOP_I(c, loc, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); + _ADDOP_I(c, loc, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); } } return 1; @@ -5010,7 +5010,7 @@ compiler_formatted_value(struct compiler *c, expr_ty e) int oparg; /* The expression to be formatted. */ - VISIT(c, expr, e->v.FormattedValue.value); + _VISIT(c, expr, e->v.FormattedValue.value); switch (conversion) { case 's': oparg = FVC_STR; break; @@ -5024,13 +5024,13 @@ compiler_formatted_value(struct compiler *c, expr_ty e) } if (e->v.FormattedValue.format_spec) { /* Evaluate the format spec, and update our opcode arg. */ - VISIT(c, expr, e->v.FormattedValue.format_spec); + _VISIT(c, expr, e->v.FormattedValue.format_spec); oparg |= FVS_HAVE_SPEC; } /* And push our opcode and oparg */ location loc = LOC(e); - ADDOP_I(c, loc, FORMAT_VALUE, oparg); + _ADDOP_I(c, loc, FORMAT_VALUE, oparg); return 1; } @@ -5048,7 +5048,7 @@ compiler_subkwargs(struct compiler *c, location loc, if (n > 1 && !big) { for (i = begin; i < end; i++) { kw = asdl_seq_GET(keywords, i); - VISIT(c, expr, kw->value); + _VISIT(c, expr, kw->value); } keys = PyTuple_New(n); if (keys == NULL) { @@ -5058,23 +5058,23 @@ compiler_subkwargs(struct compiler *c, location loc, key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key)); } - ADDOP_LOAD_CONST_NEW(c, loc, keys); - ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); + _ADDOP_LOAD_CONST_NEW(c, loc, keys); + _ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); return 1; } if (big) { - ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0); + _ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0); } for (i = begin; i < end; i++) { kw = asdl_seq_GET(keywords, i); - ADDOP_LOAD_CONST(c, loc, kw->arg); - VISIT(c, expr, kw->value); + _ADDOP_LOAD_CONST(c, loc, kw->arg); + _VISIT(c, expr, kw->value); if (big) { - ADDOP_I(c, NO_LOCATION, MAP_ADD, 1); + _ADDOP_I(c, NO_LOCATION, MAP_ADD, 1); } } if (!big) { - ADDOP_I(c, loc, BUILD_MAP, n); + _ADDOP_I(c, loc, BUILD_MAP, n); } return 1; } @@ -5101,7 +5101,7 @@ compiler_call_simple_kw_helper(struct compiler *c, location loc, return 0; } Py_DECREF(names); - ADDOP_I(c, loc, KW_NAMES, arg); + _ADDOP_I(c, loc, KW_NAMES, arg); return 1; } @@ -5142,22 +5142,22 @@ compiler_call_helper(struct compiler *c, location loc, for (i = 0; i < nelts; i++) { expr_ty elt = asdl_seq_GET(args, i); assert(elt->kind != Starred_kind); - VISIT(c, expr, elt); + _VISIT(c, expr, elt); } if (nkwelts) { - VISIT_SEQ(c, keyword, keywords); + _VISIT_SEQ(c, keyword, keywords); if (!compiler_call_simple_kw_helper(c, loc, keywords, nkwelts)) { return 0; }; } - ADDOP_I(c, loc, CALL, n + nelts + nkwelts); + _ADDOP_I(c, loc, CALL, n + nelts + nkwelts); return 1; ex_call: /* Do positional arguments. */ if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { - VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); + _VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); } else if (starunpack_helper(c, loc, args, n, BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1) == 0) { @@ -5178,17 +5178,17 @@ compiler_call_helper(struct compiler *c, location loc, return 0; } if (have_dict) { - ADDOP_I(c, loc, DICT_MERGE, 1); + _ADDOP_I(c, loc, DICT_MERGE, 1); } have_dict = 1; nseen = 0; } if (!have_dict) { - ADDOP_I(c, loc, BUILD_MAP, 0); + _ADDOP_I(c, loc, BUILD_MAP, 0); have_dict = 1; } - VISIT(c, expr, kw->value); - ADDOP_I(c, loc, DICT_MERGE, 1); + _VISIT(c, expr, kw->value); + _ADDOP_I(c, loc, DICT_MERGE, 1); } else { nseen++; @@ -5200,13 +5200,13 @@ compiler_call_helper(struct compiler *c, location loc, return 0; } if (have_dict) { - ADDOP_I(c, loc, DICT_MERGE, 1); + _ADDOP_I(c, loc, DICT_MERGE, 1); } have_dict = 1; } assert(have_dict); } - ADDOP_I(c, loc, CALL_FUNCTION_EX, nkwelts > 0); + _ADDOP_I(c, loc, CALL_FUNCTION_EX, nkwelts > 0); return 1; } @@ -5261,7 +5261,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, if (gen_index == 0) { /* Receive outermost iter as an implicit argument */ c->u->u_argcount = 1; - ADDOP_I(c, loc, LOAD_FAST, 0); + _ADDOP_I(c, loc, LOAD_FAST, 0); } else { /* Sub-iter - calculate on the fly */ @@ -5282,21 +5282,21 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, if (asdl_seq_LEN(elts) == 1) { expr_ty elt = asdl_seq_GET(elts, 0); if (elt->kind != Starred_kind) { - VISIT(c, expr, elt); + _VISIT(c, expr, elt); start = NO_LABEL; } } if (IS_LABEL(start)) { - VISIT(c, expr, gen->iter); - ADDOP(c, loc, GET_ITER); + _VISIT(c, expr, gen->iter); + _ADDOP(c, loc, GET_ITER); } } if (IS_LABEL(start)) { depth++; USE_LABEL(c, start); - ADDOP_JUMP(c, loc, FOR_ITER, anchor); + _ADDOP_JUMP(c, loc, FOR_ITER, anchor); } - VISIT(c, expr, gen->target); + _VISIT(c, expr, gen->target); /* XXX this needs to be cleaned up...a lot! */ Py_ssize_t n = asdl_seq_LEN(gen->ifs); @@ -5322,28 +5322,28 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, /* comprehension specific code */ switch (type) { case COMP_GENEXP: - VISIT(c, expr, elt); - ADDOP_YIELD(c, elt_loc); - ADDOP(c, elt_loc, POP_TOP); + _VISIT(c, expr, elt); + _ADDOP_YIELD(c, elt_loc); + _ADDOP(c, elt_loc, POP_TOP); break; case COMP_LISTCOMP: - VISIT(c, expr, elt); - ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); + _VISIT(c, expr, elt); + _ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); break; case COMP_SETCOMP: - VISIT(c, expr, elt); - ADDOP_I(c, elt_loc, SET_ADD, depth + 1); + _VISIT(c, expr, elt); + _ADDOP_I(c, elt_loc, SET_ADD, depth + 1); break; case COMP_DICTCOMP: /* With '{k: v}', k is evaluated before v, so we do the same. */ - VISIT(c, expr, elt); - VISIT(c, expr, val); + _VISIT(c, expr, elt); + _VISIT(c, expr, val); elt_loc = LOCATION(elt->lineno, val->end_lineno, elt->col_offset, val->end_col_offset); - ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); + _ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); break; default: return 0; @@ -5352,10 +5352,10 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, USE_LABEL(c, if_cleanup); if (IS_LABEL(start)) { - ADDOP_JUMP(c, elt_loc, JUMP, start); + _ADDOP_JUMP(c, elt_loc, JUMP, start); USE_LABEL(c, anchor); - ADDOP(c, NO_LOCATION, END_FOR); + _ADDOP(c, NO_LOCATION, END_FOR); } return 1; @@ -5377,12 +5377,12 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, if (gen_index == 0) { /* Receive outermost iter as an implicit argument */ c->u->u_argcount = 1; - ADDOP_I(c, loc, LOAD_FAST, 0); + _ADDOP_I(c, loc, LOAD_FAST, 0); } else { /* Sub-iter - calculate on the fly */ - VISIT(c, expr, gen->iter); - ADDOP(c, loc, GET_AITER); + _VISIT(c, expr, gen->iter); + _ADDOP(c, loc, GET_AITER); } USE_LABEL(c, start); @@ -5392,12 +5392,12 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, return 0; } - ADDOP_JUMP(c, loc, SETUP_FINALLY, except); - ADDOP(c, loc, GET_ANEXT); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); - ADDOP(c, loc, POP_BLOCK); - VISIT(c, expr, gen->target); + _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + _ADDOP(c, loc, GET_ANEXT); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); + _ADDOP(c, loc, POP_BLOCK); + _VISIT(c, expr, gen->target); Py_ssize_t n = asdl_seq_LEN(gen->ifs); for (Py_ssize_t i = 0; i < n; i++) { @@ -5422,28 +5422,28 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, /* comprehension specific code */ switch (type) { case COMP_GENEXP: - VISIT(c, expr, elt); - ADDOP_YIELD(c, elt_loc); - ADDOP(c, elt_loc, POP_TOP); + _VISIT(c, expr, elt); + _ADDOP_YIELD(c, elt_loc); + _ADDOP(c, elt_loc, POP_TOP); break; case COMP_LISTCOMP: - VISIT(c, expr, elt); - ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); + _VISIT(c, expr, elt); + _ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); break; case COMP_SETCOMP: - VISIT(c, expr, elt); - ADDOP_I(c, elt_loc, SET_ADD, depth + 1); + _VISIT(c, expr, elt); + _ADDOP_I(c, elt_loc, SET_ADD, depth + 1); break; case COMP_DICTCOMP: /* With '{k: v}', k is evaluated before v, so we do the same. */ - VISIT(c, expr, elt); - VISIT(c, expr, val); + _VISIT(c, expr, elt); + _VISIT(c, expr, val); elt_loc = LOCATION(elt->lineno, val->end_lineno, elt->col_offset, val->end_col_offset); - ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); + _ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); break; default: return 0; @@ -5451,13 +5451,13 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, } USE_LABEL(c, if_cleanup); - ADDOP_JUMP(c, elt_loc, JUMP, start); + _ADDOP_JUMP(c, elt_loc, JUMP, start); compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); USE_LABEL(c, except); - ADDOP(c, loc, END_ASYNC_FOR); + _ADDOP(c, loc, END_ASYNC_FOR); return 1; } @@ -5512,7 +5512,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, goto error_in_scope; } - ADDOP_I(c, loc, op, 0); + _ADDOP_I(c, loc, op, 0); } if (!compiler_comprehension_generator(c, loc, generators, 0, 0, @@ -5521,7 +5521,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, } if (type != COMP_GENEXP) { - ADDOP(c, LOC(e), RETURN_VALUE); + _ADDOP(c, LOC(e), RETURN_VALUE); } if (type == COMP_GENEXP) { if (!wrap_in_stopiteration_handler(c)) { @@ -5545,21 +5545,21 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, Py_DECREF(qualname); Py_DECREF(co); - VISIT(c, expr, outermost->iter); + _VISIT(c, expr, outermost->iter); loc = LOC(e); if (outermost->is_async) { - ADDOP(c, loc, GET_AITER); + _ADDOP(c, loc, GET_AITER); } else { - ADDOP(c, loc, GET_ITER); + _ADDOP(c, loc, GET_ITER); } - ADDOP_I(c, loc, CALL, 0); + _ADDOP_I(c, loc, CALL, 0); if (is_async_generator && type != COMP_GENEXP) { - ADDOP_I(c, loc, GET_AWAITABLE, 0); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); + _ADDOP_I(c, loc, GET_AWAITABLE, 0); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); } return 1; @@ -5616,7 +5616,7 @@ compiler_dictcomp(struct compiler *c, expr_ty e) static int compiler_visit_keyword(struct compiler *c, keyword_ty k) { - VISIT(c, expr, k->value); + _VISIT(c, expr, k->value); return 1; } @@ -5624,20 +5624,20 @@ 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_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress); - ADDOP_I(c, NO_LOCATION, RERAISE, 2); + _ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress); + _ADDOP_I(c, NO_LOCATION, RERAISE, 2); USE_LABEL(c, suppress); - ADDOP(c, NO_LOCATION, POP_TOP); /* exc_value */ - ADDOP(c, NO_LOCATION, POP_BLOCK); - ADDOP(c, NO_LOCATION, POP_EXCEPT); - ADDOP(c, NO_LOCATION, POP_TOP); - ADDOP(c, NO_LOCATION, POP_TOP); + _ADDOP(c, NO_LOCATION, POP_TOP); /* exc_value */ + _ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_EXCEPT); + _ADDOP(c, NO_LOCATION, POP_TOP); + _ADDOP(c, NO_LOCATION, POP_TOP); NEW_JUMP_TARGET_LABEL(c, exit); - ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); USE_LABEL(c, cleanup); - POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); + _POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); USE_LABEL(c, exit); return 1; @@ -5686,14 +5686,14 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) NEW_JUMP_TARGET_LABEL(c, cleanup); /* Evaluate EXPR */ - VISIT(c, expr, item->context_expr); + _VISIT(c, expr, item->context_expr); - ADDOP(c, loc, BEFORE_ASYNC_WITH); - ADDOP_I(c, loc, GET_AWAITABLE, 1); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); + _ADDOP(c, loc, BEFORE_ASYNC_WITH); + _ADDOP_I(c, loc, GET_AWAITABLE, 1); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); - ADDOP_JUMP(c, loc, SETUP_WITH, final); + _ADDOP_JUMP(c, loc, SETUP_WITH, final); /* SETUP_WITH pushes a finally block. */ USE_LABEL(c, block); @@ -5702,17 +5702,17 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) } if (item->optional_vars) { - VISIT(c, expr, item->optional_vars); + _VISIT(c, expr, item->optional_vars); } else { /* Discard result from context.__aenter__() */ - ADDOP(c, loc, POP_TOP); + _ADDOP(c, loc, POP_TOP); } pos++; if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) { /* BLOCK code */ - VISIT_SEQ(c, stmt, s->v.AsyncWith.body) + _VISIT_SEQ(c, stmt, s->v.AsyncWith.body) } else if (!compiler_async_with(c, s, pos)) { return 0; @@ -5720,7 +5720,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) compiler_pop_fblock(c, ASYNC_WITH, block); - ADDOP(c, loc, POP_BLOCK); + _ADDOP(c, loc, POP_BLOCK); /* End of body; start the cleanup */ /* For successful outcome: @@ -5728,23 +5728,23 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) */ if(!compiler_call_exit_with_nones(c, loc)) return 0; - ADDOP_I(c, loc, GET_AWAITABLE, 2); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); + _ADDOP_I(c, loc, GET_AWAITABLE, 2); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); - ADDOP(c, loc, POP_TOP); + _ADDOP(c, loc, POP_TOP); - ADDOP_JUMP(c, loc, JUMP, exit); + _ADDOP_JUMP(c, loc, JUMP, exit); /* For exceptional outcome: */ USE_LABEL(c, final); - ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - ADDOP(c, loc, PUSH_EXC_INFO); - ADDOP(c, loc, WITH_EXCEPT_START); - ADDOP_I(c, loc, GET_AWAITABLE, 2); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); + _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + _ADDOP(c, loc, PUSH_EXC_INFO); + _ADDOP(c, loc, WITH_EXCEPT_START); + _ADDOP_I(c, loc, GET_AWAITABLE, 2); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); compiler_with_except_finish(c, cleanup); USE_LABEL(c, exit); @@ -5786,11 +5786,11 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) NEW_JUMP_TARGET_LABEL(c, cleanup); /* Evaluate EXPR */ - VISIT(c, expr, item->context_expr); + _VISIT(c, expr, item->context_expr); /* Will push bound __exit__ */ location loc = LOC(s); - ADDOP(c, loc, BEFORE_WITH); - ADDOP_JUMP(c, loc, SETUP_WITH, final); + _ADDOP(c, loc, BEFORE_WITH); + _ADDOP_JUMP(c, loc, SETUP_WITH, final); /* SETUP_WITH pushes a finally block. */ USE_LABEL(c, block); @@ -5799,21 +5799,21 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) } if (item->optional_vars) { - VISIT(c, expr, item->optional_vars); + _VISIT(c, expr, item->optional_vars); } else { /* Discard result from context.__enter__() */ - ADDOP(c, loc, POP_TOP); + _ADDOP(c, loc, POP_TOP); } pos++; if (pos == asdl_seq_LEN(s->v.With.items)) /* BLOCK code */ - VISIT_SEQ(c, stmt, s->v.With.body) + _VISIT_SEQ(c, stmt, s->v.With.body) else if (!compiler_with(c, s, pos)) return 0; - ADDOP(c, NO_LOCATION, POP_BLOCK); + _ADDOP(c, NO_LOCATION, POP_BLOCK); compiler_pop_fblock(c, WITH, block); /* End of body; start the cleanup. */ @@ -5824,15 +5824,15 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) loc = LOC(s); if (!compiler_call_exit_with_nones(c, loc)) return 0; - ADDOP(c, loc, POP_TOP); - ADDOP_JUMP(c, loc, JUMP, exit); + _ADDOP(c, loc, POP_TOP); + _ADDOP_JUMP(c, loc, JUMP, exit); /* For exceptional outcome: */ USE_LABEL(c, final); - ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - ADDOP(c, loc, PUSH_EXC_INFO); - ADDOP(c, loc, WITH_EXCEPT_START); + _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + _ADDOP(c, loc, PUSH_EXC_INFO); + _ADDOP(c, loc, WITH_EXCEPT_START); compiler_with_except_finish(c, cleanup); USE_LABEL(c, exit); @@ -5845,20 +5845,20 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) location loc = LOC(e); switch (e->kind) { case NamedExpr_kind: - VISIT(c, expr, e->v.NamedExpr.value); - ADDOP_I(c, loc, COPY, 1); - VISIT(c, expr, e->v.NamedExpr.target); + _VISIT(c, expr, e->v.NamedExpr.value); + _ADDOP_I(c, loc, COPY, 1); + _VISIT(c, expr, e->v.NamedExpr.target); break; case BoolOp_kind: return compiler_boolop(c, e); case BinOp_kind: - VISIT(c, expr, e->v.BinOp.left); - VISIT(c, expr, e->v.BinOp.right); - ADDOP_BINARY(c, loc, e->v.BinOp.op); + _VISIT(c, expr, e->v.BinOp.left); + _VISIT(c, expr, e->v.BinOp.right); + _ADDOP_BINARY(c, loc, e->v.BinOp.op); break; case UnaryOp_kind: - VISIT(c, expr, e->v.UnaryOp.operand); - ADDOP(c, loc, unaryop(e->v.UnaryOp.op)); + _VISIT(c, expr, e->v.UnaryOp.operand); + _ADDOP(c, loc, unaryop(e->v.UnaryOp.op)); break; case Lambda_kind: return compiler_lambda(c, e); @@ -5880,12 +5880,12 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) if (c->u->u_ste->ste_type != FunctionBlock) return compiler_error(c, loc, "'yield' outside function"); if (e->v.Yield.value) { - VISIT(c, expr, e->v.Yield.value); + _VISIT(c, expr, e->v.Yield.value); } else { - ADDOP_LOAD_CONST(c, loc, Py_None); + _ADDOP_LOAD_CONST(c, loc, Py_None); } - ADDOP_YIELD(c, loc); + _ADDOP_YIELD(c, loc); break; case YieldFrom_kind: if (c->u->u_ste->ste_type != FunctionBlock) @@ -5894,10 +5894,10 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) return compiler_error(c, loc, "'yield from' inside async function"); - VISIT(c, expr, e->v.YieldFrom.value); - ADDOP(c, loc, GET_YIELD_FROM_ITER); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 0); + _VISIT(c, expr, e->v.YieldFrom.value); + _ADDOP(c, loc, GET_YIELD_FROM_ITER); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 0); break; case Await_kind: if (!IS_TOP_LEVEL_AWAIT(c)){ @@ -5911,17 +5911,17 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) } } - VISIT(c, expr, e->v.Await.value); - ADDOP_I(c, loc, GET_AWAITABLE, 0); - ADDOP_LOAD_CONST(c, loc, Py_None); - ADD_YIELD_FROM(c, loc, 1); + _VISIT(c, expr, e->v.Await.value); + _ADDOP_I(c, loc, GET_AWAITABLE, 0); + _ADDOP_LOAD_CONST(c, loc, Py_None); + _ADD_YIELD_FROM(c, loc, 1); break; case Compare_kind: return compiler_compare(c, e); case Call_kind: return compiler_call(c, e); case Constant_kind: - ADDOP_LOAD_CONST(c, loc, e->v.Constant.value); + _ADDOP_LOAD_CONST(c, loc, e->v.Constant.value); break; case JoinedStr_kind: return compiler_joined_str(c, e); @@ -5929,21 +5929,21 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) return compiler_formatted_value(c, e); /* The following exprs can be assignment targets. */ case Attribute_kind: - VISIT(c, expr, e->v.Attribute.value); + _VISIT(c, expr, e->v.Attribute.value); loc = LOC(e); loc = update_start_location_to_match_attr(c, loc, e); switch (e->v.Attribute.ctx) { case Load: - ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); + _ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); break; case Store: if (forbidden_name(c, loc, e->v.Attribute.attr, e->v.Attribute.ctx)) { return 0; } - ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); + _ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); break; case Del: - ADDOP_NAME(c, loc, DELETE_ATTR, e->v.Attribute.attr, names); + _ADDOP_NAME(c, loc, DELETE_ATTR, e->v.Attribute.attr, names); break; } break; @@ -5967,7 +5967,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) if (n == 0) { return 0; } - ADDOP_I(c, loc, BUILD_SLICE, n); + _ADDOP_I(c, loc, BUILD_SLICE, n); break; } case Name_kind: @@ -6005,27 +6005,27 @@ compiler_augassign(struct compiler *c, stmt_ty s) switch (e->kind) { case Attribute_kind: - VISIT(c, expr, e->v.Attribute.value); - ADDOP_I(c, loc, COPY, 1); + _VISIT(c, expr, e->v.Attribute.value); + _ADDOP_I(c, loc, COPY, 1); loc = update_start_location_to_match_attr(c, loc, e); - ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); + _ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); break; case Subscript_kind: - VISIT(c, expr, e->v.Subscript.value); + _VISIT(c, expr, e->v.Subscript.value); if (is_two_element_slice(e->v.Subscript.slice)) { if (!compiler_slice(c, e->v.Subscript.slice)) { return 0; } - ADDOP_I(c, loc, COPY, 3); - ADDOP_I(c, loc, COPY, 3); - ADDOP_I(c, loc, COPY, 3); - ADDOP(c, loc, BINARY_SLICE); + _ADDOP_I(c, loc, COPY, 3); + _ADDOP_I(c, loc, COPY, 3); + _ADDOP_I(c, loc, COPY, 3); + _ADDOP(c, loc, BINARY_SLICE); } else { - VISIT(c, expr, e->v.Subscript.slice); - ADDOP_I(c, loc, COPY, 2); - ADDOP_I(c, loc, COPY, 2); - ADDOP(c, loc, BINARY_SUBSCR); + _VISIT(c, expr, e->v.Subscript.slice); + _ADDOP_I(c, loc, COPY, 2); + _ADDOP_I(c, loc, COPY, 2); + _ADDOP(c, loc, BINARY_SUBSCR); } break; case Name_kind: @@ -6041,28 +6041,28 @@ compiler_augassign(struct compiler *c, stmt_ty s) loc = LOC(s); - VISIT(c, expr, s->v.AugAssign.value); - ADDOP_INPLACE(c, loc, s->v.AugAssign.op); + _VISIT(c, expr, s->v.AugAssign.value); + _ADDOP_INPLACE(c, loc, s->v.AugAssign.op); loc = LOC(e); switch (e->kind) { case Attribute_kind: loc = update_start_location_to_match_attr(c, loc, e); - ADDOP_I(c, loc, SWAP, 2); - ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); + _ADDOP_I(c, loc, SWAP, 2); + _ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); break; case Subscript_kind: if (is_two_element_slice(e->v.Subscript.slice)) { - ADDOP_I(c, loc, SWAP, 4); - ADDOP_I(c, loc, SWAP, 3); - ADDOP_I(c, loc, SWAP, 2); - ADDOP(c, loc, STORE_SLICE); + _ADDOP_I(c, loc, SWAP, 4); + _ADDOP_I(c, loc, SWAP, 3); + _ADDOP_I(c, loc, SWAP, 2); + _ADDOP(c, loc, STORE_SLICE); } else { - ADDOP_I(c, loc, SWAP, 3); - ADDOP_I(c, loc, SWAP, 2); - ADDOP(c, loc, STORE_SUBSCR); + _ADDOP_I(c, loc, SWAP, 3); + _ADDOP_I(c, loc, SWAP, 2); + _ADDOP(c, loc, STORE_SUBSCR); } break; case Name_kind: @@ -6076,8 +6076,8 @@ compiler_augassign(struct compiler *c, stmt_ty s) static int check_ann_expr(struct compiler *c, expr_ty e) { - VISIT(c, expr, e); - ADDOP(c, LOC(e), POP_TOP); + _VISIT(c, expr, e); + _ADDOP(c, LOC(e), POP_TOP); return 1; } @@ -6141,8 +6141,8 @@ compiler_annassign(struct compiler *c, stmt_ty s) /* We perform the actual assignment first. */ if (s->v.AnnAssign.value) { - VISIT(c, expr, s->v.AnnAssign.value); - VISIT(c, expr, targ); + _VISIT(c, expr, s->v.AnnAssign.value); + _VISIT(c, expr, targ); } switch (targ->kind) { case Name_kind: @@ -6153,15 +6153,15 @@ compiler_annassign(struct compiler *c, stmt_ty s) (c->u->u_scope_type == COMPILER_SCOPE_MODULE || c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) { - VISIT(c, annexpr, s->v.AnnAssign.annotation) + _VISIT(c, annexpr, s->v.AnnAssign.annotation) } else { - VISIT(c, expr, s->v.AnnAssign.annotation); + _VISIT(c, expr, s->v.AnnAssign.annotation); } - ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names); + _ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names); mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); - ADDOP_LOAD_CONST_NEW(c, loc, mangled); - ADDOP(c, loc, STORE_SUBSCR); + _ADDOP_LOAD_CONST_NEW(c, loc, mangled); + _ADDOP(c, loc, STORE_SUBSCR); } break; case Attribute_kind: @@ -6273,28 +6273,28 @@ compiler_subscript(struct compiler *c, expr_ty e) } } - VISIT(c, expr, e->v.Subscript.value); + _VISIT(c, expr, e->v.Subscript.value); if (is_two_element_slice(e->v.Subscript.slice) && ctx != Del) { if (!compiler_slice(c, e->v.Subscript.slice)) { return 0; } if (ctx == Load) { - ADDOP(c, loc, BINARY_SLICE); + _ADDOP(c, loc, BINARY_SLICE); } else { assert(ctx == Store); - ADDOP(c, loc, STORE_SLICE); + _ADDOP(c, loc, STORE_SLICE); } } else { - VISIT(c, expr, e->v.Subscript.slice); + _VISIT(c, expr, e->v.Subscript.slice); switch (ctx) { case Load: op = BINARY_SUBSCR; break; case Store: op = STORE_SUBSCR; break; case Del: op = DELETE_SUBSCR; break; } assert(op); - ADDOP(c, loc, op); + _ADDOP(c, loc, op); } return 1; } @@ -6309,22 +6309,22 @@ compiler_slice(struct compiler *c, expr_ty s) /* only handles the cases where BUILD_SLICE is emitted */ if (s->v.Slice.lower) { - VISIT(c, expr, s->v.Slice.lower); + _VISIT(c, expr, s->v.Slice.lower); } else { - ADDOP_LOAD_CONST(c, LOC(s), Py_None); + _ADDOP_LOAD_CONST(c, LOC(s), Py_None); } if (s->v.Slice.upper) { - VISIT(c, expr, s->v.Slice.upper); + _VISIT(c, expr, s->v.Slice.upper); } else { - ADDOP_LOAD_CONST(c, LOC(s), Py_None); + _ADDOP_LOAD_CONST(c, LOC(s), Py_None); } if (s->v.Slice.step) { n++; - VISIT(c, expr, s->v.Slice.step); + _VISIT(c, expr, s->v.Slice.step); } return n; } @@ -6383,7 +6383,7 @@ jump_to_fail_pop(struct compiler *c, location loc, // capture on success: Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores); RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops)); - ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]); + _ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]); return 1; } @@ -6423,7 +6423,7 @@ static int pattern_helper_rotate(struct compiler *c, location loc, Py_ssize_t count) { while (1 < count) { - ADDOP_I(c, loc, SWAP, count--); + _ADDOP_I(c, loc, SWAP, count--); } return 1; } @@ -6433,7 +6433,7 @@ pattern_helper_store_name(struct compiler *c, location loc, identifier n, pattern_context *pc) { if (n == NULL) { - ADDOP(c, loc, POP_TOP); + _ADDOP(c, loc, POP_TOP); return 1; } if (forbidden_name(c, loc, n, Store)) { @@ -6468,7 +6468,7 @@ pattern_unpack_helper(struct compiler *c, location loc, return compiler_error(c, loc, "too many expressions in " "star-unpacking sequence pattern"); - ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); + _ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); seen_star = 1; } else if (elt->kind == MatchStar_kind) { @@ -6477,7 +6477,7 @@ pattern_unpack_helper(struct compiler *c, location loc, } } if (!seen_star) { - ADDOP_I(c, loc, UNPACK_SEQUENCE, n); + _ADDOP_I(c, loc, UNPACK_SEQUENCE, n); } return 1; } @@ -6521,23 +6521,23 @@ pattern_helper_sequence_subscr(struct compiler *c, location loc, assert(WILDCARD_STAR_CHECK(pattern)); continue; } - ADDOP_I(c, loc, COPY, 1); + _ADDOP_I(c, loc, COPY, 1); if (i < star) { - ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(i)); + _ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(i)); } else { // The subject may not support negative indexing! Compute a // nonnegative index: - ADDOP(c, loc, GET_LEN); - ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(size - i)); - ADDOP_BINARY(c, loc, Sub); + _ADDOP(c, loc, GET_LEN); + _ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(size - i)); + _ADDOP_BINARY(c, loc, Sub); } - ADDOP(c, loc, BINARY_SUBSCR); + _ADDOP(c, loc, BINARY_SUBSCR); RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); } // Pop the subject, we're done with it: pc->on_top--; - ADDOP(c, loc, POP_TOP); + _ADDOP(c, loc, POP_TOP); return 1; } @@ -6571,7 +6571,7 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) } // Need to make a copy for (possibly) storing later: pc->on_top++; - ADDOP_I(c, LOC(p), COPY, 1); + _ADDOP_I(c, LOC(p), COPY, 1); RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); // Success! Store it: pc->on_top--; @@ -6634,7 +6634,7 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) if (nattrs) { RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); } - VISIT(c, expr, p->v.MatchClass.cls); + _VISIT(c, expr, p->v.MatchClass.cls); PyObject *attr_names; RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs)); Py_ssize_t i; @@ -6642,15 +6642,15 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) PyObject *name = asdl_seq_GET(kwd_attrs, i); PyTuple_SET_ITEM(attr_names, i, Py_NewRef(name)); } - ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names); - ADDOP_I(c, LOC(p), MATCH_CLASS, nargs); - ADDOP_I(c, LOC(p), COPY, 1); - ADDOP_LOAD_CONST(c, LOC(p), Py_None); - ADDOP_I(c, LOC(p), IS_OP, 1); + _ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names); + _ADDOP_I(c, LOC(p), MATCH_CLASS, nargs); + _ADDOP_I(c, LOC(p), COPY, 1); + _ADDOP_LOAD_CONST(c, LOC(p), Py_None); + _ADDOP_I(c, LOC(p), IS_OP, 1); // TOS is now a tuple of (nargs + nattrs) attributes (or None): pc->on_top++; RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); - ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs); + _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs); pc->on_top += nargs + nattrs - 1; for (i = 0; i < nargs + nattrs; i++) { pc->on_top--; @@ -6664,7 +6664,7 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) pattern = asdl_seq_GET(kwd_patterns, i - nargs); } if (WILDCARD_CHECK(pattern)) { - ADDOP(c, LOC(p), POP_TOP); + _ADDOP(c, LOC(p), POP_TOP); continue; } RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); @@ -6692,19 +6692,19 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, PyObject *star_target = p->v.MatchMapping.rest; // We need to keep the subject on top during the mapping and length checks: pc->on_top++; - ADDOP(c, LOC(p), MATCH_MAPPING); + _ADDOP(c, LOC(p), MATCH_MAPPING); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); if (!size && !star_target) { // If the pattern is just "{}", we're done! Pop the subject: pc->on_top--; - ADDOP(c, LOC(p), POP_TOP); + _ADDOP(c, LOC(p), POP_TOP); return 1; } if (size) { // If the pattern has any keys in it, perform a length check: - ADDOP(c, LOC(p), GET_LEN); - ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); - ADDOP_COMPARE(c, LOC(p), GtE); + _ADDOP(c, LOC(p), GET_LEN); + _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); + _ADDOP_COMPARE(c, LOC(p), GtE); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } if (INT_MAX < size - 1) { @@ -6759,17 +6759,17 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // all keys have been checked; there are no duplicates Py_DECREF(seen); - ADDOP_I(c, LOC(p), BUILD_TUPLE, size); - ADDOP(c, LOC(p), MATCH_KEYS); + _ADDOP_I(c, LOC(p), BUILD_TUPLE, size); + _ADDOP(c, LOC(p), MATCH_KEYS); // There's now a tuple of keys and a tuple of values on top of the subject: pc->on_top += 2; - ADDOP_I(c, LOC(p), COPY, 1); - ADDOP_LOAD_CONST(c, LOC(p), Py_None); - ADDOP_I(c, LOC(p), IS_OP, 1); + _ADDOP_I(c, LOC(p), COPY, 1); + _ADDOP_LOAD_CONST(c, LOC(p), Py_None); + _ADDOP_I(c, LOC(p), IS_OP, 1); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); // So far so good. Use that tuple of values on the stack to match // sub-patterns against: - ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); + _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); pc->on_top += size - 1; for (Py_ssize_t i = 0; i < size; i++) { pc->on_top--; @@ -6786,20 +6786,20 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // rest = dict(TOS1) // for key in TOS: // del rest[key] - ADDOP_I(c, LOC(p), BUILD_MAP, 0); // [subject, keys, empty] - ADDOP_I(c, LOC(p), SWAP, 3); // [empty, keys, subject] - ADDOP_I(c, LOC(p), DICT_UPDATE, 2); // [copy, keys] - ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); // [copy, keys...] + _ADDOP_I(c, LOC(p), BUILD_MAP, 0); // [subject, keys, empty] + _ADDOP_I(c, LOC(p), SWAP, 3); // [empty, keys, subject] + _ADDOP_I(c, LOC(p), DICT_UPDATE, 2); // [copy, keys] + _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); // [copy, keys...] while (size) { - ADDOP_I(c, LOC(p), COPY, 1 + size--); // [copy, keys..., copy] - ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key] - ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...] + _ADDOP_I(c, LOC(p), COPY, 1 + size--); // [copy, keys..., copy] + _ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key] + _ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...] } RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), star_target, pc)); } else { - ADDOP(c, LOC(p), POP_TOP); // Tuple of keys. - ADDOP(c, LOC(p), POP_TOP); // Subject. + _ADDOP(c, LOC(p), POP_TOP); // Tuple of keys. + _ADDOP(c, LOC(p), POP_TOP); // Subject. } return 1; @@ -6948,7 +6948,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) Py_DECREF(control); // NOTE: Returning macros are safe again. // Pop the copy of the subject: - ADDOP(c, LOC(p), POP_TOP); + _ADDOP(c, LOC(p), POP_TOP); return 1; diff: compiler_error(c, LOC(p), "alternative patterns bind different names"); @@ -6987,27 +6987,27 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, } // We need to keep the subject on top during the sequence and length checks: pc->on_top++; - ADDOP(c, LOC(p), MATCH_SEQUENCE); + _ADDOP(c, LOC(p), MATCH_SEQUENCE); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); if (star < 0) { // No star: len(subject) == size - ADDOP(c, LOC(p), GET_LEN); - ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); - ADDOP_COMPARE(c, LOC(p), Eq); + _ADDOP(c, LOC(p), GET_LEN); + _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); + _ADDOP_COMPARE(c, LOC(p), Eq); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } else if (size > 1) { // Star: len(subject) >= size - 1 - ADDOP(c, LOC(p), GET_LEN); - ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1)); - ADDOP_COMPARE(c, LOC(p), GtE); + _ADDOP(c, LOC(p), GET_LEN); + _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1)); + _ADDOP_COMPARE(c, LOC(p), GtE); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } // Whatever comes next should consume the subject: pc->on_top--; if (only_wildcard) { // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. - ADDOP(c, LOC(p), POP_TOP); + _ADDOP(c, LOC(p), POP_TOP); } else if (star_wildcard) { RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc)); @@ -7027,8 +7027,8 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) const char *e = "patterns may only match literals and attribute lookups"; return compiler_error(c, LOC(p), e); } - VISIT(c, expr, value); - ADDOP_COMPARE(c, LOC(p), Eq); + _VISIT(c, expr, value); + _ADDOP_COMPARE(c, LOC(p), Eq); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); return 1; } @@ -7037,8 +7037,8 @@ static int compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchSingleton_kind); - ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value); - ADDOP_COMPARE(c, LOC(p), Is); + _ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value); + _ADDOP_COMPARE(c, LOC(p), Is); RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); return 1; } @@ -7073,7 +7073,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) static int compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) { - VISIT(c, expr, s->v.Match.subject); + _VISIT(c, expr, s->v.Match.subject); NEW_JUMP_TARGET_LABEL(c, end); Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); assert(cases > 0); @@ -7083,7 +7083,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) m = asdl_seq_GET(s->v.Match.cases, i); // Only copy the subject if we're *not* on the last case: if (i != cases - has_default - 1) { - ADDOP_I(c, LOC(m->pattern), COPY, 1); + _ADDOP_I(c, LOC(m->pattern), COPY, 1); } RETURN_IF_FALSE(pc->stores = PyList_New(0)); // Irrefutable cases must be either guarded, last, or both: @@ -7114,10 +7114,10 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) } // Success! Pop the subject off, we're done with it: if (i != cases - has_default - 1) { - ADDOP(c, LOC(m->pattern), POP_TOP); + _ADDOP(c, LOC(m->pattern), POP_TOP); } - VISIT_SEQ(c, stmt, m->body); - ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + _VISIT_SEQ(c, stmt, m->body); + _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); // If the pattern fails to match, we want the line number of the // cleanup to be associated with the failed pattern, not the last line // of the body @@ -7129,16 +7129,16 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) m = asdl_seq_GET(s->v.Match.cases, cases - 1); if (cases == 1) { // No matches. Done with the subject: - ADDOP(c, LOC(m->pattern), POP_TOP); + _ADDOP(c, LOC(m->pattern), POP_TOP); } else { // Show line coverage for default case (it doesn't create bytecode) - ADDOP(c, LOC(m->pattern), NOP); + _ADDOP(c, LOC(m->pattern), NOP); } if (m->guard) { RETURN_IF_FALSE(compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)); } - VISIT_SEQ(c, stmt, m->body); + _VISIT_SEQ(c, stmt, m->body); } USE_LABEL(c, end); return 1; @@ -8830,9 +8830,9 @@ assemble(struct compiler *c, int addNone) /* Make sure every block that falls off the end returns None. */ if (!basicblock_returns(CFG_BUILDER(c)->g_curblock)) { if (addNone) { - ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); } - ADDOP(c, NO_LOCATION, RETURN_VALUE); + _ADDOP(c, NO_LOCATION, RETURN_VALUE); } int nblocks = 0; From 4ca4b8c2f204a1b447524748a96cc76ca0a26523 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:32:54 +0000 Subject: [PATCH 12/93] add new versions of the macros (not used yet) --- Python/compile.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/Python/compile.c b/Python/compile.c index 27efd2740aede5..a41a841ab54215 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1609,6 +1609,123 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return cfg_builder_addop(g, opcode, target.id, loc); } +#define ADDOP(C, LOC, OP) \ + RETURN_IF_ERROR(cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC))); + +#define ADDOP_IN_SCOPE(C, LOC, OP) { \ + if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) { \ + compiler_exit_scope(c); \ + return -1; \ + } \ +} + +#define ADDOP_LOAD_CONST(C, LOC, O) \ + RETURN_IF_ERROR(compiler_addop_load_const((C), (LOC), (O))); + +/* Same as ADDOP_LOAD_CONST, but steals a reference. */ +#define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \ + PyObject *__new_const = (O); \ + if (__new_const == NULL) { \ + return ERROR; \ + } \ + if (compiler_addop_load_const((C), (LOC), __new_const) < 0) { \ + Py_DECREF(__new_const); \ + return ERROR; \ + } \ + Py_DECREF(__new_const); \ +} + +#define ADDOP_N(C, LOC, OP, O, TYPE) { \ + assert(!HAS_CONST(OP)); /* use _ADDOP_LOAD_CONST_NEW */ \ + if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) { \ + Py_DECREF((O)); \ + return ERROR; \ + } \ + Py_DECREF((O)); \ +} + +#define ADDOP_NAME(C, LOC, OP, O, TYPE) \ + RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O))); + +#define ADDOP_I(C, LOC, OP, O) \ + RETURN_IF_ERROR(cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC))); + +#define ADDOP_JUMP(C, LOC, OP, O) \ + RETURN_IF_ERROR(cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O))); + +#define ADDOP_COMPARE(C, LOC, CMP) { \ + if (!compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))) \ + return ERROR; \ +} + +#define ADDOP_BINARY(C, LOC, BINOP) { \ + if (!addop_binary((C), (LOC), (BINOP), false)) \ + return ERROR; \ +} + +#define ADDOP_INPLACE(C, LOC, BINOP) { \ + if (!addop_binary((C), (LOC), (BINOP), true)) \ + return ERROR; \ +} + +#define ADD_YIELD_FROM(C, LOC, await) { \ + if (! compiler_add_yield_from((C), (LOC), (await))) \ + return ERROR; \ +} + +#define POP_EXCEPT_AND_RERAISE(C, LOC) { \ + if (!compiler_pop_except_and_reraise((C), (LOC))) \ + return ERROR; \ +} + +#define ADDOP_YIELD(C, LOC) { \ + if (!addop_yield((C), (LOC))) \ + return ERROR; \ +} + +/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use + the ASDL name to synthesize the name of the C type and the visit function. +*/ + +#define VISIT(C, TYPE, V) {\ + if (!compiler_visit_ ## TYPE((C), (V))) \ + return ERROR; \ +} + +#define VISIT_IN_SCOPE(C, TYPE, V) {\ + if (!compiler_visit_ ## TYPE((C), (V))) { \ + compiler_exit_scope(c); \ + return ERROR; \ + } \ +} + +#define VISIT_SEQ(C, TYPE, SEQ) { \ + int _i; \ + asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ + for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ + TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ + if (!compiler_visit_ ## TYPE((C), elt)) \ + return ERROR; \ + } \ +} + +#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ + int _i; \ + asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ + for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ + TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ + if (!compiler_visit_ ## TYPE((C), elt)) { \ + compiler_exit_scope(c); \ + return ERROR; \ + } \ + } \ +} + + +/*******************/ + + + #define _ADDOP(C, LOC, OP) { \ if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) \ From 9baeb63222985bf754f5f198906186fdc611a5f8 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:36:01 +0000 Subject: [PATCH 13/93] compiler_enter_scope returns SUCCESS/ERROR --- Python/compile.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index a41a841ab54215..a742508aefa68a 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1856,7 +1856,7 @@ compiler_enter_scope(struct compiler *c, identifier name, struct compiler_unit)); if (!u) { PyErr_NoMemory(); - return 0; + return ERROR; } u->u_scope_type = scope_type; u->u_argcount = 0; @@ -1865,14 +1865,14 @@ compiler_enter_scope(struct compiler *c, identifier name, u->u_ste = PySymtable_Lookup(c->c_st, key); if (!u->u_ste) { compiler_unit_free(u); - return 0; + return ERROR; } u->u_name = Py_NewRef(name); u->u_varnames = list2dict(u->u_ste->ste_varnames); u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); if (!u->u_varnames || !u->u_cellvars) { compiler_unit_free(u); - return 0; + return ERROR; } if (u->u_ste->ste_needs_class_closure) { /* Cook up an implicit __class__ cell. */ @@ -1883,7 +1883,7 @@ compiler_enter_scope(struct compiler *c, identifier name, _PyLong_GetZero()); if (res < 0) { compiler_unit_free(u); - return 0; + return ERROR; } } @@ -1891,7 +1891,7 @@ compiler_enter_scope(struct compiler *c, identifier name, PyDict_GET_SIZE(u->u_cellvars)); if (!u->u_freevars) { compiler_unit_free(u); - return 0; + return ERROR; } u->u_nfblocks = 0; @@ -1899,12 +1899,12 @@ compiler_enter_scope(struct compiler *c, identifier name, u->u_consts = PyDict_New(); if (!u->u_consts) { compiler_unit_free(u); - return 0; + return ERROR; } u->u_names = PyDict_New(); if (!u->u_names) { compiler_unit_free(u); - return 0; + return ERROR; } u->u_private = NULL; @@ -1915,7 +1915,7 @@ compiler_enter_scope(struct compiler *c, identifier name, if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { Py_XDECREF(capsule); compiler_unit_free(u); - return 0; + return ERROR; } Py_DECREF(capsule); u->u_private = Py_XNewRef(c->u->u_private); @@ -1926,7 +1926,7 @@ compiler_enter_scope(struct compiler *c, identifier name, cfg_builder *g = CFG_BUILDER(c); if (cfg_builder_init(g) < 0) { - return 0; + return ERROR; } if (u->u_scope_type == COMPILER_SCOPE_MODULE) { @@ -1934,15 +1934,15 @@ compiler_enter_scope(struct compiler *c, identifier name, } else { if (compiler_set_qualname(c) < 0){ - return 0; + return ERROR; } } - _ADDOP_I(c, loc, RESUME, 0); + ADDOP_I(c, loc, RESUME, 0); if (u->u_scope_type == COMPILER_SCOPE_MODULE) { loc.lineno = -1; } - return 1; + return SUCCESS; } static void @@ -2307,8 +2307,8 @@ static int compiler_codegen(struct compiler *c, mod_ty mod) { _Py_DECLARE_STR(anon_module, "<module>"); - if (!compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, - mod, 1)) { + if (compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, + mod, 1) < 0) { return 0; } location loc = LOCATION(1, 1, 0, 0); @@ -2810,7 +2810,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) funcflags |= 0x04; } - if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { + if (compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno) < 0) { return 0; } @@ -2882,8 +2882,8 @@ compiler_class(struct compiler *c, stmt_ty s) This borrows from compiler_call. */ /* 1. compile the class body into a code object */ - if (!compiler_enter_scope(c, s->v.ClassDef.name, - COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) { + if (compiler_enter_scope(c, s->v.ClassDef.name, + COMPILER_SCOPE_CLASS, (void *)s, firstlineno) < 0) { return 0; } /* this block represents what we do in the new scope */ @@ -3195,8 +3195,8 @@ compiler_lambda(struct compiler *c, expr_ty e) } _Py_DECLARE_STR(anon_lambda, "<lambda>"); - if (!compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA, - (void *)e, e->lineno)) { + if (compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA, + (void *)e, e->lineno) < 0) { return 0; } /* Make None the first constant, so the lambda can't have a @@ -5592,8 +5592,8 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, int is_top_level_await = IS_TOP_LEVEL_AWAIT(c); outermost = (comprehension_ty) asdl_seq_GET(generators, 0); - if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, - (void *)e, e->lineno)) + if (compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, + (void *)e, e->lineno) < 0) { goto error; } From 566acc56ab87fb30b6e6a59062438743ab524c8f Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:38:27 +0000 Subject: [PATCH 14/93] find_add returns bool --- Python/compile.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index a742508aefa68a..593e03b1812b49 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1976,7 +1976,7 @@ compiler_exit_scope(struct compiler *c) /* Search if variable annotations are present statically in a block. */ -static int +static bool find_ann(asdl_stmt_seq *stmts) { int i, j, res = 0; @@ -1986,7 +1986,7 @@ find_ann(asdl_stmt_seq *stmts) st = (stmt_ty)asdl_seq_GET(stmts, i); switch (st->kind) { case AnnAssign_kind: - return 1; + return true; case For_kind: res = find_ann(st->v.For.body) || find_ann(st->v.For.orelse); @@ -2014,7 +2014,7 @@ find_ann(asdl_stmt_seq *stmts) excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( st->v.Try.handlers, j); if (find_ann(handler->v.ExceptHandler.body)) { - return 1; + return true; } } res = find_ann(st->v.Try.body) || @@ -2026,7 +2026,7 @@ find_ann(asdl_stmt_seq *stmts) excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( st->v.TryStar.handlers, j); if (find_ann(handler->v.ExceptHandler.body)) { - return 1; + return true; } } res = find_ann(st->v.TryStar.body) || @@ -2034,7 +2034,7 @@ find_ann(asdl_stmt_seq *stmts) find_ann(st->v.TryStar.orelse); break; default: - res = 0; + res = false; } if (res) { break; From 93287a1c15394db5f786460fce21fb2f1f987472 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:46:18 +0000 Subject: [PATCH 15/93] compiler_push_fblock returns SUCCESS/ERROR --- Python/compile.c | 52 +++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 593e03b1812b49..8f27ae1192345f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2054,14 +2054,15 @@ compiler_push_fblock(struct compiler *c, location loc, { struct fblockinfo *f; if (c->u->u_nfblocks >= CO_MAXBLOCKS) { - return compiler_error(c, loc, "too many statically nested blocks"); + compiler_error(c, loc, "too many statically nested blocks"); + return ERROR; } f = &c->u->u_fblock[c->u->u_nfblocks++]; f->fb_type = t; f->fb_block = block_label; f->fb_exit = exit; f->fb_datum = datum; - return 1; + return SUCCESS; } static void @@ -2156,7 +2157,7 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, /* This POP_BLOCK gets the line number of the unwinding statement */ _ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { - if (!compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL)) { + if (compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL) < 0) { return 0; } } @@ -3271,7 +3272,7 @@ compiler_for(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup); NEW_JUMP_TARGET_LABEL(c, end); - if (!compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL)) { + if (compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL) < 0) { return 0; } _VISIT(c, expr, s->v.For.iter); @@ -3316,7 +3317,7 @@ compiler_async_for(struct compiler *c, stmt_ty s) _ADDOP(c, loc, GET_AITER); USE_LABEL(c, start); - if (!compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL)) { + if (compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL) < 0) { return 0; } /* SETUP_FINALLY to guard the __anext__ call */ @@ -3358,7 +3359,7 @@ compiler_while(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, anchor); USE_LABEL(c, loop); - if (!compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL)) { + if (compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL) < 0) { return 0; } if (!compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0)) { @@ -3515,8 +3516,9 @@ compiler_try_finally(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, loc, SETUP_FINALLY, end); USE_LABEL(c, body); - if (!compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.Try.finalbody)) + if (compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.Try.finalbody) < 0) { return 0; + } if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { if (!compiler_try_except(c, s)) return 0; @@ -3536,8 +3538,9 @@ compiler_try_finally(struct compiler *c, stmt_ty s) loc = NO_LOCATION; _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); _ADDOP(c, loc, PUSH_EXC_INFO); - if (!compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL)) + if (compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL) < 0) { return 0; + } _VISIT_SEQ(c, stmt, s->v.Try.finalbody); loc = location_of_last_executing_statement(s->v.Try.finalbody); compiler_pop_fblock(c, FINALLY_END, end); @@ -3564,7 +3567,7 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, loc, SETUP_FINALLY, end); USE_LABEL(c, body); - if (!compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.TryStar.finalbody)) { + if (compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.TryStar.finalbody) < 0) { return 0; } if (s->v.TryStar.handlers && asdl_seq_LEN(s->v.TryStar.handlers)) { @@ -3587,7 +3590,7 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s) loc = NO_LOCATION; _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); _ADDOP(c, loc, PUSH_EXC_INFO); - if (!compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL)) { + if (compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL) < 0) { return 0; } _VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); @@ -3646,8 +3649,9 @@ compiler_try_except(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); USE_LABEL(c, body); - if (!compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL)) + if (compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL) < 0) { return 0; + } _VISIT_SEQ(c, stmt, s->v.Try.body); compiler_pop_fblock(c, TRY_EXCEPT, body); _ADDOP(c, NO_LOCATION, POP_BLOCK); @@ -3662,8 +3666,9 @@ compiler_try_except(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); _ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); /* Runtime will push a block here, so we need to account for that */ - if (!compiler_push_fblock(c, loc, EXCEPTION_HANDLER, NO_LABEL, NO_LABEL, NULL)) + if (compiler_push_fblock(c, loc, EXCEPTION_HANDLER, NO_LABEL, NO_LABEL, NULL) < 0) { return 0; + } for (i = 0; i < n; i++) { excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( s->v.Try.handlers, i); @@ -3699,8 +3704,8 @@ compiler_try_except(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); USE_LABEL(c, cleanup_body); - if (!compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, - NO_LABEL, handler->v.ExceptHandler.name)) { + if (compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, + NO_LABEL, handler->v.ExceptHandler.name) < 0) { return 0; } @@ -3732,8 +3737,9 @@ compiler_try_except(struct compiler *c, stmt_ty s) _ADDOP(c, loc, POP_TOP); /* exc_value */ USE_LABEL(c, cleanup_body); - if (!compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, NULL)) + if (compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, NULL) < 0) { return 0; + } _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); _ADDOP(c, NO_LOCATION, POP_BLOCK); @@ -3818,7 +3824,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); USE_LABEL(c, body); - if (!compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL)) { + if (compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL) < 0) { return 0; } _VISIT_SEQ(c, stmt, s->v.TryStar.body); @@ -3832,8 +3838,8 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); _ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); /* Runtime will push a block here, so we need to account for that */ - if (!compiler_push_fblock(c, loc, EXCEPTION_GROUP_HANDLER, - NO_LABEL, NO_LABEL, "except handler")) { + if (compiler_push_fblock(c, loc, EXCEPTION_GROUP_HANDLER, + NO_LABEL, NO_LABEL, "except handler") < 0) { return 0; } for (Py_ssize_t i = 0; i < n; i++) { @@ -3895,7 +3901,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); USE_LABEL(c, cleanup_body); - if (!compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, handler->v.ExceptHandler.name)) { + if (compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, handler->v.ExceptHandler.name) < 0) { return 0; } @@ -5504,8 +5510,8 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, USE_LABEL(c, start); /* Runtime will push a block here, so we need to account for that */ - if (!compiler_push_fblock(c, loc, ASYNC_COMPREHENSION_GENERATOR, - start, NO_LABEL, NULL)) { + if (compiler_push_fblock(c, loc, ASYNC_COMPREHENSION_GENERATOR, + start, NO_LABEL, NULL) < 0) { return 0; } @@ -5814,7 +5820,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) /* SETUP_WITH pushes a finally block. */ USE_LABEL(c, block); - if (!compiler_push_fblock(c, loc, ASYNC_WITH, block, final, s)) { + if (compiler_push_fblock(c, loc, ASYNC_WITH, block, final, s) < 0) { return 0; } @@ -5911,7 +5917,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) /* SETUP_WITH pushes a finally block. */ USE_LABEL(c, block); - if (!compiler_push_fblock(c, loc, WITH, block, final, s)) { + if (compiler_push_fblock(c, loc, WITH, block, final, s) < 0) { return 0; } From 851538020ffe0ecb9675c74bef4ca0a063447be6 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:48:58 +0000 Subject: [PATCH 16/93] compiler_call_exit_with_nones returns SUCCESS/ERROR --- Python/compile.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 8f27ae1192345f..7ce6e405823a46 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2078,11 +2078,11 @@ compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label blo static int compiler_call_exit_with_nones(struct compiler *c, location loc) { - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADDOP_I(c, loc, CALL, 2); - return 1; + ADDOP_LOAD_CONST(c, loc, Py_None); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADDOP_I(c, loc, CALL, 2); + return SUCCESS; } static int @@ -2191,7 +2191,7 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, if (preserve_tos) { _ADDOP_I(c, *ploc, SWAP, 2); } - if(!compiler_call_exit_with_nones(c, *ploc)) { + if (compiler_call_exit_with_nones(c, *ploc) < 0) { return 0; } if (info->fb_type == ASYNC_WITH) { @@ -5849,8 +5849,9 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) /* For successful outcome: * call __exit__(None, None, None) */ - if(!compiler_call_exit_with_nones(c, loc)) + if (compiler_call_exit_with_nones(c, loc) < 0) { return 0; + } _ADDOP_I(c, loc, GET_AWAITABLE, 2); _ADDOP_LOAD_CONST(c, loc, Py_None); _ADD_YIELD_FROM(c, loc, 1); @@ -5945,8 +5946,9 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) * call __exit__(None, None, None) */ loc = LOC(s); - if (!compiler_call_exit_with_nones(c, loc)) + if (compiler_call_exit_with_nones(c, loc) < 0) { return 0; + } _ADDOP(c, loc, POP_TOP); _ADDOP_JUMP(c, loc, JUMP, exit); From 13583a34967caa1027a373db12f1267c79b35b10 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:53:42 +0000 Subject: [PATCH 17/93] pop_except_and_reraise returns SUCCESS/ERROR --- Python/compile.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7ce6e405823a46..28a09f63369c80 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1673,10 +1673,8 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return ERROR; \ } -#define POP_EXCEPT_AND_RERAISE(C, LOC) { \ - if (!compiler_pop_except_and_reraise((C), (LOC))) \ - return ERROR; \ -} +#define POP_EXCEPT_AND_RERAISE(C, LOC) \ + RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC))); #define ADDOP_YIELD(C, LOC) { \ if (!addop_yield((C), (LOC))) \ @@ -1795,8 +1793,10 @@ cfg_builder_addop_j(cfg_builder *g, location loc, #define _ADD_YIELD_FROM(C, LOC, await) \ RETURN_IF_FALSE(compiler_add_yield_from((C), (LOC), (await))) -#define _POP_EXCEPT_AND_RERAISE(C, LOC) \ - RETURN_IF_FALSE(compiler_pop_except_and_reraise((C), (LOC))) +#define _POP_EXCEPT_AND_RERAISE(C, LOC) { \ + if (compiler_pop_except_and_reraise((C), (LOC)) < 0) \ + return 0; \ +} #define _ADDOP_YIELD(C, LOC) \ RETURN_IF_FALSE(addop_yield((C), (LOC))) @@ -2119,10 +2119,10 @@ compiler_pop_except_and_reraise(struct compiler *c, location loc) * (exception_unwind clears the stack) */ - _ADDOP_I(c, loc, COPY, 3); - _ADDOP(c, loc, POP_EXCEPT); - _ADDOP_I(c, loc, RERAISE, 1); - return 1; + ADDOP_I(c, loc, COPY, 3); + ADDOP(c, loc, POP_EXCEPT); + ADDOP_I(c, loc, RERAISE, 1); + return SUCCESS; } /* Unwind a frame block. If preserve_tos is true, the TOS before From 1fd574a3fe2257ef8507879703fa2663b891f1cb Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 17:57:23 +0000 Subject: [PATCH 18/93] compiler_add_yield_from returns SUCCESS/ERROR --- Python/compile.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 28a09f63369c80..4a4b030e3ee321 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1668,10 +1668,8 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return ERROR; \ } -#define ADD_YIELD_FROM(C, LOC, await) { \ - if (! compiler_add_yield_from((C), (LOC), (await))) \ - return ERROR; \ -} +#define ADD_YIELD_FROM(C, LOC, await) \ + RETURN_IF_ERROR(compiler_add_yield_from((C), (LOC), (await))); #define POP_EXCEPT_AND_RERAISE(C, LOC) \ RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC))); @@ -1790,8 +1788,10 @@ cfg_builder_addop_j(cfg_builder *g, location loc, #define _ADDOP_INPLACE(C, LOC, BINOP) \ RETURN_IF_FALSE(addop_binary((C), (LOC), (BINOP), true)) -#define _ADD_YIELD_FROM(C, LOC, await) \ - RETURN_IF_FALSE(compiler_add_yield_from((C), (LOC), (await))) +#define _ADD_YIELD_FROM(C, LOC, await) { \ + if (compiler_add_yield_from((C), (LOC), (await)) < 0) \ + return 0; \ +} #define _POP_EXCEPT_AND_RERAISE(C, LOC) { \ if (compiler_pop_except_and_reraise((C), (LOC)) < 0) \ @@ -2093,20 +2093,20 @@ compiler_add_yield_from(struct compiler *c, location loc, int await) NEW_JUMP_TARGET_LABEL(c, exit); USE_LABEL(c, send); - _ADDOP_JUMP(c, loc, SEND, exit); + ADDOP_JUMP(c, loc, SEND, exit); // Set up a virtual try/except to handle when StopIteration is raised during // a close or throw call. The only way YIELD_VALUE raises if they do! - _ADDOP_JUMP(c, loc, SETUP_FINALLY, fail); - _ADDOP_I(c, loc, YIELD_VALUE, 0); - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP_I(c, loc, RESUME, await ? 3 : 2); - _ADDOP_JUMP(c, loc, JUMP_NO_INTERRUPT, send); + ADDOP_JUMP(c, loc, SETUP_FINALLY, fail); + ADDOP_I(c, loc, YIELD_VALUE, 0); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP_I(c, loc, RESUME, await ? 3 : 2); + ADDOP_JUMP(c, loc, JUMP_NO_INTERRUPT, send); USE_LABEL(c, fail); - _ADDOP(c, loc, CLEANUP_THROW); + ADDOP(c, loc, CLEANUP_THROW); USE_LABEL(c, exit); - return 1; + return SUCCESS; } static int From a3d5cad2c845def8cfe2bdb8fe3ec1a4e4e58735 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:02:13 +0000 Subject: [PATCH 19/93] compiler_unwind_fblock returns SUCCESS/ERROR --- Python/compile.c | 70 ++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 4a4b030e3ee321..006df4ed609e8e 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2139,30 +2139,30 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, case EXCEPTION_HANDLER: case EXCEPTION_GROUP_HANDLER: case ASYNC_COMPREHENSION_GENERATOR: - return 1; + return SUCCESS; case FOR_LOOP: /* Pop the iterator */ if (preserve_tos) { - _ADDOP_I(c, *ploc, SWAP, 2); + ADDOP_I(c, *ploc, SWAP, 2); } - _ADDOP(c, *ploc, POP_TOP); - return 1; + ADDOP(c, *ploc, POP_TOP); + return SUCCESS; case TRY_EXCEPT: - _ADDOP(c, *ploc, POP_BLOCK); - return 1; + ADDOP(c, *ploc, POP_BLOCK); + return SUCCESS; case FINALLY_TRY: /* This POP_BLOCK gets the line number of the unwinding statement */ - _ADDOP(c, *ploc, POP_BLOCK); + ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { if (compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL) < 0) { - return 0; + return ERROR; } } /* Emit the finally block */ - _VISIT_SEQ(c, stmt, info->fb_datum); + VISIT_SEQ(c, stmt, info->fb_datum); if (preserve_tos) { compiler_pop_fblock(c, POP_VALUE, NO_LABEL); } @@ -2170,64 +2170,64 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, * statement causing the unwinding, so make the unwinding * instruction artificial */ *ploc = NO_LOCATION; - return 1; + return SUCCESS; case FINALLY_END: if (preserve_tos) { - _ADDOP_I(c, *ploc, SWAP, 2); + ADDOP_I(c, *ploc, SWAP, 2); } - _ADDOP(c, *ploc, POP_TOP); /* exc_value */ + ADDOP(c, *ploc, POP_TOP); /* exc_value */ if (preserve_tos) { - _ADDOP_I(c, *ploc, SWAP, 2); + ADDOP_I(c, *ploc, SWAP, 2); } - _ADDOP(c, *ploc, POP_BLOCK); - _ADDOP(c, *ploc, POP_EXCEPT); - return 1; + ADDOP(c, *ploc, POP_BLOCK); + ADDOP(c, *ploc, POP_EXCEPT); + return SUCCESS; case WITH: case ASYNC_WITH: *ploc = LOC((stmt_ty)info->fb_datum); - _ADDOP(c, *ploc, POP_BLOCK); + ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { - _ADDOP_I(c, *ploc, SWAP, 2); + ADDOP_I(c, *ploc, SWAP, 2); } if (compiler_call_exit_with_nones(c, *ploc) < 0) { - return 0; + return ERROR; } if (info->fb_type == ASYNC_WITH) { - _ADDOP_I(c, *ploc, GET_AWAITABLE, 2); - _ADDOP_LOAD_CONST(c, *ploc, Py_None); - _ADD_YIELD_FROM(c, *ploc, 1); + ADDOP_I(c, *ploc, GET_AWAITABLE, 2); + ADDOP_LOAD_CONST(c, *ploc, Py_None); + ADD_YIELD_FROM(c, *ploc, 1); } - _ADDOP(c, *ploc, POP_TOP); + ADDOP(c, *ploc, POP_TOP); /* The exit block should appear to execute after the * statement causing the unwinding, so make the unwinding * instruction artificial */ *ploc = NO_LOCATION; - return 1; + return SUCCESS; case HANDLER_CLEANUP: { if (info->fb_datum) { - _ADDOP(c, *ploc, POP_BLOCK); + ADDOP(c, *ploc, POP_BLOCK); } if (preserve_tos) { - _ADDOP_I(c, *ploc, SWAP, 2); + ADDOP_I(c, *ploc, SWAP, 2); } - _ADDOP(c, *ploc, POP_BLOCK); - _ADDOP(c, *ploc, POP_EXCEPT); + ADDOP(c, *ploc, POP_BLOCK); + ADDOP(c, *ploc, POP_EXCEPT); if (info->fb_datum) { - _ADDOP_LOAD_CONST(c, *ploc, Py_None); + ADDOP_LOAD_CONST(c, *ploc, Py_None); compiler_nameop(c, *ploc, info->fb_datum, Store); compiler_nameop(c, *ploc, info->fb_datum, Del); } - return 1; + return SUCCESS; } case POP_VALUE: { if (preserve_tos) { - _ADDOP_I(c, *ploc, SWAP, 2); + ADDOP_I(c, *ploc, SWAP, 2); } - _ADDOP(c, *ploc, POP_TOP); - return 1; + ADDOP(c, *ploc, POP_TOP); + return SUCCESS; } } Py_UNREACHABLE(); @@ -2252,7 +2252,7 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc, } struct fblockinfo copy = *top; c->u->u_nfblocks--; - if (!compiler_unwind_fblock(c, ploc, ©, preserve_tos)) { + if (compiler_unwind_fblock(c, ploc, ©, preserve_tos) < 0) { return 0; } if (!compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop)) { @@ -3437,7 +3437,7 @@ compiler_break(struct compiler *c, location loc) if (loop == NULL) { return compiler_error(c, loc, "'break' outside loop"); } - if (!compiler_unwind_fblock(c, &loc, loop, 0)) { + if (compiler_unwind_fblock(c, &loc, loop, 0) < 0) { return 0; } _ADDOP_JUMP(c, loc, JUMP, loop->fb_exit); From 6fe5dd786a387893539f8a486db93100136d8fab Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:06:37 +0000 Subject: [PATCH 20/93] compiler_unwind_fblock_stack returns SUCCESS/ERROR --- Python/compile.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 006df4ed609e8e..5ae46fe978b64c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2239,28 +2239,27 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc, int preserve_tos, struct fblockinfo **loop) { if (c->u->u_nfblocks == 0) { - return 1; + return SUCCESS; } struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; if (top->fb_type == EXCEPTION_GROUP_HANDLER) { - return compiler_error( - c, *ploc, "'break', 'continue' and 'return' cannot appear in an except* block"); + compiler_error(c, *ploc, + "'break', 'continue' and 'return' cannot appear in an except* block"); + return ERROR; } if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { *loop = top; - return 1; + return SUCCESS; } struct fblockinfo copy = *top; c->u->u_nfblocks--; if (compiler_unwind_fblock(c, ploc, ©, preserve_tos) < 0) { - return 0; - } - if (!compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop)) { - return 0; + return ERROR; } + RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop)); c->u->u_fblock[c->u->u_nfblocks] = copy; c->u->u_nfblocks++; - return 1; + return SUCCESS; } /* Compile a sequence of statements, checking for a docstring @@ -3412,8 +3411,9 @@ compiler_return(struct compiler *c, stmt_ty s) _ADDOP(c, loc, NOP); } - if (!compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL)) + if (compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL) < 0) { return 0; + } if (s->v.Return.value == NULL) { _ADDOP_LOAD_CONST(c, loc, Py_None); } @@ -3431,7 +3431,7 @@ compiler_break(struct compiler *c, location loc) struct fblockinfo *loop = NULL; /* Emit instruction with line number */ _ADDOP(c, loc, NOP); - if (!compiler_unwind_fblock_stack(c, &loc, 0, &loop)) { + if (compiler_unwind_fblock_stack(c, &loc, 0, &loop) < 0) { return 0; } if (loop == NULL) { @@ -3450,7 +3450,7 @@ compiler_continue(struct compiler *c, location loc) struct fblockinfo *loop = NULL; /* Emit instruction with line number */ _ADDOP(c, loc, NOP); - if (!compiler_unwind_fblock_stack(c, &loc, 0, &loop)) { + if (compiler_unwind_fblock_stack(c, &loc, 0, &loop) < 0) { return 0; } if (loop == NULL) { From 297a0d69998119c275372db5763235c44343b099 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:20:34 +0000 Subject: [PATCH 21/93] compiler_body returns SUCCESS/ERROR --- Python/compile.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 5ae46fe978b64c..9e0c96febe6683 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2282,10 +2282,11 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts) } /* Every annotated class and module should have __annotations__. */ if (find_ann(stmts)) { - _ADDOP(c, loc, SETUP_ANNOTATIONS); + ADDOP(c, loc, SETUP_ANNOTATIONS); + } + if (!asdl_seq_LEN(stmts)) { + return SUCCESS; } - if (!asdl_seq_LEN(stmts)) - return 1; /* if not -OO mode, set docstring */ if (c->c_optimize < 2) { docstring = _PyAST_GetDocString(stmts); @@ -2293,14 +2294,16 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts) i = 1; st = (stmt_ty)asdl_seq_GET(stmts, 0); assert(st->kind == Expr_kind); - _VISIT(c, expr, st->v.Expr.value); - if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store)) - return 0; + VISIT(c, expr, st->v.Expr.value); + if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store)) { + return ERROR; + } } } - for (; i < asdl_seq_LEN(stmts); i++) - _VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); - return 1; + for (; i < asdl_seq_LEN(stmts); i++) { + VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); + } + return SUCCESS; } static int @@ -2314,7 +2317,7 @@ compiler_codegen(struct compiler *c, mod_ty mod) location loc = LOCATION(1, 1, 0, 0); switch (mod->kind) { case Module_kind: - if (!compiler_body(c, loc, mod->v.Module.body)) { + if (compiler_body(c, loc, mod->v.Module.body) < 0) { compiler_exit_scope(c); return 0; } @@ -2908,7 +2911,7 @@ compiler_class(struct compiler *c, stmt_ty s) return 0; } /* compile the body proper */ - if (!compiler_body(c, loc, s->v.ClassDef.body)) { + if (compiler_body(c, loc, s->v.ClassDef.body) < 0) { compiler_exit_scope(c); return 0; } From c504cd37ed0d07dfc2c9100d50000cb13bf54b16 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:25:05 +0000 Subject: [PATCH 22/93] compiler_codegen returns SUCCESS/ERROR --- Python/compile.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 9e0c96febe6683..24d7760c066f03 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2310,42 +2310,42 @@ static int compiler_codegen(struct compiler *c, mod_ty mod) { _Py_DECLARE_STR(anon_module, "<module>"); - if (compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, - mod, 1) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, + mod, 1)); + location loc = LOCATION(1, 1, 0, 0); switch (mod->kind) { case Module_kind: if (compiler_body(c, loc, mod->v.Module.body) < 0) { compiler_exit_scope(c); - return 0; + return ERROR; } break; case Interactive_kind: if (find_ann(mod->v.Interactive.body)) { - _ADDOP(c, loc, SETUP_ANNOTATIONS); + ADDOP(c, loc, SETUP_ANNOTATIONS); } c->c_interactive = 1; - _VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); + VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); break; case Expression_kind: - _VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); + VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); break; default: PyErr_Format(PyExc_SystemError, "module kind %d should not be possible", mod->kind); - return 0; + return ERROR; } - return 1; + return SUCCESS; } static PyCodeObject * compiler_mod(struct compiler *c, mod_ty mod) { int addNone = mod->kind != Expression_kind; - if (!compiler_codegen(c, mod)) { + if (compiler_codegen(c, mod) < 0) { return NULL; } PyCodeObject *co = assemble(c, addNone); @@ -10227,7 +10227,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags, return NULL; } - if (!compiler_codegen(c, mod)) { + if (compiler_codegen(c, mod) < 0) { goto finally; } From b6028e22b5a242befaa718d8e6a4ab0aaf47eac4 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:36:44 +0000 Subject: [PATCH 23/93] compiler_make_closure returns SUCCESS/ERROR --- Python/compile.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 24d7760c066f03..6a50644ed0cfa9 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2412,7 +2412,7 @@ compiler_make_closure(struct compiler *c, location loc, */ int reftype = get_ref_type(c, name); if (reftype == -1) { - return 0; + return ERROR; } int arg; if (reftype == CELL) { @@ -2435,16 +2435,16 @@ compiler_make_closure(struct compiler *c, location loc, co->co_name, freevars); Py_DECREF(freevars); - return 0; + return ERROR; } - _ADDOP_I(c, loc, LOAD_CLOSURE, arg); + ADDOP_I(c, loc, LOAD_CLOSURE, arg); } flags |= 0x08; - _ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars); + ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars); } - _ADDOP_LOAD_CONST(c, loc, (PyObject*)co); - _ADDOP_I(c, loc, MAKE_FUNCTION, flags); - return 1; + ADDOP_LOAD_CONST(c, loc, (PyObject*)co); + ADDOP_I(c, loc, MAKE_FUNCTION, flags); + return SUCCESS; } static int @@ -2846,7 +2846,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) Py_XDECREF(co); return 0; } - if (!compiler_make_closure(c, loc, co, funcflags, qualname)) { + if (compiler_make_closure(c, loc, co, funcflags, qualname) < 0) { Py_DECREF(qualname); Py_DECREF(co); return 0; @@ -2952,7 +2952,7 @@ compiler_class(struct compiler *c, stmt_ty s) _ADDOP(c, loc, LOAD_BUILD_CLASS); /* 3. load a function (or closure) made from the code object */ - if (!compiler_make_closure(c, loc, co, 0, NULL)) { + if (compiler_make_closure(c, loc, co, 0, NULL) < 0) { Py_DECREF(co); return 0; } @@ -3226,7 +3226,7 @@ compiler_lambda(struct compiler *c, expr_ty e) return 0; } - if (!compiler_make_closure(c, loc, co, funcflags, qualname)) { + if (compiler_make_closure(c, loc, co, funcflags, qualname) < 0) { Py_DECREF(qualname); Py_DECREF(co); return 0; @@ -5665,7 +5665,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, goto error; loc = LOC(e); - if (!compiler_make_closure(c, loc, co, 0, qualname)) { + if (compiler_make_closure(c, loc, co, 0, qualname) < 0) { goto error; } Py_DECREF(qualname); From 19235f78f57343a064c47c7d76e8c91e5e79e9c7 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:43:01 +0000 Subject: [PATCH 24/93] compiler_decorators and compiler_apply_decorators return SUCCESS/ERROR --- Python/compile.c | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 6a50644ed0cfa9..9eeef654a50f0c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2450,28 +2450,28 @@ compiler_make_closure(struct compiler *c, location loc, static int compiler_decorators(struct compiler *c, asdl_expr_seq* decos) { - int i; - - if (!decos) - return 1; + if (!decos) { + return SUCCESS; + } - for (i = 0; i < asdl_seq_LEN(decos); i++) { - _VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); + for (Py_ssize_t i = 0; i < asdl_seq_LEN(decos); i++) { + VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); } - return 1; + return SUCCESS; } static int compiler_apply_decorators(struct compiler *c, asdl_expr_seq* decos) { - if (!decos) - return 1; + if (!decos) { + return SUCCESS; + } for (Py_ssize_t i = asdl_seq_LEN(decos) - 1; i > -1; i--) { location loc = LOC((expr_ty)asdl_seq_GET(decos, i)); - _ADDOP_I(c, loc, CALL, 0); + ADDOP_I(c, loc, CALL, 0); } - return 1; + return SUCCESS; } static int @@ -2792,8 +2792,9 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) if (!compiler_check_debug_args(c, args)) return 0; - if (!compiler_decorators(c, decos)) + if (compiler_decorators(c, decos) < 0) { return 0; + } firstlineno = s->lineno; if (asdl_seq_LEN(decos)) { @@ -2854,8 +2855,9 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) Py_DECREF(qualname); Py_DECREF(co); - if (!compiler_apply_decorators(c, decos)) + if (compiler_apply_decorators(c, decos) < 0) { return 0; + } return compiler_nameop(c, loc, name, Store); } @@ -2866,8 +2868,9 @@ compiler_class(struct compiler *c, stmt_ty s) int i, firstlineno; asdl_expr_seq *decos = s->v.ClassDef.decorator_list; - if (!compiler_decorators(c, decos)) + if (compiler_decorators(c, decos) < 0) { return 0; + } firstlineno = s->lineno; if (asdl_seq_LEN(decos)) { @@ -2967,8 +2970,9 @@ compiler_class(struct compiler *c, stmt_ty s) s->v.ClassDef.keywords)) return 0; /* 6. apply decorators */ - if (!compiler_apply_decorators(c, decos)) + if (compiler_apply_decorators(c, decos) < 0) { return 0; + } /* 7. store into <name> */ if (!compiler_nameop(c, loc, s->v.ClassDef.name, Store)) From 9d936351c23e26cf548438e7f0485b5ebfac3be4 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:47:19 +0000 Subject: [PATCH 25/93] compiler_visit_kwonlydefaults returns SUCCESS/ERROR --- Python/compile.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 9eeef654a50f0c..4221da7877bd24 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2480,7 +2480,7 @@ compiler_visit_kwonlydefaults(struct compiler *c, location loc, { /* Push a dict of keyword-only default values. - Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. + Return -1 on error, 0 if no dict pushed, 1 if a dict is pushed. */ int i; PyObject *keys = NULL; @@ -2497,7 +2497,7 @@ compiler_visit_kwonlydefaults(struct compiler *c, location loc, keys = PyList_New(1); if (keys == NULL) { Py_DECREF(mangled); - return 0; + return ERROR; } PyList_SET_ITEM(keys, 0, mangled); } @@ -2523,12 +2523,12 @@ compiler_visit_kwonlydefaults(struct compiler *c, location loc, return 1; } else { - return -1; + return 0; } error: Py_XDECREF(keys); - return 0; + return ERROR; } static int @@ -2653,8 +2653,8 @@ compiler_default_arguments(struct compiler *c, location loc, int res = compiler_visit_kwonlydefaults(c, loc, args->kwonlyargs, args->kw_defaults); - if (res == 0) { - return -1; + if (res < 0) { + return ERROR; } else if (res > 0) { funcflags |= 0x02; From dfbe826be76b6cc07c3f5d626af88ba5b1fae066 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:49:44 +0000 Subject: [PATCH 26/93] forbidden_name returns bool --- Python/compile.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 4221da7877bd24..e803160feb64c2 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2663,27 +2663,28 @@ compiler_default_arguments(struct compiler *c, location loc, return funcflags; } -static int +static bool forbidden_name(struct compiler *c, location loc, identifier name, expr_context_ty ctx) { if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { compiler_error(c, loc, "cannot assign to __debug__"); - return 1; + return true; } if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) { compiler_error(c, loc, "cannot delete __debug__"); - return 1; + return true; } - return 0; + return false; } static int compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) { if (arg != NULL) { - if (forbidden_name(c, LOC(arg), arg->arg, Store)) + if (forbidden_name(c, LOC(arg), arg->arg, Store)) { return 0; + } } return 1; } @@ -4394,8 +4395,9 @@ compiler_nameop(struct compiler *c, location loc, !_PyUnicode_EqualToASCIIString(name, "True") && !_PyUnicode_EqualToASCIIString(name, "False")); - if (forbidden_name(c, loc, name, ctx)) + if (forbidden_name(c, loc, name, ctx)) { return 0; + } mangled = _Py_Mangle(c->u->u_private, name); if (!mangled) @@ -6278,8 +6280,9 @@ compiler_annassign(struct compiler *c, stmt_ty s) } switch (targ->kind) { case Name_kind: - if (forbidden_name(c, loc, targ->v.Name.id, Store)) + if (forbidden_name(c, loc, targ->v.Name.id, Store)) { return 0; + } /* If we have a simple name in a module or class, store annotation. */ if (s->v.AnnAssign.simple && (c->u->u_scope_type == COMPILER_SCOPE_MODULE || @@ -6297,8 +6300,9 @@ compiler_annassign(struct compiler *c, stmt_ty s) } break; case Attribute_kind: - if (forbidden_name(c, loc, targ->v.Attribute.attr, Store)) + if (forbidden_name(c, loc, targ->v.Attribute.attr, Store)) { return 0; + } if (!s->v.AnnAssign.value && !check_ann_expr(c, targ->v.Attribute.value)) { return 0; From 9757b0dac536bf9f493bd179c4eb5240a85d1489 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 18:58:42 +0000 Subject: [PATCH 27/93] compiler_check_debug_one_arg, compiler_check_debug_args, compiler_check_debug_args_seq return SUCCESS/ERROR --- Python/compile.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e803160feb64c2..5b66e513dbdd26 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2683,10 +2683,10 @@ compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) { if (arg != NULL) { if (forbidden_name(c, LOC(arg), arg->arg, Store)) { - return 0; + return ERROR; } } - return 1; + return SUCCESS; } static int @@ -2694,27 +2694,22 @@ compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) { if (args != NULL) { for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) { - if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) - return 0; + RETURN_IF_ERROR( + compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))); } } - return 1; + return SUCCESS; } static int compiler_check_debug_args(struct compiler *c, arguments_ty args) { - if (!compiler_check_debug_args_seq(c, args->posonlyargs)) - return 0; - if (!compiler_check_debug_args_seq(c, args->args)) - return 0; - if (!compiler_check_debug_one_arg(c, args->vararg)) - return 0; - if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) - return 0; - if (!compiler_check_debug_one_arg(c, args->kwarg)) - return 0; - return 1; + RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->posonlyargs)); + RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->args)); + RETURN_IF_ERROR(compiler_check_debug_one_arg(c, args->vararg)); + RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->kwonlyargs)); + RETURN_IF_ERROR(compiler_check_debug_one_arg(c, args->kwarg)); + return SUCCESS; } static inline int @@ -2790,8 +2785,9 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) scope_type = COMPILER_SCOPE_FUNCTION; } - if (!compiler_check_debug_args(c, args)) + if (compiler_check_debug_args(c, args) < 0) { return 0; + } if (compiler_decorators(c, decos) < 0) { return 0; @@ -3193,8 +3189,9 @@ compiler_lambda(struct compiler *c, expr_ty e) arguments_ty args = e->v.Lambda.args; assert(e->kind == Lambda_kind); - if (!compiler_check_debug_args(c, args)) + if (compiler_check_debug_args(c, args) < 0) { return 0; + } location loc = LOC(e); funcflags = compiler_default_arguments(c, loc, args); From 151e8f03bf3f3347b26b90b44a05bc7f1a701cb3 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:03:42 +0000 Subject: [PATCH 28/93] insert_instruction, wrap_in_stopiteration_handler return SUCCESS/ERROR --- Python/compile.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 5b66e513dbdd26..238afd3dbfb94f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2714,14 +2714,12 @@ compiler_check_debug_args(struct compiler *c, arguments_ty args) static inline int insert_instruction(basicblock *block, int pos, struct instr *instr) { - if (basicblock_next_instr(block) < 0) { - return -1; - } + RETURN_IF_ERROR(basicblock_next_instr(block)); for (int i = block->b_iused - 1; i > pos; i--) { block->b_instr[i] = block->b_instr[i-1]; } block->b_instr[pos] = *instr; - return 0; + return SUCCESS; } static int @@ -2736,16 +2734,15 @@ wrap_in_stopiteration_handler(struct compiler *c) .i_loc = NO_LOCATION, .i_target = NULL, }; - if (insert_instruction(c->u->u_cfg_builder.g_entryblock, 0, &setup)) { - return 0; - } + RETURN_IF_ERROR( + insert_instruction(c->u->u_cfg_builder.g_entryblock, 0, &setup)); - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - _ADDOP(c, NO_LOCATION, RETURN_VALUE); + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + ADDOP(c, NO_LOCATION, RETURN_VALUE); USE_LABEL(c, handler); - _ADDOP(c, NO_LOCATION, STOPITERATION_ERROR); - _ADDOP_I(c, NO_LOCATION, RERAISE, 1); - return 1; + ADDOP(c, NO_LOCATION, STOPITERATION_ERROR); + ADDOP_I(c, NO_LOCATION, RERAISE, 1); + return SUCCESS; } static int @@ -2831,7 +2828,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) _VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); } if (c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator) { - if (!wrap_in_stopiteration_handler(c)) { + if (wrap_in_stopiteration_handler(c) < 0) { compiler_exit_scope(c); return 0; } @@ -5653,7 +5650,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, _ADDOP(c, LOC(e), RETURN_VALUE); } if (type == COMP_GENEXP) { - if (!wrap_in_stopiteration_handler(c)) { + if (wrap_in_stopiteration_handler(c) < 0) { goto error_in_scope; } } From 1409c3ece639653d331989055eedb209814d28ee Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:17:31 +0000 Subject: [PATCH 29/93] compiler_class returns SUCCESS/ERROR --- Python/compile.c | 65 ++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 238afd3dbfb94f..c5b68bf32adbcb 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2862,9 +2862,7 @@ compiler_class(struct compiler *c, stmt_ty s) int i, firstlineno; asdl_expr_seq *decos = s->v.ClassDef.decorator_list; - if (compiler_decorators(c, decos) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_decorators(c, decos)); firstlineno = s->lineno; if (asdl_seq_LEN(decos)) { @@ -2882,10 +2880,10 @@ compiler_class(struct compiler *c, stmt_ty s) This borrows from compiler_call. */ /* 1. compile the class body into a code object */ - if (compiler_enter_scope(c, s->v.ClassDef.name, - COMPILER_SCOPE_CLASS, (void *)s, firstlineno) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_enter_scope(c, s->v.ClassDef.name, + COMPILER_SCOPE_CLASS, (void *)s, firstlineno)); + /* this block represents what we do in the new scope */ { location loc = LOCATION(firstlineno, firstlineno, 0, 0); @@ -2894,23 +2892,23 @@ compiler_class(struct compiler *c, stmt_ty s) /* load (global) __name__ ... */ if (!compiler_nameop(c, loc, &_Py_ID(__name__), Load)) { compiler_exit_scope(c); - return 0; + return ERROR; } /* ... and store it as __module__ */ if (!compiler_nameop(c, loc, &_Py_ID(__module__), Store)) { compiler_exit_scope(c); - return 0; + return ERROR; } assert(c->u->u_qualname); - _ADDOP_LOAD_CONST(c, loc, c->u->u_qualname); + ADDOP_LOAD_CONST(c, loc, c->u->u_qualname); if (!compiler_nameop(c, loc, &_Py_ID(__qualname__), Store)) { compiler_exit_scope(c); - return 0; + return ERROR; } /* compile the body proper */ if (compiler_body(c, loc, s->v.ClassDef.body) < 0) { compiler_exit_scope(c); - return 0; + return ERROR; } /* The following code is artificial */ /* Return __classcell__ if it is referenced, otherwise return None */ @@ -2919,59 +2917,60 @@ compiler_class(struct compiler *c, stmt_ty s) i = compiler_lookup_arg(c->u->u_cellvars, &_Py_ID(__class__)); if (i < 0) { compiler_exit_scope(c); - return 0; + return ERROR; } assert(i == 0); - _ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i); - _ADDOP_I(c, NO_LOCATION, COPY, 1); + ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i); + ADDOP_I(c, NO_LOCATION, COPY, 1); if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store)) { compiler_exit_scope(c); - return 0; + return ERROR; } } else { /* No methods referenced __class__, so just return None */ assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); } - _ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE); + ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE); /* create the code object */ co = assemble(c, 1); } /* leave the new scope */ compiler_exit_scope(c); - if (co == NULL) - return 0; + if (co == NULL) { + return ERROR; + } location loc = LOC(s); /* 2. load the 'build_class' function */ - _ADDOP(c, loc, PUSH_NULL); - _ADDOP(c, loc, LOAD_BUILD_CLASS); + ADDOP(c, loc, PUSH_NULL); + ADDOP(c, loc, LOAD_BUILD_CLASS); /* 3. load a function (or closure) made from the code object */ if (compiler_make_closure(c, loc, co, 0, NULL) < 0) { Py_DECREF(co); - return 0; + return ERROR; } Py_DECREF(co); /* 4. load class name */ - _ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name); + ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name); /* 5. generate the rest of the code for the call */ if (!compiler_call_helper(c, loc, 2, s->v.ClassDef.bases, - s->v.ClassDef.keywords)) - return 0; - /* 6. apply decorators */ - if (compiler_apply_decorators(c, decos) < 0) { - return 0; + s->v.ClassDef.keywords)) { + return ERROR; } + /* 6. apply decorators */ + RETURN_IF_ERROR(compiler_apply_decorators(c, decos)); /* 7. store into <name> */ - if (!compiler_nameop(c, loc, s->v.ClassDef.name, Store)) - return 0; - return 1; + if (!compiler_nameop(c, loc, s->v.ClassDef.name, Store)) { + return ERROR; + } + return SUCCESS; } /* Return 0 if the expression is a constant value except named singletons. @@ -4202,7 +4201,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case FunctionDef_kind: return compiler_function(c, s, 0); case ClassDef_kind: - return compiler_class(c, s); + return compiler_class(c, s) == SUCCESS ? 1 : 0; case Return_kind: return compiler_return(c, s); case Delete_kind: From d1a28e451ed07ff9a14c63cbfe07595fde3520b9 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:21:08 +0000 Subject: [PATCH 30/93] compiler_return returns SUCCESS/ERROR --- Python/compile.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index c5b68bf32adbcb..2df9f1f41855c6 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3389,41 +3389,41 @@ compiler_return(struct compiler *c, stmt_ty s) location loc = LOC(s); int preserve_tos = ((s->v.Return.value != NULL) && (s->v.Return.value->kind != Constant_kind)); - if (c->u->u_ste->ste_type != FunctionBlock) - return compiler_error(c, loc, "'return' outside function"); + if (c->u->u_ste->ste_type != FunctionBlock) { + compiler_error(c, loc, "'return' outside function"); + return ERROR; + } if (s->v.Return.value != NULL && c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) { - return compiler_error( - c, loc, "'return' with value in async generator"); + compiler_error(c, loc, "'return' with value in async generator"); + return ERROR; } if (preserve_tos) { - _VISIT(c, expr, s->v.Return.value); + VISIT(c, expr, s->v.Return.value); } else { /* Emit instruction with line number for return value */ if (s->v.Return.value != NULL) { loc = LOC(s->v.Return.value); - _ADDOP(c, loc, NOP); + ADDOP(c, loc, NOP); } } if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) { loc = LOC(s); - _ADDOP(c, loc, NOP); + ADDOP(c, loc, NOP); } - if (compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL)); if (s->v.Return.value == NULL) { - _ADDOP_LOAD_CONST(c, loc, Py_None); + ADDOP_LOAD_CONST(c, loc, Py_None); } else if (!preserve_tos) { - _ADDOP_LOAD_CONST(c, loc, s->v.Return.value->v.Constant.value); + ADDOP_LOAD_CONST(c, loc, s->v.Return.value->v.Constant.value); } - _ADDOP(c, loc, RETURN_VALUE); + ADDOP(c, loc, RETURN_VALUE); - return 1; + return SUCCESS; } static int @@ -4203,7 +4203,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case ClassDef_kind: return compiler_class(c, s) == SUCCESS ? 1 : 0; case Return_kind: - return compiler_return(c, s); + return compiler_return(c, s) == SUCCESS ? 1 : 0; case Delete_kind: _VISIT_SEQ(c, expr, s->v.Delete.targets) break; From 15b73da4befa227043e4908d5af5d33873b1b10f Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:25:12 +0000 Subject: [PATCH 31/93] compiler_augassign return SUCCESS/ERROR --- Python/compile.c | 58 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 2df9f1f41855c6..55366947e8f7e4 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4221,7 +4221,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) break; } case AugAssign_kind: - return compiler_augassign(c, s); + return compiler_augassign(c, s) == SUCCESS ? 1 : 0; case AnnAssign_kind: return compiler_annassign(c, s); case For_kind: @@ -6132,72 +6132,72 @@ compiler_augassign(struct compiler *c, stmt_ty s) switch (e->kind) { case Attribute_kind: - _VISIT(c, expr, e->v.Attribute.value); - _ADDOP_I(c, loc, COPY, 1); + VISIT(c, expr, e->v.Attribute.value); + ADDOP_I(c, loc, COPY, 1); loc = update_start_location_to_match_attr(c, loc, e); - _ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); + ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); break; case Subscript_kind: - _VISIT(c, expr, e->v.Subscript.value); + VISIT(c, expr, e->v.Subscript.value); if (is_two_element_slice(e->v.Subscript.slice)) { if (!compiler_slice(c, e->v.Subscript.slice)) { - return 0; + return ERROR; } - _ADDOP_I(c, loc, COPY, 3); - _ADDOP_I(c, loc, COPY, 3); - _ADDOP_I(c, loc, COPY, 3); - _ADDOP(c, loc, BINARY_SLICE); + ADDOP_I(c, loc, COPY, 3); + ADDOP_I(c, loc, COPY, 3); + ADDOP_I(c, loc, COPY, 3); + ADDOP(c, loc, BINARY_SLICE); } else { - _VISIT(c, expr, e->v.Subscript.slice); - _ADDOP_I(c, loc, COPY, 2); - _ADDOP_I(c, loc, COPY, 2); - _ADDOP(c, loc, BINARY_SUBSCR); + VISIT(c, expr, e->v.Subscript.slice); + ADDOP_I(c, loc, COPY, 2); + ADDOP_I(c, loc, COPY, 2); + ADDOP(c, loc, BINARY_SUBSCR); } break; case Name_kind: if (!compiler_nameop(c, loc, e->v.Name.id, Load)) - return 0; + return ERROR; break; default: PyErr_Format(PyExc_SystemError, "invalid node type (%d) for augmented assignment", e->kind); - return 0; + return ERROR; } loc = LOC(s); - _VISIT(c, expr, s->v.AugAssign.value); - _ADDOP_INPLACE(c, loc, s->v.AugAssign.op); + VISIT(c, expr, s->v.AugAssign.value); + ADDOP_INPLACE(c, loc, s->v.AugAssign.op); loc = LOC(e); switch (e->kind) { case Attribute_kind: loc = update_start_location_to_match_attr(c, loc, e); - _ADDOP_I(c, loc, SWAP, 2); - _ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); + ADDOP_I(c, loc, SWAP, 2); + ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); break; case Subscript_kind: if (is_two_element_slice(e->v.Subscript.slice)) { - _ADDOP_I(c, loc, SWAP, 4); - _ADDOP_I(c, loc, SWAP, 3); - _ADDOP_I(c, loc, SWAP, 2); - _ADDOP(c, loc, STORE_SLICE); + ADDOP_I(c, loc, SWAP, 4); + ADDOP_I(c, loc, SWAP, 3); + ADDOP_I(c, loc, SWAP, 2); + ADDOP(c, loc, STORE_SLICE); } else { - _ADDOP_I(c, loc, SWAP, 3); - _ADDOP_I(c, loc, SWAP, 2); - _ADDOP(c, loc, STORE_SUBSCR); + ADDOP_I(c, loc, SWAP, 3); + ADDOP_I(c, loc, SWAP, 2); + ADDOP(c, loc, STORE_SUBSCR); } break; case Name_kind: - return compiler_nameop(c, loc, e->v.Name.id, Store); + return compiler_nameop(c, loc, e->v.Name.id, Store) ? SUCCESS : ERROR; default: Py_UNREACHABLE(); } - return 1; + return SUCCESS; } static int From 40615daf7830cba3e0bdf4bb476869671bc1eae5 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:28:01 +0000 Subject: [PATCH 32/93] compiler_annassign returns SUCCESS/ERROR --- Python/compile.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 55366947e8f7e4..15ed1156288fed 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4223,7 +4223,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case AugAssign_kind: return compiler_augassign(c, s) == SUCCESS ? 1 : 0; case AnnAssign_kind: - return compiler_annassign(c, s); + return compiler_annassign(c, s) == SUCCESS ? 1 : 0; case For_kind: return compiler_for(c, s); case While_kind: @@ -6268,57 +6268,57 @@ compiler_annassign(struct compiler *c, stmt_ty s) /* We perform the actual assignment first. */ if (s->v.AnnAssign.value) { - _VISIT(c, expr, s->v.AnnAssign.value); - _VISIT(c, expr, targ); + VISIT(c, expr, s->v.AnnAssign.value); + VISIT(c, expr, targ); } switch (targ->kind) { case Name_kind: if (forbidden_name(c, loc, targ->v.Name.id, Store)) { - return 0; + return ERROR; } /* If we have a simple name in a module or class, store annotation. */ if (s->v.AnnAssign.simple && (c->u->u_scope_type == COMPILER_SCOPE_MODULE || c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) { - _VISIT(c, annexpr, s->v.AnnAssign.annotation) + VISIT(c, annexpr, s->v.AnnAssign.annotation) } else { - _VISIT(c, expr, s->v.AnnAssign.annotation); + VISIT(c, expr, s->v.AnnAssign.annotation); } - _ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names); + ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names); mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); - _ADDOP_LOAD_CONST_NEW(c, loc, mangled); - _ADDOP(c, loc, STORE_SUBSCR); + ADDOP_LOAD_CONST_NEW(c, loc, mangled); + ADDOP(c, loc, STORE_SUBSCR); } break; case Attribute_kind: if (forbidden_name(c, loc, targ->v.Attribute.attr, Store)) { - return 0; + return ERROR; } if (!s->v.AnnAssign.value && !check_ann_expr(c, targ->v.Attribute.value)) { - return 0; + return ERROR; } break; case Subscript_kind: if (!s->v.AnnAssign.value && (!check_ann_expr(c, targ->v.Subscript.value) || !check_ann_subscr(c, targ->v.Subscript.slice))) { - return 0; + return ERROR; } break; default: PyErr_Format(PyExc_SystemError, "invalid node type (%d) for annotated assignment", targ->kind); - return 0; + return ERROR; } /* Annotation is evaluated last. */ if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { - return 0; + return ERROR; } - return 1; + return SUCCESS; } /* Raises a SyntaxError and returns 0. From 90bb804da327c8cec3d2b76868aa201accfc9036 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:29:39 +0000 Subject: [PATCH 33/93] compiler_for returns SUCCESS/ERROR --- Python/compile.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 15ed1156288fed..d62651a34620ac 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3272,30 +3272,29 @@ compiler_for(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup); NEW_JUMP_TARGET_LABEL(c, end); - if (compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL) < 0) { - return 0; - } - _VISIT(c, expr, s->v.For.iter); - _ADDOP(c, loc, GET_ITER); + RETURN_IF_ERROR(compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL)); + + VISIT(c, expr, s->v.For.iter); + ADDOP(c, loc, GET_ITER); USE_LABEL(c, start); - _ADDOP_JUMP(c, loc, FOR_ITER, cleanup); + ADDOP_JUMP(c, loc, FOR_ITER, cleanup); USE_LABEL(c, body); - _VISIT(c, expr, s->v.For.target); - _VISIT_SEQ(c, stmt, s->v.For.body); + VISIT(c, expr, s->v.For.target); + VISIT_SEQ(c, stmt, s->v.For.body); /* Mark jump as artificial */ - _ADDOP_JUMP(c, NO_LOCATION, JUMP, start); + ADDOP_JUMP(c, NO_LOCATION, JUMP, start); USE_LABEL(c, cleanup); - _ADDOP(c, NO_LOCATION, END_FOR); + ADDOP(c, NO_LOCATION, END_FOR); compiler_pop_fblock(c, FOR_LOOP, start); - _VISIT_SEQ(c, stmt, s->v.For.orelse); + VISIT_SEQ(c, stmt, s->v.For.orelse); USE_LABEL(c, end); - return 1; + return SUCCESS; } @@ -4225,7 +4224,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case AnnAssign_kind: return compiler_annassign(c, s) == SUCCESS ? 1 : 0; case For_kind: - return compiler_for(c, s); + return compiler_for(c, s) == SUCCESS ? 1 : 0; case While_kind: return compiler_while(c, s); case If_kind: From 42dca9270262c5983397972abb3dda0ba7ef443c Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:32:25 +0000 Subject: [PATCH 34/93] compiler_while returns SUCCESS/ERROR --- Python/compile.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index d62651a34620ac..61ecf891c22663 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3358,28 +3358,27 @@ compiler_while(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, anchor); USE_LABEL(c, loop); - if (compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL) < 0) { - return 0; - } + + RETURN_IF_ERROR(compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL)); if (!compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0)) { - return 0; + return ERROR; } USE_LABEL(c, body); - _VISIT_SEQ(c, stmt, s->v.While.body); + VISIT_SEQ(c, stmt, s->v.While.body); if (!compiler_jump_if(c, LOC(s), s->v.While.test, body, 1)) { - return 0; + return ERROR; } compiler_pop_fblock(c, WHILE_LOOP, loop); USE_LABEL(c, anchor); if (s->v.While.orelse) { - _VISIT_SEQ(c, stmt, s->v.While.orelse); + VISIT_SEQ(c, stmt, s->v.While.orelse); } USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -4226,7 +4225,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case For_kind: return compiler_for(c, s) == SUCCESS ? 1 : 0; case While_kind: - return compiler_while(c, s); + return compiler_while(c, s) == SUCCESS ? 1 : 0; case If_kind: return compiler_if(c, s); case Match_kind: From fdf615f00edaafc77282531039c356f269b8d7bf Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:35:22 +0000 Subject: [PATCH 35/93] compiler_if returns SUCCESS/ERROR --- Python/compile.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 61ecf891c22663..e696a0c231ccc7 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3249,18 +3249,18 @@ compiler_if(struct compiler *c, stmt_ty s) next = end; } if (!compiler_jump_if(c, LOC(s), s->v.If.test, next, 0)) { - return 0; + return ERROR; } - _VISIT_SEQ(c, stmt, s->v.If.body); + VISIT_SEQ(c, stmt, s->v.If.body); if (asdl_seq_LEN(s->v.If.orelse)) { - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, next); - _VISIT_SEQ(c, stmt, s->v.If.orelse); + VISIT_SEQ(c, stmt, s->v.If.orelse); } USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -4227,7 +4227,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case While_kind: return compiler_while(c, s) == SUCCESS ? 1 : 0; case If_kind: - return compiler_if(c, s); + return compiler_if(c, s) == SUCCESS ? 1 : 0; case Match_kind: return compiler_match(c, s); case Raise_kind: From 99a7257cde3099a41bef1497f7e4de93b5c3b67c Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:38:27 +0000 Subject: [PATCH 36/93] compiler_try_finally returns SUCCESS/ERROR --- Python/compile.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e696a0c231ccc7..b730dd271659d2 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3512,45 +3512,46 @@ compiler_try_finally(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup); /* `try` block */ - _ADDOP_JUMP(c, loc, SETUP_FINALLY, end); + ADDOP_JUMP(c, loc, SETUP_FINALLY, end); USE_LABEL(c, body); - if (compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.Try.finalbody) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, FINALLY_TRY, body, end, + s->v.Try.finalbody)); + if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { - if (!compiler_try_except(c, s)) - return 0; + if (!compiler_try_except(c, s)) { + return ERROR; + } } else { - _VISIT_SEQ(c, stmt, s->v.Try.body); + VISIT_SEQ(c, stmt, s->v.Try.body); } - _ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_BLOCK); compiler_pop_fblock(c, FINALLY_TRY, body); - _VISIT_SEQ(c, stmt, s->v.Try.finalbody); + VISIT_SEQ(c, stmt, s->v.Try.finalbody); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); + ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); /* `finally` block */ USE_LABEL(c, end); loc = NO_LOCATION; - _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - _ADDOP(c, loc, PUSH_EXC_INFO); - if (compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL) < 0) { - return 0; - } - _VISIT_SEQ(c, stmt, s->v.Try.finalbody); + ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + ADDOP(c, loc, PUSH_EXC_INFO); + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL)); + VISIT_SEQ(c, stmt, s->v.Try.finalbody); loc = location_of_last_executing_statement(s->v.Try.finalbody); compiler_pop_fblock(c, FINALLY_END, end); - _ADDOP_I(c, loc, RERAISE, 0); + ADDOP_I(c, loc, RERAISE, 0); USE_LABEL(c, cleanup); - _POP_EXCEPT_AND_RERAISE(c, loc); + POP_EXCEPT_AND_RERAISE(c, loc); USE_LABEL(c, exit); - return 1; + return SUCCESS; } static int @@ -3973,7 +3974,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) static int compiler_try(struct compiler *c, stmt_ty s) { if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) - return compiler_try_finally(c, s); + return compiler_try_finally(c, s) == SUCCESS ? 1 : 0; else return compiler_try_except(c, s); } From 0902cf1db2500a58c0570968a5c4db3e6c03fecd Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:47:43 +0000 Subject: [PATCH 37/93] compiler_try_except returns SUCCESS/ERROR --- Python/compile.c | 89 ++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index b730dd271659d2..3507d3492c6edb 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3520,9 +3520,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s) s->v.Try.finalbody)); if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { - if (!compiler_try_except(c, s)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_try_except(c, s)); } else { VISIT_SEQ(c, stmt, s->v.Try.body); @@ -3646,42 +3644,43 @@ compiler_try_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, end); NEW_JUMP_TARGET_LABEL(c, cleanup); - _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + ADDOP_JUMP(c, loc, SETUP_FINALLY, except); USE_LABEL(c, body); - if (compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL) < 0) { - return 0; - } - _VISIT_SEQ(c, stmt, s->v.Try.body); + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL)); + VISIT_SEQ(c, stmt, s->v.Try.body); compiler_pop_fblock(c, TRY_EXCEPT, body); - _ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_BLOCK); if (s->v.Try.orelse && asdl_seq_LEN(s->v.Try.orelse)) { - _VISIT_SEQ(c, stmt, s->v.Try.orelse); + VISIT_SEQ(c, stmt, s->v.Try.orelse); } - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); n = asdl_seq_LEN(s->v.Try.handlers); USE_LABEL(c, except); - _ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); - _ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); + ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); + ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); + /* Runtime will push a block here, so we need to account for that */ - if (compiler_push_fblock(c, loc, EXCEPTION_HANDLER, NO_LABEL, NO_LABEL, NULL) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, EXCEPTION_HANDLER, NO_LABEL, NO_LABEL, NULL)); + for (i = 0; i < n; i++) { excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( s->v.Try.handlers, i); location loc = LOC(handler); if (!handler->v.ExceptHandler.type && i < n-1) { - return compiler_error(c, loc, "default 'except:' must be last"); + compiler_error(c, loc, "default 'except:' must be last"); + return ERROR; } NEW_JUMP_TARGET_LABEL(c, next_except); except = next_except; if (handler->v.ExceptHandler.type) { - _VISIT(c, expr, handler->v.ExceptHandler.type); - _ADDOP(c, loc, CHECK_EXC_MATCH); - _ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, except); + VISIT(c, expr, handler->v.ExceptHandler.type); + ADDOP(c, loc, CHECK_EXC_MATCH); + ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, except); } if (handler->v.ExceptHandler.name) { NEW_JUMP_TARGET_LABEL(c, cleanup_end); @@ -3701,63 +3700,63 @@ compiler_try_except(struct compiler *c, stmt_ty s) */ /* second try: */ - _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); + ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); USE_LABEL(c, cleanup_body); - if (compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, - NO_LABEL, handler->v.ExceptHandler.name) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, + NO_LABEL, handler->v.ExceptHandler.name)); /* second # body */ - _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); + VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); /* name = None; del name; # Mark as artificial */ - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP(c, NO_LOCATION, POP_EXCEPT); - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_EXCEPT); + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); /* except: */ USE_LABEL(c, cleanup_end); /* name = None; del name; # artificial */ - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); - _ADDOP_I(c, NO_LOCATION, RERAISE, 1); + ADDOP_I(c, NO_LOCATION, RERAISE, 1); } else { NEW_JUMP_TARGET_LABEL(c, cleanup_body); - _ADDOP(c, loc, POP_TOP); /* exc_value */ + ADDOP(c, loc, POP_TOP); /* exc_value */ USE_LABEL(c, cleanup_body); - if (compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, NULL) < 0) { - return 0; - } - _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, + NO_LABEL, NULL)); + + VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP(c, NO_LOCATION, POP_EXCEPT); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_EXCEPT); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); } USE_LABEL(c, except); } /* artificial */ compiler_pop_fblock(c, EXCEPTION_HANDLER, NO_LABEL); - _ADDOP_I(c, NO_LOCATION, RERAISE, 0); + ADDOP_I(c, NO_LOCATION, RERAISE, 0); USE_LABEL(c, cleanup); - _POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); + POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); USE_LABEL(c, end); - return 1; + return SUCCESS; } /* @@ -3976,7 +3975,7 @@ compiler_try(struct compiler *c, stmt_ty s) { if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) return compiler_try_finally(c, s) == SUCCESS ? 1 : 0; else - return compiler_try_except(c, s); + return compiler_try_except(c, s) == SUCCESS ? 1 : 0; } static int From 2680b56f12d46ef18894f875f983c99db764766a Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:55:42 +0000 Subject: [PATCH 38/93] compiler_try_star_finally, compiler_try_star_except return SUCCESS/ERROR --- Python/compile.c | 145 +++++++++++++++++++++++------------------------ 1 file changed, 72 insertions(+), 73 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 3507d3492c6edb..c9ea72d570747b 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3562,46 +3562,45 @@ compiler_try_star_finally(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, exit); NEW_JUMP_TARGET_LABEL(c, cleanup); /* `try` block */ - _ADDOP_JUMP(c, loc, SETUP_FINALLY, end); + ADDOP_JUMP(c, loc, SETUP_FINALLY, end); USE_LABEL(c, body); - if (compiler_push_fblock(c, loc, FINALLY_TRY, body, end, s->v.TryStar.finalbody) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, FINALLY_TRY, body, end, + s->v.TryStar.finalbody)); + if (s->v.TryStar.handlers && asdl_seq_LEN(s->v.TryStar.handlers)) { - if (!compiler_try_star_except(c, s)) { - return 0; - } + RETURN_IF_ERROR(compiler_try_star_except(c, s)); } else { - _VISIT_SEQ(c, stmt, s->v.TryStar.body); + VISIT_SEQ(c, stmt, s->v.TryStar.body); } - _ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_BLOCK); compiler_pop_fblock(c, FINALLY_TRY, body); - _VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); + VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); + ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); /* `finally` block */ USE_LABEL(c, end); loc = NO_LOCATION; - _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - _ADDOP(c, loc, PUSH_EXC_INFO); - if (compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL) < 0) { - return 0; - } - _VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); + ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + ADDOP(c, loc, PUSH_EXC_INFO); + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL)); + + VISIT_SEQ(c, stmt, s->v.TryStar.finalbody); loc = location_of_last_executing_statement(s->v.Try.finalbody); compiler_pop_fblock(c, FINALLY_END, end); - _ADDOP_I(c, loc, RERAISE, 0); + ADDOP_I(c, loc, RERAISE, 0); USE_LABEL(c, cleanup); - _POP_EXCEPT_AND_RERAISE(c, loc); + POP_EXCEPT_AND_RERAISE(c, loc); USE_LABEL(c, exit); - return 1; + return SUCCESS; } @@ -3820,27 +3819,27 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup); NEW_JUMP_TARGET_LABEL(c, reraise_star); - _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + ADDOP_JUMP(c, loc, SETUP_FINALLY, except); USE_LABEL(c, body); - if (compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL) < 0) { - return 0; - } - _VISIT_SEQ(c, stmt, s->v.TryStar.body); + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL)); + VISIT_SEQ(c, stmt, s->v.TryStar.body); compiler_pop_fblock(c, TRY_EXCEPT, body); - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, orelse); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP_JUMP(c, NO_LOCATION, JUMP, orelse); Py_ssize_t n = asdl_seq_LEN(s->v.TryStar.handlers); USE_LABEL(c, except); - _ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); - _ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); + ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup); + ADDOP(c, NO_LOCATION, PUSH_EXC_INFO); + /* Runtime will push a block here, so we need to account for that */ - if (compiler_push_fblock(c, loc, EXCEPTION_GROUP_HANDLER, - NO_LABEL, NO_LABEL, "except handler") < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, EXCEPTION_GROUP_HANDLER, + NO_LABEL, NO_LABEL, "except handler")); + for (Py_ssize_t i = 0; i < n; i++) { excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( s->v.TryStar.handlers, i); @@ -3854,7 +3853,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) [exc] COPY 1 [orig, exc] */ - _ADDOP_I(c, loc, COPY, 1); + ADDOP_I(c, loc, COPY, 1); /* create empty list for exceptions raised/reraise in the except* blocks */ /* @@ -3862,16 +3861,16 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) [orig, exc, []] SWAP 2 [orig, [], exc] */ - _ADDOP_I(c, loc, BUILD_LIST, 0); - _ADDOP_I(c, loc, SWAP, 2); + ADDOP_I(c, loc, BUILD_LIST, 0); + ADDOP_I(c, loc, SWAP, 2); } if (handler->v.ExceptHandler.type) { - _VISIT(c, expr, handler->v.ExceptHandler.type); - _ADDOP(c, loc, CHECK_EG_MATCH); - _ADDOP_I(c, loc, COPY, 1); - _ADDOP_JUMP(c, loc, POP_JUMP_IF_NOT_NONE, handle_match); - _ADDOP(c, loc, POP_TOP); // match - _ADDOP_JUMP(c, loc, JUMP, except); + VISIT(c, expr, handler->v.ExceptHandler.type); + ADDOP(c, loc, CHECK_EG_MATCH); + ADDOP_I(c, loc, COPY, 1); + ADDOP_JUMP(c, loc, POP_JUMP_IF_NOT_NONE, handle_match); + ADDOP(c, loc, POP_TOP); // match + ADDOP_JUMP(c, loc, JUMP, except); } USE_LABEL(c, handle_match); @@ -3883,7 +3882,7 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store); } else { - _ADDOP(c, loc, POP_TOP); // match + ADDOP(c, loc, POP_TOP); // match } /* @@ -3897,46 +3896,46 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) del name */ /* second try: */ - _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); + ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end); USE_LABEL(c, cleanup_body); - if (compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, NO_LABEL, handler->v.ExceptHandler.name) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body, + NO_LABEL, handler->v.ExceptHandler.name)); /* second # body */ - _VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); + VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); /* name = None; del name; # artificial */ - _ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_BLOCK); if (handler->v.ExceptHandler.name) { - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); } - _ADDOP_JUMP(c, NO_LOCATION, JUMP, except); + ADDOP_JUMP(c, NO_LOCATION, JUMP, except); /* except: */ USE_LABEL(c, cleanup_end); /* name = None; del name; # artificial */ if (handler->v.ExceptHandler.name) { - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); } /* add exception raised to the res list */ - _ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc - _ADDOP(c, NO_LOCATION, POP_TOP); // lasti - _ADDOP_JUMP(c, NO_LOCATION, JUMP, except); + ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc + ADDOP(c, NO_LOCATION, POP_TOP); // lasti + ADDOP_JUMP(c, NO_LOCATION, JUMP, except); USE_LABEL(c, except); if (i == n - 1) { /* Add exc to the list (if not None it's the unhandled part of the EG) */ - _ADDOP_I(c, NO_LOCATION, LIST_APPEND, 1); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, reraise_star); + ADDOP_I(c, NO_LOCATION, LIST_APPEND, 1); + ADDOP_JUMP(c, NO_LOCATION, JUMP, reraise_star); } } /* artificial */ @@ -3944,30 +3943,30 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, reraise); USE_LABEL(c, reraise_star); - _ADDOP(c, NO_LOCATION, PREP_RERAISE_STAR); - _ADDOP_I(c, NO_LOCATION, COPY, 1); - _ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise); + ADDOP(c, NO_LOCATION, PREP_RERAISE_STAR); + ADDOP_I(c, NO_LOCATION, COPY, 1); + ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise); /* Nothing to reraise */ - _ADDOP(c, NO_LOCATION, POP_TOP); - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP(c, NO_LOCATION, POP_EXCEPT); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + ADDOP(c, NO_LOCATION, POP_TOP); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_EXCEPT); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, reraise); - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP_I(c, NO_LOCATION, SWAP, 2); - _ADDOP(c, NO_LOCATION, POP_EXCEPT); - _ADDOP_I(c, NO_LOCATION, RERAISE, 0); + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP_I(c, NO_LOCATION, SWAP, 2); + ADDOP(c, NO_LOCATION, POP_EXCEPT); + ADDOP_I(c, NO_LOCATION, RERAISE, 0); USE_LABEL(c, cleanup); - _POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); + POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); USE_LABEL(c, orelse); - _VISIT_SEQ(c, stmt, s->v.TryStar.orelse); + VISIT_SEQ(c, stmt, s->v.TryStar.orelse); USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -3982,10 +3981,10 @@ static int compiler_try_star(struct compiler *c, stmt_ty s) { if (s->v.TryStar.finalbody && asdl_seq_LEN(s->v.TryStar.finalbody)) { - return compiler_try_star_finally(c, s); + return compiler_try_star_finally(c, s) == SUCCESS ? 1 : 0; } else { - return compiler_try_star_except(c, s); + return compiler_try_star_except(c, s) == SUCCESS ? 1 : 0; } } From 6136f33c18f5af122efaeac7f132454bb9b92979 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 19:57:44 +0000 Subject: [PATCH 39/93] compiler_try, compiler_try_star return SUCCESS/ERROR --- Python/compile.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index c9ea72d570747b..674cff44377f59 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3972,19 +3972,19 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) static int compiler_try(struct compiler *c, stmt_ty s) { if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) - return compiler_try_finally(c, s) == SUCCESS ? 1 : 0; + return compiler_try_finally(c, s); else - return compiler_try_except(c, s) == SUCCESS ? 1 : 0; + return compiler_try_except(c, s); } static int compiler_try_star(struct compiler *c, stmt_ty s) { if (s->v.TryStar.finalbody && asdl_seq_LEN(s->v.TryStar.finalbody)) { - return compiler_try_star_finally(c, s) == SUCCESS ? 1 : 0; + return compiler_try_star_finally(c, s); } else { - return compiler_try_star_except(c, s) == SUCCESS ? 1 : 0; + return compiler_try_star_except(c, s); } } @@ -4244,9 +4244,9 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) break; } case Try_kind: - return compiler_try(c, s); + return compiler_try(c, s) == SUCCESS ? 1 : 0; case TryStar_kind: - return compiler_try_star(c, s); + return compiler_try_star(c, s) == SUCCESS ? 1 : 0; case Assert_kind: return compiler_assert(c, s); case Import_kind: From 73d8b72917da4349b3c3e577726bbb8602aeb832 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 20:05:07 +0000 Subject: [PATCH 40/93] compiler_assert returns SUCCESS/ERROR --- Python/compile.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 674cff44377f59..ced137f85c6c0c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4149,25 +4149,25 @@ compiler_assert(struct compiler *c, stmt_ty s) if (!compiler_warn(c, LOC(s), "assertion is always true, " "perhaps remove parentheses?")) { - return 0; + return ERROR; } } if (c->c_optimize) { - return 1; + return SUCCESS; } NEW_JUMP_TARGET_LABEL(c, end); if (!compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1)) { - return 0; + return ERROR; } - _ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR); + ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR); if (s->v.Assert.msg) { - _VISIT(c, expr, s->v.Assert.msg); - _ADDOP_I(c, LOC(s), CALL, 0); + VISIT(c, expr, s->v.Assert.msg); + ADDOP_I(c, LOC(s), CALL, 0); } - _ADDOP_I(c, LOC(s), RAISE_VARARGS, 1); + ADDOP_I(c, LOC(s), RAISE_VARARGS, 1); USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -4248,7 +4248,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case TryStar_kind: return compiler_try_star(c, s) == SUCCESS ? 1 : 0; case Assert_kind: - return compiler_assert(c, s); + return compiler_assert(c, s) == SUCCESS ? 1 : 0; case Import_kind: return compiler_import(c, s); case ImportFrom_kind: From 6966b263615e5b4cfe58ed1f70cefeeb75cf972b Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 20:06:07 +0000 Subject: [PATCH 41/93] compiler_import, compiler_import_as return SUCCESS/ERROR --- Python/compile.c | 52 +++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index ced137f85c6c0c..1d2dde9ecc8ceb 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4000,33 +4000,36 @@ compiler_import_as(struct compiler *c, location loc, */ Py_ssize_t len = PyUnicode_GET_LENGTH(name); Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); - if (dot == -2) - return 0; + if (dot == -2) { + return ERROR; + } if (dot != -1) { /* Consume the base module name to get the first attribute */ while (1) { Py_ssize_t pos = dot + 1; PyObject *attr; dot = PyUnicode_FindChar(name, '.', pos, len, 1); - if (dot == -2) - return 0; + if (dot == -2) { + return ERROR; + } attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); - if (!attr) - return 0; - _ADDOP_N(c, loc, IMPORT_FROM, attr, names); + if (!attr) { + return ERROR; + } + ADDOP_N(c, loc, IMPORT_FROM, attr, names); if (dot == -1) { break; } - _ADDOP_I(c, loc, SWAP, 2); - _ADDOP(c, loc, POP_TOP); + ADDOP_I(c, loc, SWAP, 2); + ADDOP(c, loc, POP_TOP); } if (!compiler_nameop(c, loc, asname, Store)) { - return 0; + return ERROR; } - _ADDOP(c, loc, POP_TOP); - return 1; + ADDOP(c, loc, POP_TOP); + return SUCCESS; } - return compiler_nameop(c, loc, asname, Store); + return compiler_nameop(c, loc, asname, Store) ? SUCCESS : ERROR; } static int @@ -4047,14 +4050,15 @@ compiler_import(struct compiler *c, stmt_ty s) alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); int r; - _ADDOP_LOAD_CONST(c, loc, zero); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADDOP_NAME(c, loc, IMPORT_NAME, alias->name, names); + ADDOP_LOAD_CONST(c, loc, zero); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADDOP_NAME(c, loc, IMPORT_NAME, alias->name, names); if (alias->asname) { r = compiler_import_as(c, loc, alias->name, alias->asname); - if (!r) + if (r == ERROR) { return r; + } } else { identifier tmp = alias->name; @@ -4062,18 +4066,20 @@ compiler_import(struct compiler *c, stmt_ty s) alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); if (dot != -1) { tmp = PyUnicode_Substring(alias->name, 0, dot); - if (tmp == NULL) - return 0; + if (tmp == NULL) { + return ERROR; + } } r = compiler_nameop(c, loc, tmp, Store); if (dot != -1) { Py_DECREF(tmp); } - if (!r) - return r; + if (!r) { + return ERROR; + } } } - return 1; + return SUCCESS; } static int @@ -4250,7 +4256,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case Assert_kind: return compiler_assert(c, s) == SUCCESS ? 1 : 0; case Import_kind: - return compiler_import(c, s); + return compiler_import(c, s) == SUCCESS ? 1 : 0; case ImportFrom_kind: return compiler_from_import(c, s); case Global_kind: From 1cdf83bb684282d0ae0a27517dce1fd447c61093 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 20:12:53 +0000 Subject: [PATCH 42/93] compiler_from_import, compiler_break return SUCCESS/ERROR --- Python/compile.c | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 1d2dde9ecc8ceb..f35671b67106d7 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3429,18 +3429,15 @@ compiler_break(struct compiler *c, location loc) { struct fblockinfo *loop = NULL; /* Emit instruction with line number */ - _ADDOP(c, loc, NOP); - if (compiler_unwind_fblock_stack(c, &loc, 0, &loop) < 0) { - return 0; - } + ADDOP(c, loc, NOP); + RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop)); if (loop == NULL) { - return compiler_error(c, loc, "'break' outside loop"); - } - if (compiler_unwind_fblock(c, &loc, loop, 0) < 0) { - return 0; + compiler_error(c, loc, "'break' outside loop"); + return ERROR; } - _ADDOP_JUMP(c, loc, JUMP, loop->fb_exit); - return 1; + RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0)); + ADDOP_JUMP(c, loc, JUMP, loop->fb_exit); + return SUCCESS; } static int @@ -4087,11 +4084,11 @@ compiler_from_import(struct compiler *c, stmt_ty s) { Py_ssize_t n = asdl_seq_LEN(s->v.ImportFrom.names); - _ADDOP_LOAD_CONST_NEW(c, LOC(s), PyLong_FromLong(s->v.ImportFrom.level)); + ADDOP_LOAD_CONST_NEW(c, LOC(s), PyLong_FromLong(s->v.ImportFrom.level)); PyObject *names = PyTuple_New(n); if (!names) { - return 0; + return ERROR; } /* build up the names */ @@ -4105,17 +4102,18 @@ compiler_from_import(struct compiler *c, stmt_ty s) _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { Py_DECREF(names); - return compiler_error(c, LOC(s), "from __future__ imports must occur " - "at the beginning of the file"); + compiler_error(c, LOC(s), "from __future__ imports must occur " + "at the beginning of the file"); + return ERROR; } - _ADDOP_LOAD_CONST_NEW(c, LOC(s), names); + ADDOP_LOAD_CONST_NEW(c, LOC(s), names); if (s->v.ImportFrom.module) { - _ADDOP_NAME(c, LOC(s), IMPORT_NAME, s->v.ImportFrom.module, names); + ADDOP_NAME(c, LOC(s), IMPORT_NAME, s->v.ImportFrom.module, names); } else { _Py_DECLARE_STR(empty, ""); - _ADDOP_NAME(c, LOC(s), IMPORT_NAME, &_Py_STR(empty), names); + ADDOP_NAME(c, LOC(s), IMPORT_NAME, &_Py_STR(empty), names); } for (Py_ssize_t i = 0; i < n; i++) { alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); @@ -4123,23 +4121,23 @@ compiler_from_import(struct compiler *c, stmt_ty s) if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { assert(n == 1); - _ADDOP(c, LOC(s), IMPORT_STAR); - return 1; + ADDOP(c, LOC(s), IMPORT_STAR); + return SUCCESS; } - _ADDOP_NAME(c, LOC(s), IMPORT_FROM, alias->name, names); + ADDOP_NAME(c, LOC(s), IMPORT_FROM, alias->name, names); store_name = alias->name; if (alias->asname) { store_name = alias->asname; } if (!compiler_nameop(c, LOC(s), store_name, Store)) { - return 0; + return ERROR; } } /* remove imported module */ - _ADDOP(c, LOC(s), POP_TOP); - return 1; + ADDOP(c, LOC(s), POP_TOP); + return SUCCESS; } static int @@ -4258,7 +4256,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case Import_kind: return compiler_import(c, s) == SUCCESS ? 1 : 0; case ImportFrom_kind: - return compiler_from_import(c, s); + return compiler_from_import(c, s) == SUCCESS ? 1 : 0; case Global_kind: case Nonlocal_kind: break; @@ -4273,7 +4271,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) } case Break_kind: { - return compiler_break(c, LOC(s)); + return compiler_break(c, LOC(s)) == SUCCESS ? 1 : 0; } case Continue_kind: { From 229d6fd7628d5175dd33df189ebc3ded4d1a8e0a Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 20:21:50 +0000 Subject: [PATCH 43/93] compiler_continue, compiler_with, compiler_async_with return SUCCESS/ERROR --- Python/compile.c | 122 ++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index f35671b67106d7..00d706a885aaf6 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3445,15 +3445,14 @@ compiler_continue(struct compiler *c, location loc) { struct fblockinfo *loop = NULL; /* Emit instruction with line number */ - _ADDOP(c, loc, NOP); - if (compiler_unwind_fblock_stack(c, &loc, 0, &loop) < 0) { - return 0; - } + ADDOP(c, loc, NOP); + RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop)); if (loop == NULL) { - return compiler_error(c, loc, "'continue' not properly in loop"); + compiler_error(c, loc, "'continue' not properly in loop"); + return ERROR; } - _ADDOP_JUMP(c, loc, JUMP, loop->fb_block); - return 1; + ADDOP_JUMP(c, loc, JUMP, loop->fb_block); + return SUCCESS; } @@ -4275,14 +4274,14 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) } case Continue_kind: { - return compiler_continue(c, LOC(s)); + return compiler_continue(c, LOC(s)) == SUCCESS ? 1 : 0; } case With_kind: - return compiler_with(c, s, 0); + return compiler_with(c, s, 0) == SUCCESS ? 1 : 0; case AsyncFunctionDef_kind: return compiler_function(c, s, 1); case AsyncWith_kind: - return compiler_async_with(c, s, 0); + return compiler_async_with(c, s, 0) == SUCCESS ? 1 : 0; case AsyncFor_kind: return compiler_async_for(c, s); } @@ -5803,7 +5802,8 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) if (IS_TOP_LEVEL_AWAIT(c)){ c->u->u_ste->ste_coroutine = 1; } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ - return compiler_error(c, loc, "'async with' outside async function"); + compiler_error(c, loc, "'async with' outside async function"); + return ERROR; } NEW_JUMP_TARGET_LABEL(c, block); @@ -5812,70 +5812,66 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) NEW_JUMP_TARGET_LABEL(c, cleanup); /* Evaluate EXPR */ - _VISIT(c, expr, item->context_expr); + VISIT(c, expr, item->context_expr); - _ADDOP(c, loc, BEFORE_ASYNC_WITH); - _ADDOP_I(c, loc, GET_AWAITABLE, 1); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); + ADDOP(c, loc, BEFORE_ASYNC_WITH); + ADDOP_I(c, loc, GET_AWAITABLE, 1); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); - _ADDOP_JUMP(c, loc, SETUP_WITH, final); + ADDOP_JUMP(c, loc, SETUP_WITH, final); /* SETUP_WITH pushes a finally block. */ USE_LABEL(c, block); - if (compiler_push_fblock(c, loc, ASYNC_WITH, block, final, s) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_push_fblock(c, loc, ASYNC_WITH, block, final, s)); if (item->optional_vars) { - _VISIT(c, expr, item->optional_vars); + VISIT(c, expr, item->optional_vars); } else { - /* Discard result from context.__aenter__() */ - _ADDOP(c, loc, POP_TOP); + /* Discard result from context.__aenter__() */ + ADDOP(c, loc, POP_TOP); } pos++; if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) { /* BLOCK code */ - _VISIT_SEQ(c, stmt, s->v.AsyncWith.body) + VISIT_SEQ(c, stmt, s->v.AsyncWith.body) } - else if (!compiler_async_with(c, s, pos)) { - return 0; + else if (compiler_async_with(c, s, pos) == ERROR) { + return ERROR; } compiler_pop_fblock(c, ASYNC_WITH, block); - _ADDOP(c, loc, POP_BLOCK); + ADDOP(c, loc, POP_BLOCK); /* End of body; start the cleanup */ /* For successful outcome: * call __exit__(None, None, None) */ - if (compiler_call_exit_with_nones(c, loc) < 0) { - return 0; - } - _ADDOP_I(c, loc, GET_AWAITABLE, 2); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); + RETURN_IF_ERROR(compiler_call_exit_with_nones(c, loc)); + ADDOP_I(c, loc, GET_AWAITABLE, 2); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); - _ADDOP(c, loc, POP_TOP); + ADDOP(c, loc, POP_TOP); - _ADDOP_JUMP(c, loc, JUMP, exit); + ADDOP_JUMP(c, loc, JUMP, exit); /* For exceptional outcome: */ USE_LABEL(c, final); - _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - _ADDOP(c, loc, PUSH_EXC_INFO); - _ADDOP(c, loc, WITH_EXCEPT_START); - _ADDOP_I(c, loc, GET_AWAITABLE, 2); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); + ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + ADDOP(c, loc, PUSH_EXC_INFO); + ADDOP(c, loc, WITH_EXCEPT_START); + ADDOP_I(c, loc, GET_AWAITABLE, 2); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); compiler_with_except_finish(c, cleanup); USE_LABEL(c, exit); - return 1; + return SUCCESS; } @@ -5913,34 +5909,34 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) NEW_JUMP_TARGET_LABEL(c, cleanup); /* Evaluate EXPR */ - _VISIT(c, expr, item->context_expr); + VISIT(c, expr, item->context_expr); /* Will push bound __exit__ */ location loc = LOC(s); - _ADDOP(c, loc, BEFORE_WITH); - _ADDOP_JUMP(c, loc, SETUP_WITH, final); + ADDOP(c, loc, BEFORE_WITH); + ADDOP_JUMP(c, loc, SETUP_WITH, final); /* SETUP_WITH pushes a finally block. */ USE_LABEL(c, block); - if (compiler_push_fblock(c, loc, WITH, block, final, s) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_push_fblock(c, loc, WITH, block, final, s)); if (item->optional_vars) { - _VISIT(c, expr, item->optional_vars); + VISIT(c, expr, item->optional_vars); } else { /* Discard result from context.__enter__() */ - _ADDOP(c, loc, POP_TOP); + ADDOP(c, loc, POP_TOP); } pos++; - if (pos == asdl_seq_LEN(s->v.With.items)) + if (pos == asdl_seq_LEN(s->v.With.items)) { /* BLOCK code */ - _VISIT_SEQ(c, stmt, s->v.With.body) - else if (!compiler_with(c, s, pos)) - return 0; + VISIT_SEQ(c, stmt, s->v.With.body) + } + else if (compiler_with(c, s, pos) == ERROR) { + return ERROR; + } - _ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_BLOCK); compiler_pop_fblock(c, WITH, block); /* End of body; start the cleanup. */ @@ -5949,22 +5945,20 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) * call __exit__(None, None, None) */ loc = LOC(s); - if (compiler_call_exit_with_nones(c, loc) < 0) { - return 0; - } - _ADDOP(c, loc, POP_TOP); - _ADDOP_JUMP(c, loc, JUMP, exit); + RETURN_IF_ERROR(compiler_call_exit_with_nones(c, loc)); + ADDOP(c, loc, POP_TOP); + ADDOP_JUMP(c, loc, JUMP, exit); /* For exceptional outcome: */ USE_LABEL(c, final); - _ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); - _ADDOP(c, loc, PUSH_EXC_INFO); - _ADDOP(c, loc, WITH_EXCEPT_START); + ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); + ADDOP(c, loc, PUSH_EXC_INFO); + ADDOP(c, loc, WITH_EXCEPT_START); compiler_with_except_finish(c, cleanup); USE_LABEL(c, exit); - return 1; + return SUCCESS; } static int From f8b658e2170aa0f5d4d0efbd6a990fdd5bf2a058 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 20:27:56 +0000 Subject: [PATCH 44/93] compiler_stmt_expr, compiler_async_for return SUCCESS/ERROR --- Python/compile.c | 54 ++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 00d706a885aaf6..e4fa099a671f1c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3305,32 +3305,32 @@ compiler_async_for(struct compiler *c, stmt_ty s) if (IS_TOP_LEVEL_AWAIT(c)){ c->u->u_ste->ste_coroutine = 1; } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { - return compiler_error(c, loc, "'async for' outside async function"); + compiler_error(c, loc, "'async for' outside async function"); + return ERROR; } NEW_JUMP_TARGET_LABEL(c, start); NEW_JUMP_TARGET_LABEL(c, except); NEW_JUMP_TARGET_LABEL(c, end); - _VISIT(c, expr, s->v.AsyncFor.iter); - _ADDOP(c, loc, GET_AITER); + VISIT(c, expr, s->v.AsyncFor.iter); + ADDOP(c, loc, GET_AITER); USE_LABEL(c, start); - if (compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL)); + /* SETUP_FINALLY to guard the __anext__ call */ - _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); - _ADDOP(c, loc, GET_ANEXT); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); - _ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ + ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + ADDOP(c, loc, GET_ANEXT); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); + ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */ /* Success block for __anext__ */ - _VISIT(c, expr, s->v.AsyncFor.target); - _VISIT_SEQ(c, stmt, s->v.AsyncFor.body); + VISIT(c, expr, s->v.AsyncFor.target); + VISIT_SEQ(c, stmt, s->v.AsyncFor.body); /* Mark jump as artificial */ - _ADDOP_JUMP(c, NO_LOCATION, JUMP, start); + ADDOP_JUMP(c, NO_LOCATION, JUMP, start); compiler_pop_fblock(c, FOR_LOOP, start); @@ -3340,13 +3340,13 @@ compiler_async_for(struct compiler *c, stmt_ty s) /* Use same line number as the iterator, * as the END_ASYNC_FOR succeeds the `for`, not the body. */ loc = LOC(s->v.AsyncFor.iter); - _ADDOP(c, loc, END_ASYNC_FOR); + ADDOP(c, loc, END_ASYNC_FOR); /* `else` block */ - _VISIT_SEQ(c, stmt, s->v.For.orelse); + VISIT_SEQ(c, stmt, s->v.For.orelse); USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -4177,20 +4177,20 @@ static int compiler_stmt_expr(struct compiler *c, location loc, expr_ty value) { if (c->c_interactive && c->c_nestlevel <= 1) { - _VISIT(c, expr, value); - _ADDOP(c, loc, PRINT_EXPR); - return 1; + VISIT(c, expr, value); + ADDOP(c, loc, PRINT_EXPR); + return SUCCESS; } if (value->kind == Constant_kind) { /* ignore constant statement */ - _ADDOP(c, loc, NOP); - return 1; + ADDOP(c, loc, NOP); + return SUCCESS; } - _VISIT(c, expr, value); - _ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */ - return 1; + VISIT(c, expr, value); + ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */ + return SUCCESS; } static int @@ -4261,7 +4261,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) break; case Expr_kind: { - return compiler_stmt_expr(c, LOC(s), s->v.Expr.value); + return compiler_stmt_expr(c, LOC(s), s->v.Expr.value) == SUCCESS ? 1 : 0; } case Pass_kind: { @@ -4283,7 +4283,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case AsyncWith_kind: return compiler_async_with(c, s, 0) == SUCCESS ? 1 : 0; case AsyncFor_kind: - return compiler_async_for(c, s); + return compiler_async_for(c, s) == SUCCESS ? 1 : 0; } return 1; From 5d8f93b2ce69964af1a3c3a01bc75c3b159f9c74 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:13:11 +0000 Subject: [PATCH 45/93] ensure_fail_pop return SUCCESS/ERROR --- Python/compile.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e4fa099a671f1c..5a39575e8aa383 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6482,20 +6482,20 @@ ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n) { Py_ssize_t size = n + 1; if (size <= pc->fail_pop_size) { - return 1; + return SUCCESS; } Py_ssize_t needed = sizeof(jump_target_label) * size; jump_target_label *resized = PyObject_Realloc(pc->fail_pop, needed); if (resized == NULL) { PyErr_NoMemory(); - return 0; + return ERROR; } pc->fail_pop = resized; while (pc->fail_pop_size < size) { NEW_JUMP_TARGET_LABEL(c, new_block); pc->fail_pop[pc->fail_pop_size++] = new_block; } - return 1; + return SUCCESS; } // Use op to jump to the correct fail_pop block. @@ -6506,7 +6506,9 @@ jump_to_fail_pop(struct compiler *c, location loc, // Pop any items on the top of the stack, plus any objects we were going to // capture on success: Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores); - RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops)); + if (ensure_fail_pop(c, pc, pops) < 0) { + return 0; + } _ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]); return 1; } @@ -7233,7 +7235,9 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) Py_DECREF(pc->stores); // NOTE: Returning macros are safe again. if (m->guard) { - RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0)); + if (ensure_fail_pop(c, pc, 0) < 0) { + return 0; + } RETURN_IF_FALSE(compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0)); } // Success! Pop the subject off, we're done with it: From 71e13c241492eb8699c25ca68be5fa04bf971ac1 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:18:25 +0000 Subject: [PATCH 46/93] jump_to_fail_pop return SUCCESS/ERROR --- Python/compile.c | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 5a39575e8aa383..47f5238f371b2e 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6506,11 +6506,9 @@ jump_to_fail_pop(struct compiler *c, location loc, // Pop any items on the top of the stack, plus any objects we were going to // capture on success: Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores); - if (ensure_fail_pop(c, pc, pops) < 0) { - return 0; - } - _ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]); - return 1; + RETURN_IF_ERROR(ensure_fail_pop(c, pc, pops)); + ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]); + return SUCCESS; } // Build all of the fail_pop blocks and reset fail_pop. @@ -6775,7 +6773,9 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) _ADDOP_I(c, LOC(p), IS_OP, 1); // TOS is now a tuple of (nargs + nattrs) attributes (or None): pc->on_top++; - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs); pc->on_top += nargs + nattrs - 1; for (i = 0; i < nargs + nattrs; i++) { @@ -6819,7 +6819,9 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // We need to keep the subject on top during the mapping and length checks: pc->on_top++; _ADDOP(c, LOC(p), MATCH_MAPPING); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } if (!size && !star_target) { // If the pattern is just "{}", we're done! Pop the subject: pc->on_top--; @@ -6831,7 +6833,9 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, _ADDOP(c, LOC(p), GET_LEN); _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); _ADDOP_COMPARE(c, LOC(p), GtE); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } } if (INT_MAX < size - 1) { return compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern"); @@ -6892,7 +6896,9 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, _ADDOP_I(c, LOC(p), COPY, 1); _ADDOP_LOAD_CONST(c, LOC(p), Py_None); _ADDOP_I(c, LOC(p), IS_OP, 1); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } // So far so good. Use that tuple of values on the stack to match // sub-patterns against: _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); @@ -7037,7 +7043,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) old_pc.fail_pop = NULL; // No match. Pop the remaining copy of the subject and fail: if ((cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) < 0) || - !jump_to_fail_pop(c, LOC(p), pc, JUMP)) { + jump_to_fail_pop(c, LOC(p), pc, JUMP) < 0) { goto error; } @@ -7114,20 +7120,26 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, // We need to keep the subject on top during the sequence and length checks: pc->on_top++; _ADDOP(c, LOC(p), MATCH_SEQUENCE); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } if (star < 0) { // No star: len(subject) == size _ADDOP(c, LOC(p), GET_LEN); _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); _ADDOP_COMPARE(c, LOC(p), Eq); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } } else if (size > 1) { // Star: len(subject) >= size - 1 _ADDOP(c, LOC(p), GET_LEN); _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1)); _ADDOP_COMPARE(c, LOC(p), GtE); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } } // Whatever comes next should consume the subject: pc->on_top--; @@ -7155,7 +7167,9 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) } _VISIT(c, expr, value); _ADDOP_COMPARE(c, LOC(p), Eq); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } return 1; } @@ -7165,7 +7179,9 @@ compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc assert(p->kind == MatchSingleton_kind); _ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value); _ADDOP_COMPARE(c, LOC(p), Is); - RETURN_IF_FALSE(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { + return 0; + } return 1; } From 4f10612f889144584dbbd9cc0ea21fa6e24a1fb9 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:22:03 +0000 Subject: [PATCH 47/93] emit_and_reset_fail_pop, compiler_error_duplicate_store return SUCCESS/ERROR --- Python/compile.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 47f5238f371b2e..842db6c48914b6 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6518,7 +6518,7 @@ emit_and_reset_fail_pop(struct compiler *c, location loc, { if (!pc->fail_pop_size) { assert(pc->fail_pop == NULL); - return 1; + return SUCCESS; } while (--pc->fail_pop_size) { USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]); @@ -6526,20 +6526,21 @@ emit_and_reset_fail_pop(struct compiler *c, location loc, pc->fail_pop_size = 0; PyObject_Free(pc->fail_pop); pc->fail_pop = NULL; - return 0; + return ERROR; } } USE_LABEL(c, pc->fail_pop[0]); PyObject_Free(pc->fail_pop); pc->fail_pop = NULL; - return 1; + return SUCCESS; } static int compiler_error_duplicate_store(struct compiler *c, location loc, identifier n) { - return compiler_error(c, loc, + compiler_error(c, loc, "multiple assignments to name %R in pattern", n); + return ERROR; } // Duplicate the effect of 3.10's ROT_* instructions using SWAPs. @@ -6569,7 +6570,8 @@ pattern_helper_store_name(struct compiler *c, location loc, return 0; } if (duplicate) { - return compiler_error_duplicate_store(c, loc, n); + compiler_error_duplicate_store(c, loc, n); + return 0; } // Rotate this object underneath any items we need to preserve: Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1; @@ -7030,8 +7032,8 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) } } assert(control); - if ((cfg_builder_addop_j(CFG_BUILDER(c), LOC(alt), JUMP, end) < 0) || - !emit_and_reset_fail_pop(c, LOC(alt), pc)) + if (cfg_builder_addop_j(CFG_BUILDER(c), LOC(alt), JUMP, end) < 0 || + emit_and_reset_fail_pop(c, LOC(alt), pc) < 0) { goto error; } @@ -7265,7 +7267,9 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) // If the pattern fails to match, we want the line number of the // cleanup to be associated with the failed pattern, not the last line // of the body - RETURN_IF_FALSE(emit_and_reset_fail_pop(c, LOC(m->pattern), pc)); + if (emit_and_reset_fail_pop(c, LOC(m->pattern), pc) < 0) { + return 0; + } } if (has_default) { // A trailing "case _" is common, and lets us save a bit of redundant From a5d017d4e302dce0dcd6cbb6cf7c2250c6b65257 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:23:49 +0000 Subject: [PATCH 48/93] pattern_helper_rotate return SUCCESS/ERROR --- Python/compile.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 842db6c48914b6..e875dc369e7971 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6548,9 +6548,9 @@ static int pattern_helper_rotate(struct compiler *c, location loc, Py_ssize_t count) { while (1 < count) { - _ADDOP_I(c, loc, SWAP, count--); + ADDOP_I(c, loc, SWAP, count--); } - return 1; + return SUCCESS; } static int @@ -6575,7 +6575,9 @@ pattern_helper_store_name(struct compiler *c, location loc, } // Rotate this object underneath any items we need to preserve: Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1; - RETURN_IF_FALSE(pattern_helper_rotate(c, loc, rotations)); + if (pattern_helper_rotate(c, loc, rotations) < 0) { + return 0; + } return !PyList_Append(pc->stores, n); } @@ -7024,7 +7026,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) // Do the same thing to the stack, using several // rotations: while (rotations--) { - if (!pattern_helper_rotate(c, LOC(alt), icontrol + 1)){ + if (pattern_helper_rotate(c, LOC(alt), icontrol + 1) < 0) { goto error; } } @@ -7060,7 +7062,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores); for (Py_ssize_t i = 0; i < nstores; i++) { // Rotate this capture to its proper place on the stack: - if (!pattern_helper_rotate(c, LOC(p), nrots)) { + if (pattern_helper_rotate(c, LOC(p), nrots) < 0) { goto error; } // Update the list of previous stores with this new name, checking for From cbb63b054246bd6e6d26456f16a4355b5a68b696 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:28:11 +0000 Subject: [PATCH 49/93] pattern_helper_store_name returns SUCCESS/ERROR --- Python/compile.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e875dc369e7971..40eaa895b6fe81 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6558,27 +6558,30 @@ pattern_helper_store_name(struct compiler *c, location loc, identifier n, pattern_context *pc) { if (n == NULL) { - _ADDOP(c, loc, POP_TOP); - return 1; + ADDOP(c, loc, POP_TOP); + return SUCCESS; } if (forbidden_name(c, loc, n, Store)) { - return 0; + return ERROR; } // Can't assign to the same name twice: int duplicate = PySequence_Contains(pc->stores, n); if (duplicate < 0) { - return 0; + return ERROR; } if (duplicate) { compiler_error_duplicate_store(c, loc, n); - return 0; + return ERROR; } // Rotate this object underneath any items we need to preserve: Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1; if (pattern_helper_rotate(c, loc, rotations) < 0) { - return 0; + return ERROR; + } + if (PyList_Append(pc->stores, n) < 0) { + return ERROR; } - return !PyList_Append(pc->stores, n); + return SUCCESS; } @@ -6695,7 +6698,7 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) const char *e = "wildcard makes remaining patterns unreachable"; return compiler_error(c, LOC(p), e); } - return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc); + return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc) == SUCCESS ? 1 : 0; } // Need to make a copy for (possibly) storing later: pc->on_top++; @@ -6703,7 +6706,9 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); // Success! Store it: pc->on_top--; - RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc)); + if (pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc) < 0) { + return 0; + } return 1; } @@ -6711,7 +6716,9 @@ static int compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchStar_kind); - RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc)); + if (pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc) < 0) { + return 0; + } return 1; } @@ -6931,7 +6938,9 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, _ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key] _ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...] } - RETURN_IF_FALSE(pattern_helper_store_name(c, LOC(p), star_target, pc)); + if (pattern_helper_store_name(c, LOC(p), star_target, pc) < 0) { + return 0; + } } else { _ADDOP(c, LOC(p), POP_TOP); // Tuple of keys. From 06ba665a91fe49e58bd9065d8c46d2b5dc20cca4 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:30:46 +0000 Subject: [PATCH 50/93] pattern_unpack_helper returns SUCCESS/ERROR --- Python/compile.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 40eaa895b6fe81..554894a3b46300 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6595,22 +6595,25 @@ pattern_unpack_helper(struct compiler *c, location loc, pattern_ty elt = asdl_seq_GET(elts, i); if (elt->kind == MatchStar_kind && !seen_star) { if ((i >= (1 << 8)) || - (n-i-1 >= (INT_MAX >> 8))) - return compiler_error(c, loc, + (n-i-1 >= (INT_MAX >> 8))) { + compiler_error(c, loc, "too many expressions in " "star-unpacking sequence pattern"); - _ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); + return ERROR; + } + ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); seen_star = 1; } else if (elt->kind == MatchStar_kind) { - return compiler_error(c, loc, + compiler_error(c, loc, "multiple starred expressions in sequence pattern"); + return ERROR; } } if (!seen_star) { - _ADDOP_I(c, loc, UNPACK_SEQUENCE, n); + ADDOP_I(c, loc, UNPACK_SEQUENCE, n); } - return 1; + return SUCCESS; } static int @@ -6618,7 +6621,9 @@ pattern_helper_sequence_unpack(struct compiler *c, location loc, asdl_pattern_seq *patterns, Py_ssize_t star, pattern_context *pc) { - RETURN_IF_FALSE(pattern_unpack_helper(c, loc, patterns)); + if (pattern_unpack_helper(c, loc, patterns) < 0) { + return 0; + } Py_ssize_t size = asdl_seq_LEN(patterns); // We've now got a bunch of new subjects on the stack. They need to remain // there after each subpattern match: From 0b84a46bf2900096de872e6429165d76323f5826 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:34:28 +0000 Subject: [PATCH 51/93] pattern_helper_sequence_unpack returns SUCCESS/ERROR --- Python/compile.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 554894a3b46300..e0f2a5be7713f7 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6622,7 +6622,7 @@ pattern_helper_sequence_unpack(struct compiler *c, location loc, pattern_context *pc) { if (pattern_unpack_helper(c, loc, patterns) < 0) { - return 0; + return ERROR; } Py_ssize_t size = asdl_seq_LEN(patterns); // We've now got a bunch of new subjects on the stack. They need to remain @@ -6632,9 +6632,11 @@ pattern_helper_sequence_unpack(struct compiler *c, location loc, // One less item to keep track of each time we loop through: pc->on_top--; pattern_ty pattern = asdl_seq_GET(patterns, i); - RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); + if (!compiler_pattern_subpattern(c, pattern, pc)) { + return ERROR; + } } - return 1; + return SUCCESS; } // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of @@ -7169,7 +7171,9 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc)); } else { - RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc)); + if (pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc) < 0) { + return 0; + } } return 1; } From c8acddb5918864d6426ecfc90823096641a9c98e Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:39:54 +0000 Subject: [PATCH 52/93] pattern_helper_sequence_subscr, compiler_pattern_subpattern return SUCCESS/ERROR --- Python/compile.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e0f2a5be7713f7..3dfff99f0d87ea 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6632,9 +6632,7 @@ pattern_helper_sequence_unpack(struct compiler *c, location loc, // One less item to keep track of each time we loop through: pc->on_top--; pattern_ty pattern = asdl_seq_GET(patterns, i); - if (!compiler_pattern_subpattern(c, pattern, pc)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc)); } return SUCCESS; } @@ -6659,24 +6657,24 @@ pattern_helper_sequence_subscr(struct compiler *c, location loc, assert(WILDCARD_STAR_CHECK(pattern)); continue; } - _ADDOP_I(c, loc, COPY, 1); + ADDOP_I(c, loc, COPY, 1); if (i < star) { - _ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(i)); + ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(i)); } else { // The subject may not support negative indexing! Compute a // nonnegative index: - _ADDOP(c, loc, GET_LEN); - _ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(size - i)); - _ADDOP_BINARY(c, loc, Sub); + ADDOP(c, loc, GET_LEN); + ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(size - i)); + ADDOP_BINARY(c, loc, Sub); } - _ADDOP(c, loc, BINARY_SUBSCR); - RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); + ADDOP(c, loc, BINARY_SUBSCR); + RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc)); } // Pop the subject, we're done with it: pc->on_top--; - _ADDOP(c, loc, POP_TOP); - return 1; + ADDOP(c, loc, POP_TOP); + return SUCCESS; } // Like compiler_pattern, but turn off checks for irrefutability. @@ -6686,9 +6684,11 @@ compiler_pattern_subpattern(struct compiler *c, { int allow_irrefutable = pc->allow_irrefutable; pc->allow_irrefutable = 1; - RETURN_IF_FALSE(compiler_pattern(c, p, pc)); + if (!compiler_pattern(c, p, pc)) { + return ERROR; + } pc->allow_irrefutable = allow_irrefutable; - return 1; + return SUCCESS; } static int @@ -6811,7 +6811,9 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) _ADDOP(c, LOC(p), POP_TOP); continue; } - RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); + if (compiler_pattern_subpattern(c, pattern, pc) < 0) { + return 0; + } } // Success! Pop the tuple of attributes: return 1; @@ -6924,7 +6926,9 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, for (Py_ssize_t i = 0; i < size; i++) { pc->on_top--; pattern_ty pattern = asdl_seq_GET(patterns, i); - RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc)); + if (compiler_pattern_subpattern(c, pattern, pc) < 0) { + return 0; + } } // If we get this far, it's a match! Whatever happens next should consume // the tuple of keys and the subject: @@ -7168,7 +7172,9 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, _ADDOP(c, LOC(p), POP_TOP); } else if (star_wildcard) { - RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc)); + if (pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc) < 0) { + return 0; + } } else { if (pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc) < 0) { From 44d545284f7b82f6f377847dbec05eea6548b7f8 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:45:16 +0000 Subject: [PATCH 53/93] compiler_pattern_as, compiler_pattern_star return SUCCESS/ERROR --- Python/compile.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 3dfff99f0d87ea..1c3c2a27fd6b26 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6700,33 +6700,34 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) if (!pc->allow_irrefutable) { if (p->v.MatchAs.name) { const char *e = "name capture %R makes remaining patterns unreachable"; - return compiler_error(c, LOC(p), e, p->v.MatchAs.name); + compiler_error(c, LOC(p), e, p->v.MatchAs.name); + return ERROR; } const char *e = "wildcard makes remaining patterns unreachable"; - return compiler_error(c, LOC(p), e); + compiler_error(c, LOC(p), e); + return ERROR; } - return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc) == SUCCESS ? 1 : 0; + return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc); } // Need to make a copy for (possibly) storing later: pc->on_top++; - _ADDOP_I(c, LOC(p), COPY, 1); - RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc)); + ADDOP_I(c, LOC(p), COPY, 1); + if (!compiler_pattern(c, p->v.MatchAs.pattern, pc)) { + return ERROR; + } // Success! Store it: pc->on_top--; - if (pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc) < 0) { - return 0; - } - return 1; + RETURN_IF_ERROR(pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc)); + return SUCCESS; } static int compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchStar_kind); - if (pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc) < 0) { - return 0; - } - return 1; + RETURN_IF_ERROR( + pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc)); + return SUCCESS; } static int @@ -7228,9 +7229,9 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) case MatchClass_kind: return compiler_pattern_class(c, p, pc); case MatchStar_kind: - return compiler_pattern_star(c, p, pc); + return compiler_pattern_star(c, p, pc) == SUCCESS ? 1 : 0; case MatchAs_kind: - return compiler_pattern_as(c, p, pc); + return compiler_pattern_as(c, p, pc) == SUCCESS ? 1 : 0; case MatchOr_kind: return compiler_pattern_or(c, p, pc); } From 77d8b27968006e7649a68bc79faa7f939470ef09 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:52:12 +0000 Subject: [PATCH 54/93] compiler_pattern_class, validate_kwd_attrs return SUCCESS/ERROR --- Python/compile.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 1c3c2a27fd6b26..34bcf1f8a1e827 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6740,18 +6740,18 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_ identifier attr = ((identifier)asdl_seq_GET(attrs, i)); location loc = LOC((pattern_ty) asdl_seq_GET(patterns, i)); if (forbidden_name(c, loc, attr, Store)) { - return -1; + return ERROR; } for (Py_ssize_t j = i + 1; j < nattrs; j++) { identifier other = ((identifier)asdl_seq_GET(attrs, j)); if (!PyUnicode_Compare(attr, other)) { location loc = LOC((pattern_ty) asdl_seq_GET(patterns, j)); compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr); - return -1; + return ERROR; } } } - return 0; + return SUCCESS; } static int @@ -6768,34 +6768,36 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern"; - return compiler_error(c, LOC(p), e, nattrs, nkwd_patterns); + compiler_error(c, LOC(p), e, nattrs, nkwd_patterns); + return ERROR; } if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) { const char *e = "too many sub-patterns in class pattern %R"; - return compiler_error(c, LOC(p), e, p->v.MatchClass.cls); + compiler_error(c, LOC(p), e, p->v.MatchClass.cls); + return ERROR; } if (nattrs) { - RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); + RETURN_IF_ERROR(validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); + } + VISIT(c, expr, p->v.MatchClass.cls); + PyObject *attr_names = PyTuple_New(nattrs); + if (!attr_names) { + return ERROR; } - _VISIT(c, expr, p->v.MatchClass.cls); - PyObject *attr_names; - RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs)); Py_ssize_t i; for (i = 0; i < nattrs; i++) { PyObject *name = asdl_seq_GET(kwd_attrs, i); PyTuple_SET_ITEM(attr_names, i, Py_NewRef(name)); } - _ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names); - _ADDOP_I(c, LOC(p), MATCH_CLASS, nargs); - _ADDOP_I(c, LOC(p), COPY, 1); - _ADDOP_LOAD_CONST(c, LOC(p), Py_None); - _ADDOP_I(c, LOC(p), IS_OP, 1); + ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names); + ADDOP_I(c, LOC(p), MATCH_CLASS, nargs); + ADDOP_I(c, LOC(p), COPY, 1); + ADDOP_LOAD_CONST(c, LOC(p), Py_None); + ADDOP_I(c, LOC(p), IS_OP, 1); // TOS is now a tuple of (nargs + nattrs) attributes (or None): pc->on_top++; - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } - _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs); pc->on_top += nargs + nattrs - 1; for (i = 0; i < nargs + nattrs; i++) { pc->on_top--; @@ -6809,15 +6811,13 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) pattern = asdl_seq_GET(kwd_patterns, i - nargs); } if (WILDCARD_CHECK(pattern)) { - _ADDOP(c, LOC(p), POP_TOP); + ADDOP(c, LOC(p), POP_TOP); continue; } - if (compiler_pattern_subpattern(c, pattern, pc) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc)); } // Success! Pop the tuple of attributes: - return 1; + return SUCCESS; } static int @@ -7227,7 +7227,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) case MatchMapping_kind: return compiler_pattern_mapping(c, p, pc); case MatchClass_kind: - return compiler_pattern_class(c, p, pc); + return compiler_pattern_class(c, p, pc) == SUCCESS ? 1 : 0; case MatchStar_kind: return compiler_pattern_star(c, p, pc) == SUCCESS ? 1 : 0; case MatchAs_kind: From 2e23b8f077a16f62d5febe8d6ce9df90d8c852fa Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:56:57 +0000 Subject: [PATCH 55/93] compiler_pattern_mapping returns SUCCESS/ERROR --- Python/compile.c | 76 ++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 42 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 34bcf1f8a1e827..ba5c3e32367f2b 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6833,33 +6833,31 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern"; - return compiler_error(c, LOC(p), e, size, npatterns); + compiler_error(c, LOC(p), e, size, npatterns); + return ERROR; } // We have a double-star target if "rest" is set PyObject *star_target = p->v.MatchMapping.rest; // We need to keep the subject on top during the mapping and length checks: pc->on_top++; - _ADDOP(c, LOC(p), MATCH_MAPPING); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } + ADDOP(c, LOC(p), MATCH_MAPPING); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); if (!size && !star_target) { // If the pattern is just "{}", we're done! Pop the subject: pc->on_top--; - _ADDOP(c, LOC(p), POP_TOP); - return 1; + ADDOP(c, LOC(p), POP_TOP); + return SUCCESS; } if (size) { // If the pattern has any keys in it, perform a length check: - _ADDOP(c, LOC(p), GET_LEN); - _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); - _ADDOP_COMPARE(c, LOC(p), GtE); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } + ADDOP(c, LOC(p), GET_LEN); + ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); + ADDOP_COMPARE(c, LOC(p), GtE); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } if (INT_MAX < size - 1) { - return compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern"); + compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern"); + return ERROR; } // Collect all of the keys into a tuple for MATCH_KEYS and // **rest. They can either be dotted names or literals: @@ -6868,7 +6866,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // SyntaxError in the case of duplicates. PyObject *seen = PySet_New(NULL); if (seen == NULL) { - return 0; + return ERROR; } // NOTE: goto error on failure in the loop below to avoid leaking `seen` @@ -6910,26 +6908,22 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // all keys have been checked; there are no duplicates Py_DECREF(seen); - _ADDOP_I(c, LOC(p), BUILD_TUPLE, size); - _ADDOP(c, LOC(p), MATCH_KEYS); + ADDOP_I(c, LOC(p), BUILD_TUPLE, size); + ADDOP(c, LOC(p), MATCH_KEYS); // There's now a tuple of keys and a tuple of values on top of the subject: pc->on_top += 2; - _ADDOP_I(c, LOC(p), COPY, 1); - _ADDOP_LOAD_CONST(c, LOC(p), Py_None); - _ADDOP_I(c, LOC(p), IS_OP, 1); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } + ADDOP_I(c, LOC(p), COPY, 1); + ADDOP_LOAD_CONST(c, LOC(p), Py_None); + ADDOP_I(c, LOC(p), IS_OP, 1); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); // So far so good. Use that tuple of values on the stack to match // sub-patterns against: - _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); + ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); pc->on_top += size - 1; for (Py_ssize_t i = 0; i < size; i++) { pc->on_top--; pattern_ty pattern = asdl_seq_GET(patterns, i); - if (compiler_pattern_subpattern(c, pattern, pc) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc)); } // If we get this far, it's a match! Whatever happens next should consume // the tuple of keys and the subject: @@ -6941,28 +6935,26 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // rest = dict(TOS1) // for key in TOS: // del rest[key] - _ADDOP_I(c, LOC(p), BUILD_MAP, 0); // [subject, keys, empty] - _ADDOP_I(c, LOC(p), SWAP, 3); // [empty, keys, subject] - _ADDOP_I(c, LOC(p), DICT_UPDATE, 2); // [copy, keys] - _ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); // [copy, keys...] + ADDOP_I(c, LOC(p), BUILD_MAP, 0); // [subject, keys, empty] + ADDOP_I(c, LOC(p), SWAP, 3); // [empty, keys, subject] + ADDOP_I(c, LOC(p), DICT_UPDATE, 2); // [copy, keys] + ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); // [copy, keys...] while (size) { - _ADDOP_I(c, LOC(p), COPY, 1 + size--); // [copy, keys..., copy] - _ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key] - _ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...] - } - if (pattern_helper_store_name(c, LOC(p), star_target, pc) < 0) { - return 0; + ADDOP_I(c, LOC(p), COPY, 1 + size--); // [copy, keys..., copy] + ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key] + ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...] } + RETURN_IF_ERROR(pattern_helper_store_name(c, LOC(p), star_target, pc)); } else { - _ADDOP(c, LOC(p), POP_TOP); // Tuple of keys. - _ADDOP(c, LOC(p), POP_TOP); // Subject. + ADDOP(c, LOC(p), POP_TOP); // Tuple of keys. + ADDOP(c, LOC(p), POP_TOP); // Subject. } - return 1; + return SUCCESS; error: Py_DECREF(seen); - return 0; + return ERROR; } static int @@ -7225,7 +7217,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) case MatchSequence_kind: return compiler_pattern_sequence(c, p, pc); case MatchMapping_kind: - return compiler_pattern_mapping(c, p, pc); + return compiler_pattern_mapping(c, p, pc) == SUCCESS ? 1 : 0; case MatchClass_kind: return compiler_pattern_class(c, p, pc) == SUCCESS ? 1 : 0; case MatchStar_kind: From 31bc6cdead0de4b13e73e225c0427d2a400de1d8 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 21:59:31 +0000 Subject: [PATCH 56/93] compiler_pattern_or returns SUCCESS/ERROR --- Python/compile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index ba5c3e32367f2b..771513f365b7ea 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -7097,15 +7097,15 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) Py_DECREF(control); // NOTE: Returning macros are safe again. // Pop the copy of the subject: - _ADDOP(c, LOC(p), POP_TOP); - return 1; + ADDOP(c, LOC(p), POP_TOP); + return SUCCESS; diff: compiler_error(c, LOC(p), "alternative patterns bind different names"); error: PyObject_Free(old_pc.fail_pop); Py_DECREF(old_pc.stores); Py_XDECREF(control); - return 0; + return ERROR; } @@ -7225,7 +7225,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) case MatchAs_kind: return compiler_pattern_as(c, p, pc) == SUCCESS ? 1 : 0; case MatchOr_kind: - return compiler_pattern_or(c, p, pc); + return compiler_pattern_or(c, p, pc) == SUCCESS ? 1 : 0; } // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter From 42c36cf88a35a888582578d71a94d1d3dc63a19c Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:03:25 +0000 Subject: [PATCH 57/93] compiler_pattern_sequence returns SUCCESS/ERROR --- Python/compile.c | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 771513f365b7ea..2f7a7d96cb0cfb 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -7125,7 +7125,8 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, if (pattern->kind == MatchStar_kind) { if (star >= 0) { const char *e = "multiple starred names in sequence pattern"; - return compiler_error(c, LOC(p), e); + compiler_error(c, LOC(p), e); + return ERROR; } star_wildcard = WILDCARD_STAR_CHECK(pattern); only_wildcard &= star_wildcard; @@ -7136,45 +7137,37 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, } // We need to keep the subject on top during the sequence and length checks: pc->on_top++; - _ADDOP(c, LOC(p), MATCH_SEQUENCE); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } + ADDOP(c, LOC(p), MATCH_SEQUENCE); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); if (star < 0) { // No star: len(subject) == size - _ADDOP(c, LOC(p), GET_LEN); - _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); - _ADDOP_COMPARE(c, LOC(p), Eq); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } + ADDOP(c, LOC(p), GET_LEN); + ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size)); + ADDOP_COMPARE(c, LOC(p), Eq); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } else if (size > 1) { // Star: len(subject) >= size - 1 - _ADDOP(c, LOC(p), GET_LEN); - _ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1)); - _ADDOP_COMPARE(c, LOC(p), GtE); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } + ADDOP(c, LOC(p), GET_LEN); + ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1)); + ADDOP_COMPARE(c, LOC(p), GtE); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } // Whatever comes next should consume the subject: pc->on_top--; if (only_wildcard) { // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. - _ADDOP(c, LOC(p), POP_TOP); + ADDOP(c, LOC(p), POP_TOP); } else if (star_wildcard) { - if (pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc) < 0) { - return 0; - } + RETURN_IF_ERROR( + pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc)); } else { - if (pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc) < 0) { - return 0; - } + RETURN_IF_ERROR( + pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc)); } - return 1; + return SUCCESS; } static int @@ -7215,7 +7208,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) case MatchSingleton_kind: return compiler_pattern_singleton(c, p, pc); case MatchSequence_kind: - return compiler_pattern_sequence(c, p, pc); + return compiler_pattern_sequence(c, p, pc) == SUCCESS ? 1 : 0; case MatchMapping_kind: return compiler_pattern_mapping(c, p, pc) == SUCCESS ? 1 : 0; case MatchClass_kind: From a0a19dc3dc3930e9d7b80a4e1a8d790c12fc43b1 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:06:29 +0000 Subject: [PATCH 58/93] compiler_pattern_value, compiler_pattern_singleton returns SUCCESS/ERROR --- Python/compile.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 2f7a7d96cb0cfb..fece7dc889ef81 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -7177,26 +7177,23 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) expr_ty value = p->v.MatchValue.value; if (!MATCH_VALUE_EXPR(value)) { const char *e = "patterns may only match literals and attribute lookups"; - return compiler_error(c, LOC(p), e); - } - _VISIT(c, expr, value); - _ADDOP_COMPARE(c, LOC(p), Eq); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; + compiler_error(c, LOC(p), e); + return ERROR; } - return 1; + VISIT(c, expr, value); + ADDOP_COMPARE(c, LOC(p), Eq); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + return SUCCESS; } static int compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc) { assert(p->kind == MatchSingleton_kind); - _ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value); - _ADDOP_COMPARE(c, LOC(p), Is); - if (jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE) < 0) { - return 0; - } - return 1; + ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value); + ADDOP_COMPARE(c, LOC(p), Is); + RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); + return SUCCESS; } static int @@ -7204,9 +7201,9 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) { switch (p->kind) { case MatchValue_kind: - return compiler_pattern_value(c, p, pc); + return compiler_pattern_value(c, p, pc) == SUCCESS ? 1 : 0; case MatchSingleton_kind: - return compiler_pattern_singleton(c, p, pc); + return compiler_pattern_singleton(c, p, pc) == SUCCESS ? 1 : 0; case MatchSequence_kind: return compiler_pattern_sequence(c, p, pc) == SUCCESS ? 1 : 0; case MatchMapping_kind: From ac78f3ca54affe3427d309f9931f2d6db569ba79 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:10:18 +0000 Subject: [PATCH 59/93] compiler_pattern return SUCCESS/ERROR --- Python/compile.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index fece7dc889ef81..fff5777987d6c6 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6684,9 +6684,7 @@ compiler_pattern_subpattern(struct compiler *c, { int allow_irrefutable = pc->allow_irrefutable; pc->allow_irrefutable = 1; - if (!compiler_pattern(c, p, pc)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_pattern(c, p, pc)); pc->allow_irrefutable = allow_irrefutable; return SUCCESS; } @@ -6712,9 +6710,7 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) // Need to make a copy for (possibly) storing later: pc->on_top++; ADDOP_I(c, LOC(p), COPY, 1); - if (!compiler_pattern(c, p->v.MatchAs.pattern, pc)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_pattern(c, p->v.MatchAs.pattern, pc)); // Success! Store it: pc->on_top--; RETURN_IF_ERROR(pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc)); @@ -6984,8 +6980,8 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) pc->fail_pop = NULL; pc->fail_pop_size = 0; pc->on_top = 0; - if ((cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, LOC(alt)) < 0) || - !compiler_pattern(c, alt, pc)) { + if (cfg_builder_addop_i(CFG_BUILDER(c), COPY, 1, LOC(alt)) < 0 || + compiler_pattern(c, alt, pc) < 0) { goto error; } // Success! @@ -7201,26 +7197,27 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) { switch (p->kind) { case MatchValue_kind: - return compiler_pattern_value(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_value(c, p, pc); case MatchSingleton_kind: - return compiler_pattern_singleton(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_singleton(c, p, pc); case MatchSequence_kind: - return compiler_pattern_sequence(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_sequence(c, p, pc); case MatchMapping_kind: - return compiler_pattern_mapping(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_mapping(c, p, pc); case MatchClass_kind: - return compiler_pattern_class(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_class(c, p, pc); case MatchStar_kind: - return compiler_pattern_star(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_star(c, p, pc); case MatchAs_kind: - return compiler_pattern_as(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_as(c, p, pc); case MatchOr_kind: - return compiler_pattern_or(c, p, pc) == SUCCESS ? 1 : 0; + return compiler_pattern_or(c, p, pc); } // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter const char *e = "invalid match pattern node in AST (kind=%d)"; - return compiler_error(c, LOC(p), e, p->kind); + compiler_error(c, LOC(p), e, p->kind); + return ERROR; } static int @@ -7245,7 +7242,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) pc->fail_pop_size = 0; pc->on_top = 0; // NOTE: Can't use returning macros here (they'll leak pc->stores)! - if (!compiler_pattern(c, m->pattern, pc)) { + if (compiler_pattern(c, m->pattern, pc) < 0) { Py_DECREF(pc->stores); return 0; } From 57bffdea8cb88b5967265b1ff155882535083ff7 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:15:59 +0000 Subject: [PATCH 60/93] compiler_match, compiler_match_inner return SUCCESS/ERROR --- Python/compile.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index fff5777987d6c6..dc6fb5042c07bd 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4231,7 +4231,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case If_kind: return compiler_if(c, s) == SUCCESS ? 1 : 0; case Match_kind: - return compiler_match(c, s); + return compiler_match(c, s) == SUCCESS ? 1 : 0; case Raise_kind: { Py_ssize_t n = 0; @@ -7223,7 +7223,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) static int compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) { - _VISIT(c, expr, s->v.Match.subject); + VISIT(c, expr, s->v.Match.subject); NEW_JUMP_TARGET_LABEL(c, end); Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases); assert(cases > 0); @@ -7233,9 +7233,12 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) m = asdl_seq_GET(s->v.Match.cases, i); // Only copy the subject if we're *not* on the last case: if (i != cases - has_default - 1) { - _ADDOP_I(c, LOC(m->pattern), COPY, 1); + ADDOP_I(c, LOC(m->pattern), COPY, 1); + } + pc->stores = PyList_New(0); + if (!pc->stores) { + return ERROR; } - RETURN_IF_FALSE(pc->stores = PyList_New(0)); // Irrefutable cases must be either guarded, last, or both: pc->allow_irrefutable = m->guard != NULL || i == cases - 1; pc->fail_pop = NULL; @@ -7244,7 +7247,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) // NOTE: Can't use returning macros here (they'll leak pc->stores)! if (compiler_pattern(c, m->pattern, pc) < 0) { Py_DECREF(pc->stores); - return 0; + return ERROR; } assert(!pc->on_top); // It's a match! Store all of the captured names (they're on the stack). @@ -7253,29 +7256,27 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) PyObject *name = PyList_GET_ITEM(pc->stores, n); if (!compiler_nameop(c, LOC(m->pattern), name, Store)) { Py_DECREF(pc->stores); - return 0; + return ERROR; } } Py_DECREF(pc->stores); // NOTE: Returning macros are safe again. if (m->guard) { - if (ensure_fail_pop(c, pc, 0) < 0) { - return 0; + RETURN_IF_ERROR(ensure_fail_pop(c, pc, 0)); + if (!compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0)) { + return ERROR; } - RETURN_IF_FALSE(compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0)); } // Success! Pop the subject off, we're done with it: if (i != cases - has_default - 1) { - _ADDOP(c, LOC(m->pattern), POP_TOP); + ADDOP(c, LOC(m->pattern), POP_TOP); } - _VISIT_SEQ(c, stmt, m->body); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + VISIT_SEQ(c, stmt, m->body); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); // If the pattern fails to match, we want the line number of the // cleanup to be associated with the failed pattern, not the last line // of the body - if (emit_and_reset_fail_pop(c, LOC(m->pattern), pc) < 0) { - return 0; - } + RETURN_IF_ERROR(emit_and_reset_fail_pop(c, LOC(m->pattern), pc)); } if (has_default) { // A trailing "case _" is common, and lets us save a bit of redundant @@ -7283,19 +7284,21 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) m = asdl_seq_GET(s->v.Match.cases, cases - 1); if (cases == 1) { // No matches. Done with the subject: - _ADDOP(c, LOC(m->pattern), POP_TOP); + ADDOP(c, LOC(m->pattern), POP_TOP); } else { // Show line coverage for default case (it doesn't create bytecode) - _ADDOP(c, LOC(m->pattern), NOP); + ADDOP(c, LOC(m->pattern), NOP); } if (m->guard) { - RETURN_IF_FALSE(compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)); + if (!compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)) { + return ERROR; + } } - _VISIT_SEQ(c, stmt, m->body); + VISIT_SEQ(c, stmt, m->body); } USE_LABEL(c, end); - return 1; + return SUCCESS; } static int From 855cdde3df79d374342c7a18509e765f89a0aa8b Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:21:46 +0000 Subject: [PATCH 61/93] compiler_function return SUCCESS/ERROR --- Python/compile.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index dc6fb5042c07bd..e7fd4dd78ab442 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2783,11 +2783,11 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) } if (compiler_check_debug_args(c, args) < 0) { - return 0; + return ERROR; } if (compiler_decorators(c, decos) < 0) { - return 0; + return ERROR; } firstlineno = s->lineno; @@ -2798,19 +2798,18 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) location loc = LOC(s); funcflags = compiler_default_arguments(c, loc, args); if (funcflags == -1) { - return 0; + return ERROR; } annotations = compiler_visit_annotations(c, loc, args, returns); if (annotations == 0) { - return 0; + return ERROR; } else if (annotations > 0) { funcflags |= 0x04; } - if (compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)); /* if not -OO mode, add docstring */ if (c->c_optimize < 2) { @@ -2818,19 +2817,19 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) } if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { compiler_exit_scope(c); - return 0; + return ERROR; } c->u->u_argcount = asdl_seq_LEN(args->args); c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { - _VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); + VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); } if (c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator) { if (wrap_in_stopiteration_handler(c) < 0) { compiler_exit_scope(c); - return 0; + return ERROR; } } co = assemble(c, 1); @@ -2839,20 +2838,20 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) if (co == NULL) { Py_XDECREF(qualname); Py_XDECREF(co); - return 0; + return ERROR; } if (compiler_make_closure(c, loc, co, funcflags, qualname) < 0) { Py_DECREF(qualname); Py_DECREF(co); - return 0; + return ERROR; } Py_DECREF(qualname); Py_DECREF(co); if (compiler_apply_decorators(c, decos) < 0) { - return 0; + return ERROR; } - return compiler_nameop(c, loc, name, Store); + return compiler_nameop(c, loc, name, Store) ? SUCCESS : ERROR; } static int @@ -4199,7 +4198,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) switch (s->kind) { case FunctionDef_kind: - return compiler_function(c, s, 0); + return compiler_function(c, s, 0) == SUCCESS ? 1 : 0; case ClassDef_kind: return compiler_class(c, s) == SUCCESS ? 1 : 0; case Return_kind: @@ -4279,7 +4278,7 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) case With_kind: return compiler_with(c, s, 0) == SUCCESS ? 1 : 0; case AsyncFunctionDef_kind: - return compiler_function(c, s, 1); + return compiler_function(c, s, 1) == SUCCESS ? 1 : 0; case AsyncWith_kind: return compiler_async_with(c, s, 0) == SUCCESS ? 1 : 0; case AsyncFor_kind: From 45028d58e53b9626496fd016dcf78564aecd7bb2 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:37:43 +0000 Subject: [PATCH 62/93] compiler_call_helper returns SUCCESS/ERROR --- Python/compile.c | 68 +++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e7fd4dd78ab442..7b936e0f2507a7 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2782,13 +2782,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) scope_type = COMPILER_SCOPE_FUNCTION; } - if (compiler_check_debug_args(c, args) < 0) { - return ERROR; - } - - if (compiler_decorators(c, decos) < 0) { - return ERROR; - } + RETURN_IF_ERROR(compiler_check_debug_args(c, args)); + RETURN_IF_ERROR(compiler_decorators(c, decos)); firstlineno = s->lineno; if (asdl_seq_LEN(decos)) { @@ -2848,9 +2843,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) Py_DECREF(qualname); Py_DECREF(co); - if (compiler_apply_decorators(c, decos) < 0) { - return ERROR; - } + RETURN_IF_ERROR(compiler_apply_decorators(c, decos)); return compiler_nameop(c, loc, name, Store) ? SUCCESS : ERROR; } @@ -2957,11 +2950,10 @@ compiler_class(struct compiler *c, stmt_ty s) ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name); /* 5. generate the rest of the code for the call */ - if (!compiler_call_helper(c, loc, 2, - s->v.ClassDef.bases, - s->v.ClassDef.keywords)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_call_helper(c, loc, 2, + s->v.ClassDef.bases, + s->v.ClassDef.keywords)); + /* 6. apply decorators */ RETURN_IF_ERROR(compiler_apply_decorators(c, decos)); @@ -5052,23 +5044,23 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) } location loc = LOC(key); if (forbidden_name(c, loc, key->arg, Store)) { - return -1; + return ERROR; } for (Py_ssize_t j = i + 1; j < nkeywords; j++) { keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg); - return -1; + return ERROR; } } } - return 0; + return SUCCESS; } static int compiler_call(struct compiler *c, expr_ty e) { - if (validate_keywords(c, e->v.Call.keywords) == -1) { + if (validate_keywords(c, e->v.Call.keywords) < 0) { return 0; } int ret = maybe_optimize_method_call(c, e); @@ -5084,7 +5076,7 @@ compiler_call(struct compiler *c, expr_ty e) loc = LOC(e); return compiler_call_helper(c, loc, 0, e->v.Call.args, - e->v.Call.keywords); + e->v.Call.keywords) == SUCCESS ? 1 : 0; } static int @@ -5239,9 +5231,7 @@ compiler_call_helper(struct compiler *c, location loc, { Py_ssize_t i, nseen, nelts, nkwelts; - if (validate_keywords(c, keywords) == -1) { - return 0; - } + RETURN_IF_ERROR(validate_keywords(c, keywords)); nelts = asdl_seq_LEN(args); nkwelts = asdl_seq_LEN(keywords); @@ -5266,26 +5256,26 @@ compiler_call_helper(struct compiler *c, location loc, for (i = 0; i < nelts; i++) { expr_ty elt = asdl_seq_GET(args, i); assert(elt->kind != Starred_kind); - _VISIT(c, expr, elt); + VISIT(c, expr, elt); } if (nkwelts) { - _VISIT_SEQ(c, keyword, keywords); + VISIT_SEQ(c, keyword, keywords); if (!compiler_call_simple_kw_helper(c, loc, keywords, nkwelts)) { - return 0; + return ERROR; }; } - _ADDOP_I(c, loc, CALL, n + nelts + nkwelts); - return 1; + ADDOP_I(c, loc, CALL, n + nelts + nkwelts); + return SUCCESS; ex_call: /* Do positional arguments. */ if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { - _VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); + VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); } else if (starunpack_helper(c, loc, args, n, BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1) == 0) { - return 0; + return ERROR; } /* Then keyword arguments */ if (nkwelts) { @@ -5299,20 +5289,20 @@ compiler_call_helper(struct compiler *c, location loc, /* A keyword argument unpacking. */ if (nseen) { if (!compiler_subkwargs(c, loc, keywords, i - nseen, i)) { - return 0; + return ERROR; } if (have_dict) { - _ADDOP_I(c, loc, DICT_MERGE, 1); + ADDOP_I(c, loc, DICT_MERGE, 1); } have_dict = 1; nseen = 0; } if (!have_dict) { - _ADDOP_I(c, loc, BUILD_MAP, 0); + ADDOP_I(c, loc, BUILD_MAP, 0); have_dict = 1; } - _VISIT(c, expr, kw->value); - _ADDOP_I(c, loc, DICT_MERGE, 1); + VISIT(c, expr, kw->value); + ADDOP_I(c, loc, DICT_MERGE, 1); } else { nseen++; @@ -5321,17 +5311,17 @@ compiler_call_helper(struct compiler *c, location loc, if (nseen) { /* Pack up any trailing keyword arguments. */ if (!compiler_subkwargs(c, loc, keywords, nkwelts - nseen, nkwelts)) { - return 0; + return ERROR; } if (have_dict) { - _ADDOP_I(c, loc, DICT_MERGE, 1); + ADDOP_I(c, loc, DICT_MERGE, 1); } have_dict = 1; } assert(have_dict); } - _ADDOP_I(c, loc, CALL_FUNCTION_EX, nkwelts > 0); - return 1; + ADDOP_I(c, loc, CALL_FUNCTION_EX, nkwelts > 0); + return SUCCESS; } From 5843200836ba1fa507224c3c086ad1259989beac Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:49:43 +0000 Subject: [PATCH 63/93] check_caller, compiler_call RETURN SUCCESS/ERROR --- Python/compile.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7b936e0f2507a7..aaaac8d6c27468 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4857,12 +4857,13 @@ check_caller(struct compiler *c, expr_ty e) case JoinedStr_kind: case FormattedValue_kind: { location loc = LOC(e); - return compiler_warn(c, loc, "'%.200s' object is not callable; " - "perhaps you missed a comma?", - infer_type(e)->tp_name); + int ret = compiler_warn(c, loc, "'%.200s' object is not callable; " + "perhaps you missed a comma?", + infer_type(e)->tp_name); + return ret == 0 ? ERROR : SUCCESS; } default: - return 1; + return SUCCESS; } } @@ -5060,23 +5061,22 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) static int compiler_call(struct compiler *c, expr_ty e) { - if (validate_keywords(c, e->v.Call.keywords) < 0) { - return 0; - } + RETURN_IF_ERROR(validate_keywords(c, e->v.Call.keywords)); int ret = maybe_optimize_method_call(c, e); - if (ret >= 0) { - return ret; + if (ret == 1) { + return SUCCESS; } - if (!check_caller(c, e->v.Call.func)) { - return 0; + if (ret == 0) { + return ERROR; } + RETURN_IF_ERROR(check_caller(c, e->v.Call.func)); location loc = LOC(e->v.Call.func); - _ADDOP(c, loc, PUSH_NULL); - _VISIT(c, expr, e->v.Call.func); + ADDOP(c, loc, PUSH_NULL); + VISIT(c, expr, e->v.Call.func); loc = LOC(e); return compiler_call_helper(c, loc, 0, e->v.Call.args, - e->v.Call.keywords) == SUCCESS ? 1 : 0; + e->v.Call.keywords); } static int @@ -6030,7 +6030,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Compare_kind: return compiler_compare(c, e); case Call_kind: - return compiler_call(c, e); + return compiler_call(c, e) == SUCCESS ? 1 : 0; case Constant_kind: _ADDOP_LOAD_CONST(c, loc, e->v.Constant.value); break; From 7611a97b6e3d072ecb1e4036d78c4a1777841b34 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 22:55:32 +0000 Subject: [PATCH 64/93] check_compare, compiler_addcompare return SUCCESS/ERROR --- Python/compile.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index aaaac8d6c27468..28f723b07387c6 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2979,9 +2979,8 @@ check_is_arg(expr_ty e) || value == Py_Ellipsis); } -/* Check operands of identity chacks ("is" and "is not"). +/* Check operands of identity checks ("is" and "is not"). Emit a warning if any operand is a constant except named singletons. - Return 0 on error. */ static int check_compare(struct compiler *c, expr_ty e) @@ -2997,12 +2996,13 @@ check_compare(struct compiler *c, expr_ty e) const char *msg = (op == Is) ? "\"is\" with a literal. Did you mean \"==\"?" : "\"is not\" with a literal. Did you mean \"!=\"?"; - return compiler_warn(c, LOC(e), msg); + int ret = compiler_warn(c, LOC(e), msg); + return ret == 0 ? ERROR : SUCCESS; } } left = right; } - return 1; + return SUCCESS; } static int compiler_addcompare(struct compiler *c, location loc, @@ -3105,7 +3105,7 @@ compiler_jump_if(struct compiler *c, location loc, case Compare_kind: { Py_ssize_t n = asdl_seq_LEN(e->v.Compare.ops) - 1; if (n > 0) { - if (!check_compare(c, e)) { + if (check_compare(c, e) < 0) { return 0; } NEW_JUMP_TARGET_LABEL(c, cleanup); @@ -4778,38 +4778,38 @@ compiler_compare(struct compiler *c, expr_ty e) location loc = LOC(e); Py_ssize_t i, n; - if (!check_compare(c, e)) { + if (check_compare(c, e) < 0) { return 0; } - _VISIT(c, expr, e->v.Compare.left); + VISIT(c, expr, e->v.Compare.left); 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_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, 0)); + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); + ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, 0)); } else { NEW_JUMP_TARGET_LABEL(c, cleanup); for (i = 0; i < n; i++) { - _VISIT(c, expr, + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); - _ADDOP_I(c, loc, SWAP, 2); - _ADDOP_I(c, loc, COPY, 2); - _ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i)); - _ADDOP_JUMP(c, loc, JUMP_IF_FALSE_OR_POP, cleanup); + ADDOP_I(c, loc, SWAP, 2); + ADDOP_I(c, loc, COPY, 2); + ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i)); + ADDOP_JUMP(c, loc, JUMP_IF_FALSE_OR_POP, cleanup); } - _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); - _ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, n)); + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); + ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, n)); NEW_JUMP_TARGET_LABEL(c, end); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, cleanup); - _ADDOP_I(c, loc, SWAP, 2); - _ADDOP(c, loc, POP_TOP); + ADDOP_I(c, loc, SWAP, 2); + ADDOP(c, loc, POP_TOP); USE_LABEL(c, end); } - return 1; + return SUCCESS; } static PyTypeObject * @@ -6028,7 +6028,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) _ADD_YIELD_FROM(c, loc, 1); break; case Compare_kind: - return compiler_compare(c, e); + return compiler_compare(c, e) == SUCCESS ? 1 : 0; case Call_kind: return compiler_call(c, e) == SUCCESS ? 1 : 0; case Constant_kind: From 31228260d2138c8181a349477badea10a257cec7 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 23:02:26 +0000 Subject: [PATCH 65/93] compiler_ifexp, compiler_boolop, unpack_helper, assignment_helper return SUCCESS/ERROR --- Python/compile.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 28f723b07387c6..333117165e94f1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3155,16 +3155,16 @@ compiler_ifexp(struct compiler *c, expr_ty e) NEW_JUMP_TARGET_LABEL(c, next); if (!compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0)) { - return 0; + return ERROR; } - _VISIT(c, expr, e->v.IfExp.body); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + VISIT(c, expr, e->v.IfExp.body); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, next); - _VISIT(c, expr, e->v.IfExp.orelse); + VISIT(c, expr, e->v.IfExp.orelse); USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -4482,16 +4482,16 @@ compiler_boolop(struct compiler *c, expr_ty e) n = asdl_seq_LEN(s) - 1; assert(n >= 0); for (i = 0; i < n; ++i) { - _VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); - _ADDOP_JUMP(c, loc, jumpi, end); + VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); + ADDOP_JUMP(c, loc, jumpi, end); NEW_JUMP_TARGET_LABEL(c, next); USE_LABEL(c, next); } - _VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); + VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); USE_LABEL(c, end); - return 1; + return SUCCESS; } static int @@ -4588,34 +4588,37 @@ unpack_helper(struct compiler *c, location loc, asdl_expr_seq *elts) expr_ty elt = asdl_seq_GET(elts, i); if (elt->kind == Starred_kind && !seen_star) { if ((i >= (1 << 8)) || - (n-i-1 >= (INT_MAX >> 8))) - return compiler_error(c, loc, + (n-i-1 >= (INT_MAX >> 8))) { + compiler_error(c, loc, "too many expressions in " "star-unpacking assignment"); - _ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); + return ERROR; + } + ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); seen_star = 1; } else if (elt->kind == Starred_kind) { - return compiler_error(c, loc, + compiler_error(c, loc, "multiple starred expressions in assignment"); + return ERROR; } } if (!seen_star) { - _ADDOP_I(c, loc, UNPACK_SEQUENCE, n); + ADDOP_I(c, loc, UNPACK_SEQUENCE, n); } - return 1; + return SUCCESS; } static int assignment_helper(struct compiler *c, location loc, asdl_expr_seq *elts) { Py_ssize_t n = asdl_seq_LEN(elts); - RETURN_IF_FALSE(unpack_helper(c, loc, elts)); + RETURN_IF_ERROR(unpack_helper(c, loc, elts)); for (Py_ssize_t i = 0; i < n; i++) { expr_ty elt = asdl_seq_GET(elts, i); - _VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); + VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); } - return 1; + return SUCCESS; } static int @@ -4624,7 +4627,7 @@ compiler_list(struct compiler *c, expr_ty e) location loc = LOC(e); asdl_expr_seq *elts = e->v.List.elts; if (e->v.List.ctx == Store) { - return assignment_helper(c, loc, elts); + return assignment_helper(c, loc, elts) == SUCCESS ? 1 : 0; } else if (e->v.List.ctx == Load) { return starunpack_helper(c, loc, elts, 0, @@ -4641,7 +4644,7 @@ compiler_tuple(struct compiler *c, expr_ty e) location loc = LOC(e); asdl_expr_seq *elts = e->v.Tuple.elts; if (e->v.Tuple.ctx == Store) { - return assignment_helper(c, loc, elts); + return assignment_helper(c, loc, elts) == SUCCESS ? 1 : 0; } else if (e->v.Tuple.ctx == Load) { return starunpack_helper(c, loc, elts, 0, @@ -5961,7 +5964,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) _VISIT(c, expr, e->v.NamedExpr.target); break; case BoolOp_kind: - return compiler_boolop(c, e); + return compiler_boolop(c, e) == SUCCESS ? 1 : 0; case BinOp_kind: _VISIT(c, expr, e->v.BinOp.left); _VISIT(c, expr, e->v.BinOp.right); @@ -5974,7 +5977,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Lambda_kind: return compiler_lambda(c, e); case IfExp_kind: - return compiler_ifexp(c, e); + return compiler_ifexp(c, e) == SUCCESS ? 1 : 0; case Dict_kind: return compiler_dict(c, e); case Set_kind: From 46e7471445fb2948c16e9d2dac88eb6510556e93 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 23:09:59 +0000 Subject: [PATCH 66/93] addop_yield, addop_binary compiler_addcompare return SUCCESS/ERROR --- Python/compile.c | 77 ++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 333117165e94f1..7cef21798cfb8c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1653,20 +1653,14 @@ cfg_builder_addop_j(cfg_builder *g, location loc, #define ADDOP_JUMP(C, LOC, OP, O) \ RETURN_IF_ERROR(cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O))); -#define ADDOP_COMPARE(C, LOC, CMP) { \ - if (!compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))) \ - return ERROR; \ -} +#define ADDOP_COMPARE(C, LOC, CMP) \ + RETURN_IF_ERROR(compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))); -#define ADDOP_BINARY(C, LOC, BINOP) { \ - if (!addop_binary((C), (LOC), (BINOP), false)) \ - return ERROR; \ -} +#define ADDOP_BINARY(C, LOC, BINOP) \ + RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), false)); -#define ADDOP_INPLACE(C, LOC, BINOP) { \ - if (!addop_binary((C), (LOC), (BINOP), true)) \ - return ERROR; \ -} +#define ADDOP_INPLACE(C, LOC, BINOP) \ + RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), true)); #define ADD_YIELD_FROM(C, LOC, await) \ RETURN_IF_ERROR(compiler_add_yield_from((C), (LOC), (await))); @@ -1675,9 +1669,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC))); #define ADDOP_YIELD(C, LOC) { \ - if (!addop_yield((C), (LOC))) \ - return ERROR; \ -} + RETURN_IF_ERROR(addop_yield((C), (LOC))); /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use the ASDL name to synthesize the name of the C type and the visit function. @@ -1778,15 +1770,20 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define _ADDOP_COMPARE(C, LOC, CMP) { \ - if (!compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))) \ + if (compiler_addcompare((C), (LOC), (cmpop_ty)(CMP)) < 0) \ return 0; \ } -#define _ADDOP_BINARY(C, LOC, BINOP) \ - RETURN_IF_FALSE(addop_binary((C), (LOC), (BINOP), false)) +#define _ADDOP_BINARY(C, LOC, BINOP) { \ + if (addop_binary((C), (LOC), (BINOP), false) < 0) \ + return 0; \ + } + +#define _ADDOP_INPLACE(C, LOC, BINOP) { \ + if (addop_binary((C), (LOC), (BINOP), true) < 0) \ + return 0; \ + } -#define _ADDOP_INPLACE(C, LOC, BINOP) \ - RETURN_IF_FALSE(addop_binary((C), (LOC), (BINOP), true)) #define _ADD_YIELD_FROM(C, LOC, await) { \ if (compiler_add_yield_from((C), (LOC), (await)) < 0) \ @@ -1798,8 +1795,10 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return 0; \ } -#define _ADDOP_YIELD(C, LOC) \ - RETURN_IF_FALSE(addop_yield((C), (LOC))) +#define _ADDOP_YIELD(C, LOC) { \ + if (addop_yield((C), (LOC)) < 0) \ + return 0; \ + } /* _VISIT and _VISIT_SEQ takes an ASDL type as their second argument. They use the ASDL name to synthesize the name of the C type and the visit function. @@ -3029,22 +3028,22 @@ static int compiler_addcompare(struct compiler *c, location loc, cmp = Py_GE; break; case Is: - _ADDOP_I(c, loc, IS_OP, 0); - return 1; + ADDOP_I(c, loc, IS_OP, 0); + return SUCCESS; case IsNot: - _ADDOP_I(c, loc, IS_OP, 1); - return 1; + ADDOP_I(c, loc, IS_OP, 1); + return SUCCESS; case In: - _ADDOP_I(c, loc, CONTAINS_OP, 0); - return 1; + ADDOP_I(c, loc, CONTAINS_OP, 0); + return SUCCESS; case NotIn: - _ADDOP_I(c, loc, CONTAINS_OP, 1); - return 1; + ADDOP_I(c, loc, CONTAINS_OP, 1); + return SUCCESS; default: Py_UNREACHABLE(); } - _ADDOP_I(c, loc, COMPARE_OP, cmp); - return 1; + ADDOP_I(c, loc, COMPARE_OP, cmp); + return SUCCESS; } @@ -4347,21 +4346,21 @@ addop_binary(struct compiler *c, location loc, operator_ty binop, default: PyErr_Format(PyExc_SystemError, "%s op %d should not be possible", inplace ? "inplace" : "binary", binop); - return 0; + return ERROR; } - _ADDOP_I(c, loc, BINARY_OP, oparg); - return 1; + ADDOP_I(c, loc, BINARY_OP, oparg); + return SUCCESS; } static int addop_yield(struct compiler *c, location loc) { if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) { - _ADDOP(c, loc, ASYNC_GEN_WRAP); + ADDOP(c, loc, ASYNC_GEN_WRAP); } - _ADDOP_I(c, loc, YIELD_VALUE, 0); - _ADDOP_I(c, loc, RESUME, 1); - return 1; + ADDOP_I(c, loc, YIELD_VALUE, 0); + ADDOP_I(c, loc, RESUME, 1); + return SUCCESS; } static int From a31d07871bc4c0f61b9251d8cd481d1f96cc1829 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 23:12:49 +0000 Subject: [PATCH 67/93] compiler_with_except_finish return SUCCESS/ERROR --- Python/compile.c | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7cef21798cfb8c..2488c3e16a0759 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1779,21 +1779,12 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return 0; \ } -#define _ADDOP_INPLACE(C, LOC, BINOP) { \ - if (addop_binary((C), (LOC), (BINOP), true) < 0) \ - return 0; \ - } - #define _ADD_YIELD_FROM(C, LOC, await) { \ if (compiler_add_yield_from((C), (LOC), (await)) < 0) \ return 0; \ } -#define _POP_EXCEPT_AND_RERAISE(C, LOC) { \ - if (compiler_pop_except_and_reraise((C), (LOC)) < 0) \ - return 0; \ -} #define _ADDOP_YIELD(C, LOC) { \ if (addop_yield((C), (LOC)) < 0) \ @@ -5740,23 +5731,23 @@ 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_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress); - _ADDOP_I(c, NO_LOCATION, RERAISE, 2); + ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress); + ADDOP_I(c, NO_LOCATION, RERAISE, 2); USE_LABEL(c, suppress); - _ADDOP(c, NO_LOCATION, POP_TOP); /* exc_value */ - _ADDOP(c, NO_LOCATION, POP_BLOCK); - _ADDOP(c, NO_LOCATION, POP_EXCEPT); - _ADDOP(c, NO_LOCATION, POP_TOP); - _ADDOP(c, NO_LOCATION, POP_TOP); + ADDOP(c, NO_LOCATION, POP_TOP); /* exc_value */ + ADDOP(c, NO_LOCATION, POP_BLOCK); + ADDOP(c, NO_LOCATION, POP_EXCEPT); + ADDOP(c, NO_LOCATION, POP_TOP); + ADDOP(c, NO_LOCATION, POP_TOP); NEW_JUMP_TARGET_LABEL(c, exit); - _ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); + ADDOP_JUMP(c, NO_LOCATION, JUMP, exit); USE_LABEL(c, cleanup); - _POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); + POP_EXCEPT_AND_RERAISE(c, NO_LOCATION); USE_LABEL(c, exit); - return 1; + return SUCCESS; } /* @@ -5859,7 +5850,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) ADDOP_I(c, loc, GET_AWAITABLE, 2); ADDOP_LOAD_CONST(c, loc, Py_None); ADD_YIELD_FROM(c, loc, 1); - compiler_with_except_finish(c, cleanup); + RETURN_IF_ERROR(compiler_with_except_finish(c, cleanup)); USE_LABEL(c, exit); return SUCCESS; @@ -5946,7 +5937,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup); ADDOP(c, loc, PUSH_EXC_INFO); ADDOP(c, loc, WITH_EXCEPT_START); - compiler_with_except_finish(c, cleanup); + RETURN_IF_ERROR(compiler_with_except_finish(c, cleanup)); USE_LABEL(c, exit); return SUCCESS; From 95a47a6a27a1097ad0cd9711858139905698475e Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 23:17:51 +0000 Subject: [PATCH 68/93] compiler_lambda return SUCCESS/ERROR --- Python/compile.c | 50 ++++++++++++------------------------------------ 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 2488c3e16a0759..e4ca9308d27871 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1800,13 +1800,6 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return 0; \ } -#define _VISIT_IN_SCOPE(C, TYPE, V) {\ - if (!compiler_visit_ ## TYPE((C), (V))) { \ - compiler_exit_scope(c); \ - return 0; \ - } \ -} - #define _VISIT_SEQ(C, TYPE, SEQ) { \ int _i; \ asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ @@ -1817,22 +1810,6 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } \ } -#define _VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ - int _i; \ - asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ - for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ - TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ - if (!compiler_visit_ ## TYPE((C), elt)) { \ - compiler_exit_scope(c); \ - return 0; \ - } \ - } \ -} - -#define RETURN_IF_FALSE(X) \ - if (!(X)) { \ - return 0; \ - } static int compiler_enter_scope(struct compiler *c, identifier name, @@ -3166,9 +3143,7 @@ compiler_lambda(struct compiler *c, expr_ty e) arguments_ty args = e->v.Lambda.args; assert(e->kind == Lambda_kind); - if (compiler_check_debug_args(c, args) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_check_debug_args(c, args)); location loc = LOC(e); funcflags = compiler_default_arguments(c, loc, args); @@ -3177,43 +3152,42 @@ compiler_lambda(struct compiler *c, expr_ty e) } _Py_DECLARE_STR(anon_lambda, "<lambda>"); - if (compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA, - (void *)e, e->lineno) < 0) { - return 0; - } + RETURN_IF_ERROR( + compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA, + (void *)e, e->lineno)); + /* Make None the first constant, so the lambda can't have a docstring. */ - if (compiler_add_const(c, Py_None) < 0) - return 0; + RETURN_IF_ERROR(compiler_add_const(c, Py_None)); c->u->u_argcount = asdl_seq_LEN(args->args); c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); - _VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); + VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); if (c->u->u_ste->ste_generator) { co = assemble(c, 0); } else { location loc = LOCATION(e->lineno, e->lineno, 0, 0); - _ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); + ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); co = assemble(c, 1); } qualname = Py_NewRef(c->u->u_qualname); compiler_exit_scope(c); if (co == NULL) { Py_DECREF(qualname); - return 0; + return ERROR; } if (compiler_make_closure(c, loc, co, funcflags, qualname) < 0) { Py_DECREF(qualname); Py_DECREF(co); - return 0; + return ERROR; } Py_DECREF(qualname); Py_DECREF(co); - return 1; + return SUCCESS; } static int @@ -5965,7 +5939,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) _ADDOP(c, loc, unaryop(e->v.UnaryOp.op)); break; case Lambda_kind: - return compiler_lambda(c, e); + return compiler_lambda(c, e) == SUCCESS ? 1 : 0; case IfExp_kind: return compiler_ifexp(c, e) == SUCCESS ? 1 : 0; case Dict_kind: From 56178d36f89c4b5e49c416c59d3d717cb4a59723 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 23:27:44 +0000 Subject: [PATCH 69/93] compiler_visit_defaults return SUCCESS/ERROR --- Python/compile.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e4ca9308d27871..6162fa3480c91a 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2601,9 +2601,9 @@ static int compiler_visit_defaults(struct compiler *c, arguments_ty args, location loc) { - _VISIT_SEQ(c, expr, args->defaults); - _ADDOP_I(c, loc, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); - return 1; + VISIT_SEQ(c, expr, args->defaults); + ADDOP_I(c, loc, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); + return SUCCESS; } static Py_ssize_t @@ -2612,8 +2612,7 @@ compiler_default_arguments(struct compiler *c, location loc, { Py_ssize_t funcflags = 0; if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { - if (!compiler_visit_defaults(c, args, loc)) - return -1; + RETURN_IF_ERROR(compiler_visit_defaults(c, args, loc)); funcflags |= 0x01; } if (args->kwonlyargs) { From 0c6687d3afabeac4aabe357319109d361b998c94 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Sun, 4 Dec 2022 23:43:24 +0000 Subject: [PATCH 70/93] compiler_visit_*annotation(s) return SUCCESS/ERROR --- Python/compile.c | 62 ++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 6162fa3480c91a..6c411651baddb1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2511,17 +2511,17 @@ compiler_visit_argannotation(struct compiler *c, identifier id, expr_ty annotation, Py_ssize_t *annotations_len, location loc) { if (!annotation) { - return 1; + return SUCCESS; } PyObject *mangled = _Py_Mangle(c->u->u_private, id); if (!mangled) { - return 0; + return ERROR; } - _ADDOP_LOAD_CONST(c, loc, mangled); + ADDOP_LOAD_CONST(c, loc, mangled); Py_DECREF(mangled); if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) { - _VISIT(c, annexpr, annotation); + VISIT(c, annexpr, annotation); } else { if (annotation->kind == Starred_kind) { @@ -2529,15 +2529,15 @@ compiler_visit_argannotation(struct compiler *c, identifier id, // Do [annotation_value] = [*Ts]. // (Note that in theory we could end up here even for an argument // other than *args, but in practice the grammar doesn't allow it.) - _VISIT(c, expr, annotation->v.Starred.value); - _ADDOP_I(c, loc, UNPACK_SEQUENCE, (Py_ssize_t) 1); + VISIT(c, expr, annotation->v.Starred.value); + ADDOP_I(c, loc, UNPACK_SEQUENCE, (Py_ssize_t) 1); } else { - _VISIT(c, expr, annotation); + VISIT(c, expr, annotation); } } *annotations_len += 2; - return 1; + return SUCCESS; } static int @@ -2547,15 +2547,15 @@ compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, int i; for (i = 0; i < asdl_seq_LEN(args); i++) { arg_ty arg = (arg_ty)asdl_seq_GET(args, i); - if (!compiler_visit_argannotation( + RETURN_IF_ERROR( + compiler_visit_argannotation( c, arg->arg, arg->annotation, annotations_len, - loc)) - return 0; + loc)); } - return 1; + return SUCCESS; } static int @@ -2565,36 +2565,36 @@ compiler_visit_annotations(struct compiler *c, location loc, /* Push arg annotation names and values. The expressions are evaluated out-of-order wrt the source code. - Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed. + Return -1 on error, 0 if no annotations pushed, 1 if a annotations is pushed. */ Py_ssize_t annotations_len = 0; - if (!compiler_visit_argannotations(c, args->args, &annotations_len, loc)) - return 0; - if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len, loc)) - return 0; + if (compiler_visit_argannotations(c, args->args, &annotations_len, loc) < 0) + return ERROR; + if (compiler_visit_argannotations(c, args->posonlyargs, &annotations_len, loc) < 0) + return ERROR; if (args->vararg && args->vararg->annotation && - !compiler_visit_argannotation(c, args->vararg->arg, - args->vararg->annotation, &annotations_len, loc)) - return 0; - if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len, loc)) - return 0; + compiler_visit_argannotation(c, args->vararg->arg, + args->vararg->annotation, &annotations_len, loc) < 0) + return ERROR; + if (compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len, loc) < 0) + return ERROR; if (args->kwarg && args->kwarg->annotation && - !compiler_visit_argannotation(c, args->kwarg->arg, - args->kwarg->annotation, &annotations_len, loc)) - return 0; + compiler_visit_argannotation(c, args->kwarg->arg, + args->kwarg->annotation, &annotations_len, loc) < 0) + return ERROR; - if (!compiler_visit_argannotation(c, &_Py_ID(return), returns, - &annotations_len, loc)) { - return 0; + if (compiler_visit_argannotation(c, &_Py_ID(return), returns, + &annotations_len, loc) < 0) { + return ERROR; } if (annotations_len) { - _ADDOP_I(c, loc, BUILD_TUPLE, annotations_len); + ADDOP_I(c, loc, BUILD_TUPLE, annotations_len); return 1; } - return -1; + return 0; } static int @@ -2762,7 +2762,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) return ERROR; } annotations = compiler_visit_annotations(c, loc, args, returns); - if (annotations == 0) { + if (annotations == ERROR) { return ERROR; } else if (annotations > 0) { From 0ee940f7d16d4bb4230a2ff7824c657e6ed17ff8 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 00:03:37 +0000 Subject: [PATCH 71/93] compiler_comprehension_* return SUCCESS/ERROR --- Python/compile.c | 116 +++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 6c411651baddb1..a03333869ae40c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1668,7 +1668,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, #define POP_EXCEPT_AND_RERAISE(C, LOC) \ RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC))); -#define ADDOP_YIELD(C, LOC) { \ +#define ADDOP_YIELD(C, LOC) \ RETURN_IF_ERROR(addop_yield((C), (LOC))); /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use @@ -5341,7 +5341,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, if (gen_index == 0) { /* Receive outermost iter as an implicit argument */ c->u->u_argcount = 1; - _ADDOP_I(c, loc, LOAD_FAST, 0); + ADDOP_I(c, loc, LOAD_FAST, 0); } else { /* Sub-iter - calculate on the fly */ @@ -5362,37 +5362,36 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, if (asdl_seq_LEN(elts) == 1) { expr_ty elt = asdl_seq_GET(elts, 0); if (elt->kind != Starred_kind) { - _VISIT(c, expr, elt); + VISIT(c, expr, elt); start = NO_LABEL; } } if (IS_LABEL(start)) { - _VISIT(c, expr, gen->iter); - _ADDOP(c, loc, GET_ITER); + VISIT(c, expr, gen->iter); + ADDOP(c, loc, GET_ITER); } } if (IS_LABEL(start)) { depth++; USE_LABEL(c, start); - _ADDOP_JUMP(c, loc, FOR_ITER, anchor); + ADDOP_JUMP(c, loc, FOR_ITER, anchor); } - _VISIT(c, expr, gen->target); + VISIT(c, expr, gen->target); /* XXX this needs to be cleaned up...a lot! */ Py_ssize_t n = asdl_seq_LEN(gen->ifs); for (Py_ssize_t i = 0; i < n; i++) { expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) { - return 0; + return ERROR; } } if (++gen_index < asdl_seq_LEN(generators)) { - if (!compiler_comprehension_generator(c, loc, - generators, gen_index, depth, - elt, val, type)) { - return 0; - } + RETURN_IF_ERROR( + compiler_comprehension_generator(c, loc, + generators, gen_index, depth, + elt, val, type)); } location elt_loc = LOC(elt); @@ -5402,43 +5401,43 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, /* comprehension specific code */ switch (type) { case COMP_GENEXP: - _VISIT(c, expr, elt); - _ADDOP_YIELD(c, elt_loc); - _ADDOP(c, elt_loc, POP_TOP); + VISIT(c, expr, elt); + ADDOP_YIELD(c, elt_loc); + ADDOP(c, elt_loc, POP_TOP); break; case COMP_LISTCOMP: - _VISIT(c, expr, elt); - _ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); + VISIT(c, expr, elt); + ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); break; case COMP_SETCOMP: - _VISIT(c, expr, elt); - _ADDOP_I(c, elt_loc, SET_ADD, depth + 1); + VISIT(c, expr, elt); + ADDOP_I(c, elt_loc, SET_ADD, depth + 1); break; case COMP_DICTCOMP: /* With '{k: v}', k is evaluated before v, so we do the same. */ - _VISIT(c, expr, elt); - _VISIT(c, expr, val); + VISIT(c, expr, elt); + VISIT(c, expr, val); elt_loc = LOCATION(elt->lineno, val->end_lineno, elt->col_offset, val->end_col_offset); - _ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); + ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); break; default: - return 0; + return ERROR; } } USE_LABEL(c, if_cleanup); if (IS_LABEL(start)) { - _ADDOP_JUMP(c, elt_loc, JUMP, start); + ADDOP_JUMP(c, elt_loc, JUMP, start); USE_LABEL(c, anchor); - _ADDOP(c, NO_LOCATION, END_FOR); + ADDOP(c, NO_LOCATION, END_FOR); } - return 1; + return SUCCESS; } static int @@ -5457,43 +5456,42 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, if (gen_index == 0) { /* Receive outermost iter as an implicit argument */ c->u->u_argcount = 1; - _ADDOP_I(c, loc, LOAD_FAST, 0); + ADDOP_I(c, loc, LOAD_FAST, 0); } else { /* Sub-iter - calculate on the fly */ - _VISIT(c, expr, gen->iter); - _ADDOP(c, loc, GET_AITER); + VISIT(c, expr, gen->iter); + ADDOP(c, loc, GET_AITER); } USE_LABEL(c, start); /* Runtime will push a block here, so we need to account for that */ if (compiler_push_fblock(c, loc, ASYNC_COMPREHENSION_GENERATOR, start, NO_LABEL, NULL) < 0) { - return 0; + return ERROR; } - _ADDOP_JUMP(c, loc, SETUP_FINALLY, except); - _ADDOP(c, loc, GET_ANEXT); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); - _ADDOP(c, loc, POP_BLOCK); - _VISIT(c, expr, gen->target); + ADDOP_JUMP(c, loc, SETUP_FINALLY, except); + ADDOP(c, loc, GET_ANEXT); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); + ADDOP(c, loc, POP_BLOCK); + VISIT(c, expr, gen->target); Py_ssize_t n = asdl_seq_LEN(gen->ifs); for (Py_ssize_t i = 0; i < n; i++) { expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) { - return 0; + return ERROR; } } depth++; if (++gen_index < asdl_seq_LEN(generators)) { - if (!compiler_comprehension_generator(c, loc, - generators, gen_index, depth, - elt, val, type)) { - return 0; - } + RETURN_IF_ERROR( + compiler_comprehension_generator(c, loc, + generators, gen_index, depth, + elt, val, type)); } location elt_loc = LOC(elt); @@ -5502,44 +5500,44 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, /* comprehension specific code */ switch (type) { case COMP_GENEXP: - _VISIT(c, expr, elt); - _ADDOP_YIELD(c, elt_loc); - _ADDOP(c, elt_loc, POP_TOP); + VISIT(c, expr, elt); + ADDOP_YIELD(c, elt_loc); + ADDOP(c, elt_loc, POP_TOP); break; case COMP_LISTCOMP: - _VISIT(c, expr, elt); - _ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); + VISIT(c, expr, elt); + ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1); break; case COMP_SETCOMP: - _VISIT(c, expr, elt); - _ADDOP_I(c, elt_loc, SET_ADD, depth + 1); + VISIT(c, expr, elt); + ADDOP_I(c, elt_loc, SET_ADD, depth + 1); break; case COMP_DICTCOMP: /* With '{k: v}', k is evaluated before v, so we do the same. */ - _VISIT(c, expr, elt); - _VISIT(c, expr, val); + VISIT(c, expr, elt); + VISIT(c, expr, val); elt_loc = LOCATION(elt->lineno, val->end_lineno, elt->col_offset, val->end_col_offset); - _ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); + ADDOP_I(c, elt_loc, MAP_ADD, depth + 1); break; default: - return 0; + return ERROR; } } USE_LABEL(c, if_cleanup); - _ADDOP_JUMP(c, elt_loc, JUMP, start); + ADDOP_JUMP(c, elt_loc, JUMP, start); compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start); USE_LABEL(c, except); - _ADDOP(c, loc, END_ASYNC_FOR); + ADDOP(c, loc, END_ASYNC_FOR); - return 1; + return SUCCESS; } static int @@ -5595,8 +5593,8 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, _ADDOP_I(c, loc, op, 0); } - if (!compiler_comprehension_generator(c, loc, generators, 0, 0, - elt, val, type)) { + if (compiler_comprehension_generator(c, loc, generators, 0, 0, + elt, val, type) < 0) { goto error_in_scope; } From 6a37392febe927a934055b101f1f4d04556aff21 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 09:12:04 +0000 Subject: [PATCH 72/93] starunpack_helper return SUCCESS/ERROR --- Python/compile.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index a03333869ae40c..a04f074d857962 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4466,7 +4466,7 @@ starunpack_helper(struct compiler *c, location loc, if (n > 2 && are_all_items_const(elts, 0, n)) { PyObject *folded = PyTuple_New(n); if (folded == NULL) { - return 0; + return ERROR; } PyObject *val; for (Py_ssize_t i = 0; i < n; i++) { @@ -4474,22 +4474,22 @@ starunpack_helper(struct compiler *c, location loc, PyTuple_SET_ITEM(folded, i, Py_NewRef(val)); } if (tuple && !pushed) { - _ADDOP_LOAD_CONST_NEW(c, loc, folded); + ADDOP_LOAD_CONST_NEW(c, loc, folded); } else { if (add == SET_ADD) { Py_SETREF(folded, PyFrozenSet_New(folded)); if (folded == NULL) { - return 0; + return ERROR; } } - _ADDOP_I(c, loc, build, pushed); - _ADDOP_LOAD_CONST_NEW(c, loc, folded); - _ADDOP_I(c, loc, extend, 1); + ADDOP_I(c, loc, build, pushed); + ADDOP_LOAD_CONST_NEW(c, loc, folded); + ADDOP_I(c, loc, extend, 1); if (tuple) { - _ADDOP(c, loc, LIST_TO_TUPLE); + ADDOP(c, loc, LIST_TO_TUPLE); } } - return 1; + return SUCCESS; } int big = n+pushed > STACK_USE_GUIDELINE; @@ -4504,42 +4504,42 @@ starunpack_helper(struct compiler *c, location loc, if (!seen_star && !big) { for (Py_ssize_t i = 0; i < n; i++) { expr_ty elt = asdl_seq_GET(elts, i); - _VISIT(c, expr, elt); + VISIT(c, expr, elt); } if (tuple) { - _ADDOP_I(c, loc, BUILD_TUPLE, n+pushed); + ADDOP_I(c, loc, BUILD_TUPLE, n+pushed); } else { - _ADDOP_I(c, loc, build, n+pushed); + ADDOP_I(c, loc, build, n+pushed); } - return 1; + return SUCCESS; } int sequence_built = 0; if (big) { - _ADDOP_I(c, loc, build, pushed); + ADDOP_I(c, loc, build, pushed); sequence_built = 1; } for (Py_ssize_t i = 0; i < n; i++) { expr_ty elt = asdl_seq_GET(elts, i); if (elt->kind == Starred_kind) { if (sequence_built == 0) { - _ADDOP_I(c, loc, build, i+pushed); + ADDOP_I(c, loc, build, i+pushed); sequence_built = 1; } - _VISIT(c, expr, elt->v.Starred.value); - _ADDOP_I(c, loc, extend, 1); + VISIT(c, expr, elt->v.Starred.value); + ADDOP_I(c, loc, extend, 1); } else { - _VISIT(c, expr, elt); + VISIT(c, expr, elt); if (sequence_built) { - _ADDOP_I(c, loc, add, 1); + ADDOP_I(c, loc, add, 1); } } } assert(sequence_built); if (tuple) { - _ADDOP(c, loc, LIST_TO_TUPLE); + ADDOP(c, loc, LIST_TO_TUPLE); } - return 1; + return SUCCESS; } static int @@ -4594,7 +4594,7 @@ compiler_list(struct compiler *c, expr_ty e) } else if (e->v.List.ctx == Load) { return starunpack_helper(c, loc, elts, 0, - BUILD_LIST, LIST_APPEND, LIST_EXTEND, 0); + BUILD_LIST, LIST_APPEND, LIST_EXTEND, 0) == SUCCESS ? 1 : 0; } else _VISIT_SEQ(c, expr, elts); @@ -4611,7 +4611,7 @@ compiler_tuple(struct compiler *c, expr_ty e) } else if (e->v.Tuple.ctx == Load) { return starunpack_helper(c, loc, elts, 0, - BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1); + BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1) == SUCCESS ? 1 : 0; } else _VISIT_SEQ(c, expr, elts); @@ -4623,7 +4623,7 @@ compiler_set(struct compiler *c, expr_ty e) { location loc = LOC(e); return starunpack_helper(c, loc, e->v.Set.elts, 0, - BUILD_SET, SET_ADD, SET_UPDATE, 0); + BUILD_SET, SET_ADD, SET_UPDATE, 0) == SUCCESS ? 1 : 0; } static int @@ -5240,7 +5240,7 @@ compiler_call_helper(struct compiler *c, location loc, VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); } else if (starunpack_helper(c, loc, args, n, BUILD_LIST, - LIST_APPEND, LIST_EXTEND, 1) == 0) { + LIST_APPEND, LIST_EXTEND, 1) < 0) { return ERROR; } /* Then keyword arguments */ From 40cb86c7c67356607f92ecfd5b2cfdd653c60018 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 09:20:55 +0000 Subject: [PATCH 73/93] compiler_list, compiler_tuple, compiler_subdict, compiler_dict return SUCCESS/ERROR --- Python/compile.c | 82 +++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index a04f074d857962..f5dfd23e7912ed 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4590,15 +4590,16 @@ compiler_list(struct compiler *c, expr_ty e) location loc = LOC(e); asdl_expr_seq *elts = e->v.List.elts; if (e->v.List.ctx == Store) { - return assignment_helper(c, loc, elts) == SUCCESS ? 1 : 0; + return assignment_helper(c, loc, elts); } else if (e->v.List.ctx == Load) { return starunpack_helper(c, loc, elts, 0, - BUILD_LIST, LIST_APPEND, LIST_EXTEND, 0) == SUCCESS ? 1 : 0; + BUILD_LIST, LIST_APPEND, LIST_EXTEND, 0); } - else - _VISIT_SEQ(c, expr, elts); - return 1; + else { + VISIT_SEQ(c, expr, elts); + } + return SUCCESS; } static int @@ -4607,15 +4608,16 @@ compiler_tuple(struct compiler *c, expr_ty e) location loc = LOC(e); asdl_expr_seq *elts = e->v.Tuple.elts; if (e->v.Tuple.ctx == Store) { - return assignment_helper(c, loc, elts) == SUCCESS ? 1 : 0; + return assignment_helper(c, loc, elts); } else if (e->v.Tuple.ctx == Load) { return starunpack_helper(c, loc, elts, 0, - BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1) == SUCCESS ? 1 : 0; + BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1); } - else - _VISIT_SEQ(c, expr, elts); - return 1; + else { + VISIT_SEQ(c, expr, elts); + } + return SUCCESS; } static int @@ -4623,7 +4625,7 @@ compiler_set(struct compiler *c, expr_ty e) { location loc = LOC(e); return starunpack_helper(c, loc, e->v.Set.elts, 0, - BUILD_SET, SET_ADD, SET_UPDATE, 0) == SUCCESS ? 1 : 0; + BUILD_SET, SET_ADD, SET_UPDATE, 0); } static int @@ -4647,34 +4649,34 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end location loc = LOC(e); if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) { for (i = begin; i < end; i++) { - _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); } keys = PyTuple_New(n); if (keys == NULL) { - return 0; + return SUCCESS; } for (i = begin; i < end; i++) { key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key)); } - _ADDOP_LOAD_CONST_NEW(c, loc, keys); - _ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); - return 1; + ADDOP_LOAD_CONST_NEW(c, loc, keys); + ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); + return SUCCESS; } if (big) { - _ADDOP_I(c, loc, BUILD_MAP, 0); + ADDOP_I(c, loc, BUILD_MAP, 0); } for (i = begin; i < end; i++) { - _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); - _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); if (big) { - _ADDOP_I(c, loc, MAP_ADD, 1); + ADDOP_I(c, loc, MAP_ADD, 1); } } if (!big) { - _ADDOP_I(c, loc, BUILD_MAP, n); + ADDOP_I(c, loc, BUILD_MAP, n); } - return 1; + return SUCCESS; } static int @@ -4691,29 +4693,25 @@ compiler_dict(struct compiler *c, expr_ty e) is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; if (is_unpacking) { if (elements) { - if (!compiler_subdict(c, e, i - elements, i)) { - return 0; - } + RETURN_IF_ERROR(compiler_subdict(c, e, i - elements, i)); if (have_dict) { - _ADDOP_I(c, loc, DICT_UPDATE, 1); + ADDOP_I(c, loc, DICT_UPDATE, 1); } have_dict = 1; elements = 0; } if (have_dict == 0) { - _ADDOP_I(c, loc, BUILD_MAP, 0); + ADDOP_I(c, loc, BUILD_MAP, 0); have_dict = 1; } - _VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); - _ADDOP_I(c, loc, DICT_UPDATE, 1); + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); + ADDOP_I(c, loc, DICT_UPDATE, 1); } else { if (elements*2 > STACK_USE_GUIDELINE) { - if (!compiler_subdict(c, e, i - elements, i + 1)) { - return 0; - } + RETURN_IF_ERROR(compiler_subdict(c, e, i - elements, i + 1)); if (have_dict) { - _ADDOP_I(c, loc, DICT_UPDATE, 1); + ADDOP_I(c, loc, DICT_UPDATE, 1); } have_dict = 1; elements = 0; @@ -4724,18 +4722,16 @@ compiler_dict(struct compiler *c, expr_ty e) } } if (elements) { - if (!compiler_subdict(c, e, n - elements, n)) { - return 0; - } + RETURN_IF_ERROR(compiler_subdict(c, e, n - elements, n)); if (have_dict) { - _ADDOP_I(c, loc, DICT_UPDATE, 1); + ADDOP_I(c, loc, DICT_UPDATE, 1); } have_dict = 1; } if (!have_dict) { - _ADDOP_I(c, loc, BUILD_MAP, 0); + ADDOP_I(c, loc, BUILD_MAP, 0); } - return 1; + return SUCCESS; } static int @@ -5940,9 +5936,9 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case IfExp_kind: return compiler_ifexp(c, e) == SUCCESS ? 1 : 0; case Dict_kind: - return compiler_dict(c, e); + return compiler_dict(c, e) == SUCCESS ? 1 : 0; case Set_kind: - return compiler_set(c, e); + return compiler_set(c, e) == SUCCESS ? 1 : 0; case GeneratorExp_kind: return compiler_genexp(c, e); case ListComp_kind: @@ -6049,9 +6045,9 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx); /* child nodes of List and Tuple will have expr_context set */ case List_kind: - return compiler_list(c, e); + return compiler_list(c, e) == SUCCESS ? 1 : 0; case Tuple_kind: - return compiler_tuple(c, e); + return compiler_tuple(c, e) == SUCCESS ? 1 : 0; } return 1; } From b8cd5a36563a4994b0714c856099c833905f1002 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 09:24:14 +0000 Subject: [PATCH 74/93] are_all_items_const returns bool --- Python/compile.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index f5dfd23e7912ed..a1aeba9a3545b3 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -511,7 +511,7 @@ static int compiler_annassign(struct compiler *, stmt_ty); static int compiler_subscript(struct compiler *, expr_ty); static int compiler_slice(struct compiler *, expr_ty); -static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t); +static bool are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t); static int compiler_with(struct compiler *, stmt_ty, int); @@ -4628,16 +4628,16 @@ compiler_set(struct compiler *c, expr_ty e) BUILD_SET, SET_ADD, SET_UPDATE, 0); } -static int +static bool are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end) { - Py_ssize_t i; - for (i = begin; i < end; i++) { + for (Py_ssize_t i = begin; i < end; i++) { expr_ty key = (expr_ty)asdl_seq_GET(seq, i); - if (key == NULL || key->kind != Constant_kind) - return 0; + if (key == NULL || key->kind != Constant_kind) { + return false; + } } - return 1; + return true; } static int From 1bc7cc69adf89fd130ac0a4e422605b1517702da Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 09:28:56 +0000 Subject: [PATCH 75/93] compiler_comprehension/listcomp/dictcomp/setcomp/genexp return SUCCESS/ERROR --- Python/compile.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index a1aeba9a3545b3..07e65bc9a43233 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5586,7 +5586,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, goto error_in_scope; } - _ADDOP_I(c, loc, op, 0); + ADDOP_I(c, loc, op, 0); } if (compiler_comprehension_generator(c, loc, generators, 0, 0, @@ -5595,7 +5595,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, } if (type != COMP_GENEXP) { - _ADDOP(c, LOC(e), RETURN_VALUE); + ADDOP(c, LOC(e), RETURN_VALUE); } if (type == COMP_GENEXP) { if (wrap_in_stopiteration_handler(c) < 0) { @@ -5609,8 +5609,9 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, if (is_top_level_await && is_async_generator){ c->u->u_ste->ste_coroutine = 1; } - if (co == NULL) + if (co == NULL) { goto error; + } loc = LOC(e); if (compiler_make_closure(c, loc, co, 0, qualname) < 0) { @@ -5619,30 +5620,30 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type, Py_DECREF(qualname); Py_DECREF(co); - _VISIT(c, expr, outermost->iter); + VISIT(c, expr, outermost->iter); loc = LOC(e); if (outermost->is_async) { - _ADDOP(c, loc, GET_AITER); + ADDOP(c, loc, GET_AITER); } else { - _ADDOP(c, loc, GET_ITER); + ADDOP(c, loc, GET_ITER); } - _ADDOP_I(c, loc, CALL, 0); + ADDOP_I(c, loc, CALL, 0); if (is_async_generator && type != COMP_GENEXP) { - _ADDOP_I(c, loc, GET_AWAITABLE, 0); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); + ADDOP_I(c, loc, GET_AWAITABLE, 0); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); } - return 1; + return SUCCESS; error_in_scope: compiler_exit_scope(c); error: Py_XDECREF(qualname); Py_XDECREF(co); - return 0; + return ERROR; } static int @@ -5940,13 +5941,13 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Set_kind: return compiler_set(c, e) == SUCCESS ? 1 : 0; case GeneratorExp_kind: - return compiler_genexp(c, e); + return compiler_genexp(c, e) == SUCCESS ? 1 : 0; case ListComp_kind: - return compiler_listcomp(c, e); + return compiler_listcomp(c, e) == SUCCESS ? 1 : 0; case SetComp_kind: - return compiler_setcomp(c, e); + return compiler_setcomp(c, e) == SUCCESS ? 1 : 0; case DictComp_kind: - return compiler_dictcomp(c, e); + return compiler_dictcomp(c, e) == SUCCESS ? 1 : 0; case Yield_kind: if (c->u->u_ste->ste_type != FunctionBlock) return compiler_error(c, loc, "'yield' outside function"); From 93af682aa0ab57b1ef640e62b0c1a12b7d39b21b Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 09:40:11 +0000 Subject: [PATCH 76/93] compiler_jump_if returns SUCCESS/ERROR --- Python/compile.c | 125 +++++++++++++++++++---------------------------- 1 file changed, 49 insertions(+), 76 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 07e65bc9a43233..52c4ddbdc0bb9f 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1636,7 +1636,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_N(C, LOC, OP, O, TYPE) { \ - assert(!HAS_CONST(OP)); /* use _ADDOP_LOAD_CONST_NEW */ \ + assert(!HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \ if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) { \ Py_DECREF((O)); \ return ERROR; \ @@ -1720,13 +1720,6 @@ cfg_builder_addop_j(cfg_builder *g, location loc, return 0; \ } -#define _ADDOP_IN_SCOPE(C, LOC, OP) { \ - if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) { \ - compiler_exit_scope(c); \ - return 0; \ - } \ -} - #define _ADDOP_LOAD_CONST(C, LOC, O) { \ if (compiler_addop_load_const((C), (LOC), (O)) < 0) \ return 0; \ @@ -3037,67 +3030,60 @@ compiler_jump_if(struct compiler *c, location loc, next2 = new_next2; } for (i = 0; i < n; ++i) { - if (!compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) { - return 0; - } - } - if (!compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, n), next, cond)) { - return 0; + RETURN_IF_ERROR( + compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, i), next2, cond2)); } + RETURN_IF_ERROR( + compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, n), next, cond)); if (!SAME_LABEL(next2, next)) { USE_LABEL(c, next2); } - return 1; + return SUCCESS; } case IfExp_kind: { NEW_JUMP_TARGET_LABEL(c, end); NEW_JUMP_TARGET_LABEL(c, next2); - if (!compiler_jump_if(c, loc, e->v.IfExp.test, next2, 0)) { - return 0; - } - if (!compiler_jump_if(c, loc, e->v.IfExp.body, next, cond)) { - return 0; - } - _ADDOP_JUMP(c, NO_LOCATION, JUMP, end); + RETURN_IF_ERROR( + compiler_jump_if(c, loc, e->v.IfExp.test, next2, 0)); + RETURN_IF_ERROR( + compiler_jump_if(c, loc, e->v.IfExp.body, next, cond)); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, next2); - if (!compiler_jump_if(c, loc, e->v.IfExp.orelse, next, cond)) { - return 0; - } + RETURN_IF_ERROR( + compiler_jump_if(c, loc, e->v.IfExp.orelse, next, cond)); USE_LABEL(c, end); - return 1; + return SUCCESS; } case Compare_kind: { Py_ssize_t n = asdl_seq_LEN(e->v.Compare.ops) - 1; if (n > 0) { - if (check_compare(c, e) < 0) { - return 0; - } + RETURN_IF_ERROR(check_compare(c, e)); NEW_JUMP_TARGET_LABEL(c, cleanup); - _VISIT(c, expr, e->v.Compare.left); + VISIT(c, expr, e->v.Compare.left); for (Py_ssize_t i = 0; i < n; i++) { - _VISIT(c, expr, + VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); - _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_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_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); + 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_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_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); + ADDOP_JUMP(c, NO_LOCATION, JUMP, end); USE_LABEL(c, cleanup); - _ADDOP(c, LOC(e), POP_TOP); + ADDOP(c, LOC(e), POP_TOP); if (!cond) { - _ADDOP_JUMP(c, NO_LOCATION, JUMP, next); + ADDOP_JUMP(c, NO_LOCATION, JUMP, next); } USE_LABEL(c, end); - return 1; + return SUCCESS; } /* fallback to general implementation */ break; @@ -3108,9 +3094,9 @@ compiler_jump_if(struct compiler *c, location loc, } /* general implementation */ - _VISIT(c, expr, e); - _ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); - return 1; + VISIT(c, expr, e); + ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); + return SUCCESS; } static int @@ -3120,9 +3106,9 @@ compiler_ifexp(struct compiler *c, expr_ty e) NEW_JUMP_TARGET_LABEL(c, end); NEW_JUMP_TARGET_LABEL(c, next); - if (!compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0)) { - return ERROR; - } + RETURN_IF_ERROR( + compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0)); + VISIT(c, expr, e->v.IfExp.body); ADDOP_JUMP(c, NO_LOCATION, JUMP, end); @@ -3202,9 +3188,9 @@ compiler_if(struct compiler *c, stmt_ty s) else { next = end; } - if (!compiler_jump_if(c, LOC(s), s->v.If.test, next, 0)) { - return ERROR; - } + RETURN_IF_ERROR( + compiler_jump_if(c, LOC(s), s->v.If.test, next, 0)); + VISIT_SEQ(c, stmt, s->v.If.body); if (asdl_seq_LEN(s->v.If.orelse)) { ADDOP_JUMP(c, NO_LOCATION, JUMP, end); @@ -3314,15 +3300,11 @@ compiler_while(struct compiler *c, stmt_ty s) USE_LABEL(c, loop); RETURN_IF_ERROR(compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL)); - if (!compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0)); USE_LABEL(c, body); VISIT_SEQ(c, stmt, s->v.While.body); - if (!compiler_jump_if(c, LOC(s), s->v.While.test, body, 1)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.While.test, body, 1)); compiler_pop_fblock(c, WHILE_LOOP, loop); @@ -4113,9 +4095,7 @@ compiler_assert(struct compiler *c, stmt_ty s) return SUCCESS; } NEW_JUMP_TARGET_LABEL(c, end); - if (!compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1)); ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR); if (s->v.Assert.msg) { VISIT(c, expr, s->v.Assert.msg); @@ -4740,9 +4720,7 @@ compiler_compare(struct compiler *c, expr_ty e) location loc = LOC(e); Py_ssize_t i, n; - if (check_compare(c, e) < 0) { - return 0; - } + RETURN_IF_ERROR(check_compare(c, e)); VISIT(c, expr, e->v.Compare.left); assert(asdl_seq_LEN(e->v.Compare.ops) > 0); n = asdl_seq_LEN(e->v.Compare.ops) - 1; @@ -5378,9 +5356,7 @@ compiler_sync_comprehension_generator(struct compiler *c, location loc, Py_ssize_t n = asdl_seq_LEN(gen->ifs); for (Py_ssize_t i = 0; i < n; i++) { expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); - if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_jump_if(c, loc, e, if_cleanup, 0)); } if (++gen_index < asdl_seq_LEN(generators)) { @@ -5477,9 +5453,7 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, Py_ssize_t n = asdl_seq_LEN(gen->ifs); for (Py_ssize_t i = 0; i < n; i++) { expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); - if (!compiler_jump_if(c, loc, e, if_cleanup, 0)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_jump_if(c, loc, e, if_cleanup, 0)); } depth++; @@ -7213,9 +7187,9 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) // NOTE: Returning macros are safe again. if (m->guard) { RETURN_IF_ERROR(ensure_fail_pop(c, pc, 0)); - if (!compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0)) { - return ERROR; - } + RETURN_IF_ERROR( + compiler_jump_if(c, LOC(m->pattern), m->guard, + pc->fail_pop[0], 0)); } // Success! Pop the subject off, we're done with it: if (i != cases - has_default - 1) { @@ -7241,9 +7215,8 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) ADDOP(c, LOC(m->pattern), NOP); } if (m->guard) { - if (!compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)) { - return ERROR; - } + RETURN_IF_ERROR( + compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)); } VISIT_SEQ(c, stmt, m->body); } From 13b6f8d3f65039f7e55117b8df10aa116ae2ed95 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 09:56:06 +0000 Subject: [PATCH 77/93] maybe_optimize_method_call, compiler_call_simple_kw_helper return SUCCESS/ERROR --- Python/compile.c | 52 ++++++++++++++++++++---------------------------- 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 52c4ddbdc0bb9f..fd7bd442447ea7 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -63,11 +63,6 @@ return -1; \ } -#define RETURN_NULL_IF_ERROR(X) \ - if ((X) == -1) { \ - return NULL; \ - } - /* If we exceed this limit, it should * be considered a compiler bug. * Currently it should be impossible @@ -4917,7 +4912,7 @@ update_start_location_to_match_attr(struct compiler *c, location loc, return loc; } -// Return 1 if the method call was optimized, -1 if not, and 0 on error. +// Return 1 if the method call was optimized, 0 if not, and -1 on error. static int maybe_optimize_method_call(struct compiler *c, expr_ty e) { @@ -4928,49 +4923,48 @@ maybe_optimize_method_call(struct compiler *c, expr_ty e) /* Check that the call node is an attribute access */ if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load) { - return -1; + return 0; } /* Check that the base object is not something that is imported */ if (is_import_originated(c, meth->v.Attribute.value)) { - return -1; + return 0; } /* Check that there aren't too many arguments */ argsl = asdl_seq_LEN(args); kwdsl = asdl_seq_LEN(kwds); if (argsl + kwdsl + (kwdsl != 0) >= STACK_USE_GUIDELINE) { - return -1; + return 0; } /* Check that there are no *varargs types of arguments. */ for (i = 0; i < argsl; i++) { expr_ty elt = asdl_seq_GET(args, i); if (elt->kind == Starred_kind) { - return -1; + return 0; } } for (i = 0; i < kwdsl; i++) { keyword_ty kw = asdl_seq_GET(kwds, i); if (kw->arg == NULL) { - return -1; + return 0; } } /* Alright, we can optimize the code. */ - _VISIT(c, expr, meth->v.Attribute.value); + VISIT(c, expr, meth->v.Attribute.value); location loc = LOC(meth); loc = update_start_location_to_match_attr(c, loc, meth); - _ADDOP_NAME(c, loc, LOAD_METHOD, meth->v.Attribute.attr, names); - _VISIT_SEQ(c, expr, e->v.Call.args); + ADDOP_NAME(c, loc, LOAD_METHOD, meth->v.Attribute.attr, names); + VISIT_SEQ(c, expr, e->v.Call.args); if (kwdsl) { - _VISIT_SEQ(c, keyword, kwds); - if (!compiler_call_simple_kw_helper(c, loc, kwds, kwdsl)) { - return 0; - }; + VISIT_SEQ(c, keyword, kwds); + RETURN_IF_ERROR( + compiler_call_simple_kw_helper(c, loc, kwds, kwdsl)); } loc = update_start_location_to_match_attr(c, LOC(e), meth); - _ADDOP_I(c, loc, CALL, argsl + kwdsl); + ADDOP_I(c, loc, CALL, argsl + kwdsl); return 1; } @@ -5003,12 +4997,12 @@ compiler_call(struct compiler *c, expr_ty e) { RETURN_IF_ERROR(validate_keywords(c, e->v.Call.keywords)); int ret = maybe_optimize_method_call(c, e); + if (ret < 0) { + return ERROR; + } if (ret == 1) { return SUCCESS; } - if (ret == 0) { - return ERROR; - } RETURN_IF_ERROR(check_caller(c, e->v.Call.func)); location loc = LOC(e->v.Call.func); ADDOP(c, loc, PUSH_NULL); @@ -5137,7 +5131,6 @@ compiler_subkwargs(struct compiler *c, location loc, /* Used by compiler_call_helper and maybe_optimize_method_call to emit * KW_NAMES before CALL. - * Returns 1 on success, 0 on error. */ static int compiler_call_simple_kw_helper(struct compiler *c, location loc, @@ -5146,7 +5139,7 @@ compiler_call_simple_kw_helper(struct compiler *c, location loc, PyObject *names; names = PyTuple_New(nkwelts); if (names == NULL) { - return 0; + return ERROR; } for (int i = 0; i < nkwelts; i++) { keyword_ty kw = asdl_seq_GET(keywords, i); @@ -5154,11 +5147,11 @@ compiler_call_simple_kw_helper(struct compiler *c, location loc, } Py_ssize_t arg = compiler_add_const(c, names); if (arg < 0) { - return 0; + return ERROR; } Py_DECREF(names); - _ADDOP_I(c, loc, KW_NAMES, arg); - return 1; + ADDOP_I(c, loc, KW_NAMES, arg); + return SUCCESS; } @@ -5200,9 +5193,8 @@ compiler_call_helper(struct compiler *c, location loc, } if (nkwelts) { VISIT_SEQ(c, keyword, keywords); - if (!compiler_call_simple_kw_helper(c, loc, keywords, nkwelts)) { - return ERROR; - }; + RETURN_IF_ERROR( + compiler_call_simple_kw_helper(c, loc, keywords, nkwelts)); } ADDOP_I(c, loc, CALL, n + nelts + nkwelts); return SUCCESS; From 3bfb5d7197b0556375ee4561a75192f1263940af Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 10:00:59 +0000 Subject: [PATCH 78/93] compiler_joined_str, compiler_formatted_value return SUCCESS/ERROR --- Python/compile.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index fd7bd442447ea7..d1ff112244aa7e 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5020,22 +5020,22 @@ compiler_joined_str(struct compiler *c, expr_ty e) Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); if (value_count > STACK_USE_GUIDELINE) { _Py_DECLARE_STR(empty, ""); - _ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty))); - _ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names); - _ADDOP_I(c, loc, BUILD_LIST, 0); + ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty))); + ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names); + ADDOP_I(c, loc, BUILD_LIST, 0); for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) { - _VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); - _ADDOP_I(c, loc, LIST_APPEND, 1); + VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i)); + ADDOP_I(c, loc, LIST_APPEND, 1); } - _ADDOP_I(c, loc, CALL, 1); + ADDOP_I(c, loc, CALL, 1); } else { - _VISIT_SEQ(c, expr, e->v.JoinedStr.values); + VISIT_SEQ(c, expr, e->v.JoinedStr.values); if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) { - _ADDOP_I(c, loc, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); + ADDOP_I(c, loc, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); } } - return 1; + return SUCCESS; } /* Used to implement f-strings. Format a single value. */ @@ -5060,7 +5060,7 @@ compiler_formatted_value(struct compiler *c, expr_ty e) int oparg; /* The expression to be formatted. */ - _VISIT(c, expr, e->v.FormattedValue.value); + VISIT(c, expr, e->v.FormattedValue.value); switch (conversion) { case 's': oparg = FVC_STR; break; @@ -5070,19 +5070,19 @@ compiler_formatted_value(struct compiler *c, expr_ty e) default: PyErr_Format(PyExc_SystemError, "Unrecognized conversion character %d", conversion); - return 0; + return ERROR; } if (e->v.FormattedValue.format_spec) { /* Evaluate the format spec, and update our opcode arg. */ - _VISIT(c, expr, e->v.FormattedValue.format_spec); + VISIT(c, expr, e->v.FormattedValue.format_spec); oparg |= FVS_HAVE_SPEC; } /* And push our opcode and oparg */ location loc = LOC(e); - _ADDOP_I(c, loc, FORMAT_VALUE, oparg); + ADDOP_I(c, loc, FORMAT_VALUE, oparg); - return 1; + return SUCCESS; } static int @@ -5962,9 +5962,9 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) _ADDOP_LOAD_CONST(c, loc, e->v.Constant.value); break; case JoinedStr_kind: - return compiler_joined_str(c, e); + return compiler_joined_str(c, e) == SUCCESS ? 1 : 0; case FormattedValue_kind: - return compiler_formatted_value(c, e); + return compiler_formatted_value(c, e) == SUCCESS ? 1 : 0; /* The following exprs can be assignment targets. */ case Attribute_kind: _VISIT(c, expr, e->v.Attribute.value); From 6d40de71e28fca438963f79c75cf7f4068bd1d7f Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 10:02:58 +0000 Subject: [PATCH 79/93] compiler_slice returns SUCCESS/ERROR --- Python/compile.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index d1ff112244aa7e..96766da23fc8e9 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6002,7 +6002,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Slice_kind: { int n = compiler_slice(c, e); - if (n == 0) { + if (n < 0) { return 0; } _ADDOP_I(c, loc, BUILD_SLICE, n); @@ -6051,9 +6051,7 @@ compiler_augassign(struct compiler *c, stmt_ty s) case Subscript_kind: VISIT(c, expr, e->v.Subscript.value); if (is_two_element_slice(e->v.Subscript.slice)) { - if (!compiler_slice(c, e->v.Subscript.slice)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_slice(c, e->v.Subscript.slice)); ADDOP_I(c, loc, COPY, 3); ADDOP_I(c, loc, COPY, 3); ADDOP_I(c, loc, COPY, 3); @@ -6315,7 +6313,7 @@ compiler_subscript(struct compiler *c, expr_ty e) _VISIT(c, expr, e->v.Subscript.value); if (is_two_element_slice(e->v.Subscript.slice) && ctx != Del) { - if (!compiler_slice(c, e->v.Subscript.slice)) { + if (compiler_slice(c, e->v.Subscript.slice) < 0) { return 0; } if (ctx == Load) { @@ -6349,22 +6347,22 @@ compiler_slice(struct compiler *c, expr_ty s) /* only handles the cases where BUILD_SLICE is emitted */ if (s->v.Slice.lower) { - _VISIT(c, expr, s->v.Slice.lower); + VISIT(c, expr, s->v.Slice.lower); } else { - _ADDOP_LOAD_CONST(c, LOC(s), Py_None); + ADDOP_LOAD_CONST(c, LOC(s), Py_None); } if (s->v.Slice.upper) { - _VISIT(c, expr, s->v.Slice.upper); + VISIT(c, expr, s->v.Slice.upper); } else { - _ADDOP_LOAD_CONST(c, LOC(s), Py_None); + ADDOP_LOAD_CONST(c, LOC(s), Py_None); } if (s->v.Slice.step) { n++; - _VISIT(c, expr, s->v.Slice.step); + VISIT(c, expr, s->v.Slice.step); } return n; } From 49da813658a87cc184f70822fe81c7f7f78d83fa Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 10:37:21 +0000 Subject: [PATCH 80/93] compiler_nameop returns SUCCESS/ERROR --- Python/compile.c | 91 ++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 96766da23fc8e9..ad61cbf87394c3 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2172,8 +2172,8 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, ADDOP(c, *ploc, POP_EXCEPT); if (info->fb_datum) { ADDOP_LOAD_CONST(c, *ploc, Py_None); - compiler_nameop(c, *ploc, info->fb_datum, Store); - compiler_nameop(c, *ploc, info->fb_datum, Del); + RETURN_IF_ERROR(compiler_nameop(c, *ploc, info->fb_datum, Store)); + RETURN_IF_ERROR(compiler_nameop(c, *ploc, info->fb_datum, Del)); } return SUCCESS; } @@ -2250,9 +2250,7 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts) st = (stmt_ty)asdl_seq_GET(stmts, 0); assert(st->kind == Expr_kind); VISIT(c, expr, st->v.Expr.value); - if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store)); } } for (; i < asdl_seq_LEN(stmts); i++) { @@ -2798,7 +2796,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) Py_DECREF(co); RETURN_IF_ERROR(compiler_apply_decorators(c, decos)); - return compiler_nameop(c, loc, name, Store) ? SUCCESS : ERROR; + return compiler_nameop(c, loc, name, Store); } static int @@ -2836,18 +2834,18 @@ compiler_class(struct compiler *c, stmt_ty s) /* use the class name for name mangling */ Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name)); /* load (global) __name__ ... */ - if (!compiler_nameop(c, loc, &_Py_ID(__name__), Load)) { + if (compiler_nameop(c, loc, &_Py_ID(__name__), Load) < 0) { compiler_exit_scope(c); return ERROR; } /* ... and store it as __module__ */ - if (!compiler_nameop(c, loc, &_Py_ID(__module__), Store)) { + if (compiler_nameop(c, loc, &_Py_ID(__module__), Store) < 0) { compiler_exit_scope(c); return ERROR; } assert(c->u->u_qualname); ADDOP_LOAD_CONST(c, loc, c->u->u_qualname); - if (!compiler_nameop(c, loc, &_Py_ID(__qualname__), Store)) { + if (compiler_nameop(c, loc, &_Py_ID(__qualname__), Store) < 0) { compiler_exit_scope(c); return ERROR; } @@ -2868,7 +2866,7 @@ compiler_class(struct compiler *c, stmt_ty s) assert(i == 0); ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i); ADDOP_I(c, NO_LOCATION, COPY, 1); - if (!compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store)) { + if (compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store) < 0) { compiler_exit_scope(c); return ERROR; } @@ -2912,10 +2910,7 @@ compiler_class(struct compiler *c, stmt_ty s) RETURN_IF_ERROR(compiler_apply_decorators(c, decos)); /* 7. store into <name> */ - if (!compiler_nameop(c, loc, s->v.ClassDef.name, Store)) { - return ERROR; - } - return SUCCESS; + return compiler_nameop(c, loc, s->v.ClassDef.name, Store); } /* Return 0 if the expression is a constant value except named singletons. @@ -3612,7 +3607,8 @@ compiler_try_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup_end); NEW_JUMP_TARGET_LABEL(c, cleanup_body); - compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store); + RETURN_IF_ERROR( + compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store)); /* try: @@ -3641,8 +3637,10 @@ compiler_try_except(struct compiler *c, stmt_ty s) ADDOP(c, NO_LOCATION, POP_BLOCK); ADDOP(c, NO_LOCATION, POP_EXCEPT); ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store)); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del)); ADDOP_JUMP(c, NO_LOCATION, JUMP, end); /* except: */ @@ -3650,8 +3648,10 @@ compiler_try_except(struct compiler *c, stmt_ty s) /* name = None; del name; # artificial */ ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store)); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del)); ADDOP_I(c, NO_LOCATION, RERAISE, 1); } @@ -3806,7 +3806,8 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) NEW_JUMP_TARGET_LABEL(c, cleanup_body); if (handler->v.ExceptHandler.name) { - compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store); + RETURN_IF_ERROR( + compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store)); } else { ADDOP(c, loc, POP_TOP); // match @@ -3837,8 +3838,10 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) ADDOP(c, NO_LOCATION, POP_BLOCK); if (handler->v.ExceptHandler.name) { ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store)); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del)); } ADDOP_JUMP(c, NO_LOCATION, JUMP, except); @@ -3848,8 +3851,10 @@ compiler_try_star_except(struct compiler *c, stmt_ty s) /* name = None; del name; # artificial */ if (handler->v.ExceptHandler.name) { ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store); - compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store)); + RETURN_IF_ERROR( + compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del)); } /* add exception raised to the res list */ @@ -3950,13 +3955,11 @@ compiler_import_as(struct compiler *c, location loc, ADDOP_I(c, loc, SWAP, 2); ADDOP(c, loc, POP_TOP); } - if (!compiler_nameop(c, loc, asname, Store)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_nameop(c, loc, asname, Store)); ADDOP(c, loc, POP_TOP); return SUCCESS; } - return compiler_nameop(c, loc, asname, Store) ? SUCCESS : ERROR; + return compiler_nameop(c, loc, asname, Store); } static int @@ -4001,9 +4004,7 @@ compiler_import(struct compiler *c, stmt_ty s) if (dot != -1) { Py_DECREF(tmp); } - if (!r) { - return ERROR; - } + RETURN_IF_ERROR(r); } } return SUCCESS; @@ -4061,9 +4062,7 @@ compiler_from_import(struct compiler *c, stmt_ty s) store_name = alias->asname; } - if (!compiler_nameop(c, LOC(s), store_name, Store)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_nameop(c, LOC(s), store_name, Store)); } /* remove imported module */ ADDOP(c, LOC(s), POP_TOP); @@ -4318,12 +4317,13 @@ compiler_nameop(struct compiler *c, location loc, !_PyUnicode_EqualToASCIIString(name, "False")); if (forbidden_name(c, loc, name, ctx)) { - return 0; + return ERROR; } mangled = _Py_Mangle(c->u->u_private, name); - if (!mangled) - return 0; + if (!mangled) { + return ERROR; + } op = 0; optype = OP_NAME; @@ -4372,8 +4372,8 @@ compiler_nameop(struct compiler *c, location loc, case Store: op = STORE_FAST; break; case Del: op = DELETE_FAST; break; } - _ADDOP_N(c, loc, op, mangled, varnames); - return 1; + ADDOP_N(c, loc, op, mangled, varnames); + return SUCCESS; case OP_GLOBAL: switch (ctx) { case Load: op = LOAD_GLOBAL; break; @@ -4394,12 +4394,12 @@ compiler_nameop(struct compiler *c, location loc, arg = dict_add_o(dict, mangled); Py_DECREF(mangled); if (arg < 0) { - return 0; + return ERROR; } if (op == LOAD_GLOBAL) { arg <<= 1; } - return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, loc) == SUCCESS ? 1 : 0; + return cfg_builder_addop_i(CFG_BUILDER(c), op, arg, loc); } static int @@ -6009,7 +6009,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) break; } case Name_kind: - return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx); + return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx) == SUCCESS ? 1 : 0; /* child nodes of List and Tuple will have expr_context set */ case List_kind: return compiler_list(c, e) == SUCCESS ? 1 : 0; @@ -6065,8 +6065,7 @@ compiler_augassign(struct compiler *c, stmt_ty s) } break; case Name_kind: - if (!compiler_nameop(c, loc, e->v.Name.id, Load)) - return ERROR; + RETURN_IF_ERROR(compiler_nameop(c, loc, e->v.Name.id, Load)); break; default: PyErr_Format(PyExc_SystemError, @@ -6102,7 +6101,7 @@ compiler_augassign(struct compiler *c, stmt_ty s) } break; case Name_kind: - return compiler_nameop(c, loc, e->v.Name.id, Store) ? SUCCESS : ERROR; + return compiler_nameop(c, loc, e->v.Name.id, Store); default: Py_UNREACHABLE(); } @@ -7168,7 +7167,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) Py_ssize_t nstores = PyList_GET_SIZE(pc->stores); for (Py_ssize_t n = 0; n < nstores; n++) { PyObject *name = PyList_GET_ITEM(pc->stores, n); - if (!compiler_nameop(c, LOC(m->pattern), name, Store)) { + if (compiler_nameop(c, LOC(m->pattern), name, Store) < 0) { Py_DECREF(pc->stores); return ERROR; } From adea67e46a551e6d61093cea24611ae49a0fe65d Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 10:47:02 +0000 Subject: [PATCH 81/93] compiler_subscript and helpers return SUCCES/ERROR --- Python/compile.c | 54 ++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index ad61cbf87394c3..7a497cc06e9e33 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -4814,7 +4814,7 @@ check_subscripter(struct compiler *c, expr_ty e) PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || PyAnySet_Check(v))) { - return 1; + return SUCCESS; } /* fall through */ case Set_kind: @@ -4822,12 +4822,13 @@ check_subscripter(struct compiler *c, expr_ty e) case GeneratorExp_kind: case Lambda_kind: { location loc = LOC(e); - return compiler_warn(c, loc, "'%.200s' object is not subscriptable; " - "perhaps you missed a comma?", - infer_type(e)->tp_name); + int ret = compiler_warn(c, loc, "'%.200s' object is not subscriptable; " + "perhaps you missed a comma?", + infer_type(e)->tp_name); + return ret == 0 ? ERROR : SUCCESS; } default: - return 1; + return SUCCESS; } } @@ -4840,14 +4841,14 @@ check_index(struct compiler *c, expr_ty e, expr_ty s) if (index_type == NULL || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) || index_type == &PySlice_Type) { - return 1; + return SUCCESS; } switch (e->kind) { case Constant_kind: v = e->v.Constant.value; if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { - return 1; + return SUCCESS; } /* fall through */ case Tuple_kind: @@ -4856,14 +4857,15 @@ check_index(struct compiler *c, expr_ty e, expr_ty s) case JoinedStr_kind: case FormattedValue_kind: { location loc = LOC(e); - return compiler_warn(c, loc, "%.200s indices must be integers " - "or slices, not %.200s; " - "perhaps you missed a comma?", - infer_type(e)->tp_name, - index_type->tp_name); + int ret = compiler_warn(c, loc, "%.200s indices must be integers " + "or slices, not %.200s; " + "perhaps you missed a comma?", + infer_type(e)->tp_name, + index_type->tp_name); + return ret == 0 ? ERROR : SUCCESS; } default: - return 1; + return SUCCESS; } } @@ -5986,7 +5988,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) } break; case Subscript_kind: - return compiler_subscript(c, e); + return compiler_subscript(c, e) == SUCCESS ? 1 : 0; case Starred_kind: switch (e->v.Starred.ctx) { case Store: @@ -6302,38 +6304,32 @@ compiler_subscript(struct compiler *c, expr_ty e) int op = 0; if (ctx == Load) { - if (!check_subscripter(c, e->v.Subscript.value)) { - return 0; - } - if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { - return 0; - } + RETURN_IF_ERROR(check_subscripter(c, e->v.Subscript.value)); + RETURN_IF_ERROR(check_index(c, e->v.Subscript.value, e->v.Subscript.slice)); } - _VISIT(c, expr, e->v.Subscript.value); + VISIT(c, expr, e->v.Subscript.value); if (is_two_element_slice(e->v.Subscript.slice) && ctx != Del) { - if (compiler_slice(c, e->v.Subscript.slice) < 0) { - return 0; - } + RETURN_IF_ERROR(compiler_slice(c, e->v.Subscript.slice)); if (ctx == Load) { - _ADDOP(c, loc, BINARY_SLICE); + ADDOP(c, loc, BINARY_SLICE); } else { assert(ctx == Store); - _ADDOP(c, loc, STORE_SLICE); + ADDOP(c, loc, STORE_SLICE); } } else { - _VISIT(c, expr, e->v.Subscript.slice); + VISIT(c, expr, e->v.Subscript.slice); switch (ctx) { case Load: op = BINARY_SUBSCR; break; case Store: op = STORE_SUBSCR; break; case Del: op = DELETE_SUBSCR; break; } assert(op); - _ADDOP(c, loc, op); + ADDOP(c, loc, op); } - return 1; + return SUCCESS; } /* Returns the number of the values emitted, From 40f7d1563eb3fc985ccb95ff73896cc543a52824 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 10:57:45 +0000 Subject: [PATCH 82/93] annotations related functions return SUCCESS/ERROR --- Python/compile.c | 58 ++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 7a497cc06e9e33..29ad90187579e4 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1733,14 +1733,6 @@ cfg_builder_addop_j(cfg_builder *g, location loc, Py_DECREF(__new_const); \ } -#define _ADDOP_N(C, LOC, OP, O, TYPE) { \ - assert(!HAS_CONST(OP)); /* use _ADDOP_LOAD_CONST_NEW */ \ - if (compiler_addop_o((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) { \ - Py_DECREF((O)); \ - return 0; \ - } \ - Py_DECREF((O)); \ -} #define _ADDOP_NAME(C, LOC, OP, O, TYPE) { \ if (compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) \ @@ -2913,13 +2905,13 @@ compiler_class(struct compiler *c, stmt_ty s) return compiler_nameop(c, loc, s->v.ClassDef.name, Store); } -/* Return 0 if the expression is a constant value except named singletons. - Return 1 otherwise. */ -static int +/* Return false if the expression is a constant value except named singletons. + Return true otherwise. */ +static bool check_is_arg(expr_ty e) { if (e->kind != Constant_kind) { - return 1; + return true; } PyObject *value = e->v.Constant.value; return (value == Py_None @@ -2935,11 +2927,11 @@ static int check_compare(struct compiler *c, expr_ty e) { Py_ssize_t i, n; - int left = check_is_arg(e->v.Compare.left); + bool left = check_is_arg(e->v.Compare.left); n = asdl_seq_LEN(e->v.Compare.ops); for (i = 0; i < n; i++) { cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); - int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); + bool right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); if (op == Is || op == IsNot) { if (!right || !left) { const char *msg = (op == Is) @@ -6113,9 +6105,9 @@ compiler_augassign(struct compiler *c, stmt_ty s) static int check_ann_expr(struct compiler *c, expr_ty e) { - _VISIT(c, expr, e); - _ADDOP(c, LOC(e), POP_TOP); - return 1; + VISIT(c, expr, e); + ADDOP(c, LOC(e), POP_TOP); + return SUCCESS; } static int @@ -6124,7 +6116,7 @@ check_annotation(struct compiler *c, stmt_ty s) /* Annotations of complex targets does not produce anything under annotations future */ if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) { - return 1; + return SUCCESS; } /* Annotations are only evaluated in a module or class. */ @@ -6132,7 +6124,7 @@ check_annotation(struct compiler *c, stmt_ty s) c->u->u_scope_type == COMPILER_SCOPE_CLASS) { return check_ann_expr(c, s->v.AnnAssign.annotation); } - return 1; + return SUCCESS; } static int @@ -6141,26 +6133,24 @@ check_ann_subscr(struct compiler *c, expr_ty e) /* We check that everything in a subscript is defined at runtime. */ switch (e->kind) { case Slice_kind: - if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) { - return 0; + if (e->v.Slice.lower && check_ann_expr(c, e->v.Slice.lower) < 0) { + return ERROR; } - if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { - return 0; + if (e->v.Slice.upper && check_ann_expr(c, e->v.Slice.upper) < 0) { + return ERROR; } - if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { - return 0; + if (e->v.Slice.step && check_ann_expr(c, e->v.Slice.step) < 0) { + return ERROR; } - return 1; + return SUCCESS; case Tuple_kind: { /* extended slice */ asdl_expr_seq *elts = e->v.Tuple.elts; Py_ssize_t i, n = asdl_seq_LEN(elts); for (i = 0; i < n; i++) { - if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { - return 0; - } + RETURN_IF_ERROR(check_ann_subscr(c, asdl_seq_GET(elts, i))); } - return 1; + return SUCCESS; } default: return check_ann_expr(c, e); @@ -6207,14 +6197,14 @@ compiler_annassign(struct compiler *c, stmt_ty s) return ERROR; } if (!s->v.AnnAssign.value && - !check_ann_expr(c, targ->v.Attribute.value)) { + check_ann_expr(c, targ->v.Attribute.value) < 0) { return ERROR; } break; case Subscript_kind: if (!s->v.AnnAssign.value && - (!check_ann_expr(c, targ->v.Subscript.value) || - !check_ann_subscr(c, targ->v.Subscript.slice))) { + (check_ann_expr(c, targ->v.Subscript.value) < 0 || + check_ann_subscr(c, targ->v.Subscript.slice) < 0)) { return ERROR; } break; @@ -6225,7 +6215,7 @@ compiler_annassign(struct compiler *c, stmt_ty s) return ERROR; } /* Annotation is evaluated last. */ - if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { + if (!s->v.AnnAssign.simple && check_annotation(c, s) < 0) { return ERROR; } return SUCCESS; From 07756b4cc24ecd152d4b9082b3117e15c222e38f Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 11:10:45 +0000 Subject: [PATCH 83/93] compiler_subkwargs return SUCCESS/ERROR --- Python/compile.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 29ad90187579e4..b78f63c54d0880 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5092,35 +5092,35 @@ compiler_subkwargs(struct compiler *c, location loc, if (n > 1 && !big) { for (i = begin; i < end; i++) { kw = asdl_seq_GET(keywords, i); - _VISIT(c, expr, kw->value); + VISIT(c, expr, kw->value); } keys = PyTuple_New(n); if (keys == NULL) { - return 0; + return ERROR; } for (i = begin; i < end; i++) { key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key)); } - _ADDOP_LOAD_CONST_NEW(c, loc, keys); - _ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); - return 1; + ADDOP_LOAD_CONST_NEW(c, loc, keys); + ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); + return SUCCESS; } if (big) { - _ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0); + ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0); } for (i = begin; i < end; i++) { kw = asdl_seq_GET(keywords, i); - _ADDOP_LOAD_CONST(c, loc, kw->arg); - _VISIT(c, expr, kw->value); + ADDOP_LOAD_CONST(c, loc, kw->arg); + VISIT(c, expr, kw->value); if (big) { - _ADDOP_I(c, NO_LOCATION, MAP_ADD, 1); + ADDOP_I(c, NO_LOCATION, MAP_ADD, 1); } } if (!big) { - _ADDOP_I(c, loc, BUILD_MAP, n); + ADDOP_I(c, loc, BUILD_MAP, n); } - return 1; + return SUCCESS; } /* Used by compiler_call_helper and maybe_optimize_method_call to emit @@ -5214,9 +5214,7 @@ compiler_call_helper(struct compiler *c, location loc, if (kw->arg == NULL) { /* A keyword argument unpacking. */ if (nseen) { - if (!compiler_subkwargs(c, loc, keywords, i - nseen, i)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_subkwargs(c, loc, keywords, i - nseen, i)); if (have_dict) { ADDOP_I(c, loc, DICT_MERGE, 1); } @@ -5236,9 +5234,7 @@ compiler_call_helper(struct compiler *c, location loc, } if (nseen) { /* Pack up any trailing keyword arguments. */ - if (!compiler_subkwargs(c, loc, keywords, nkwelts - nseen, nkwelts)) { - return ERROR; - } + RETURN_IF_ERROR(compiler_subkwargs(c, loc, keywords, nkwelts - nseen, nkwelts)); if (have_dict) { ADDOP_I(c, loc, DICT_MERGE, 1); } From 45d26ef88e25c1d4cbc6e45d1f2d7a9e253f4d78 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 11:34:47 +0000 Subject: [PATCH 84/93] visitor functions return SUCCESS/ERROR --- Python/compile.c | 337 ++++++++++++++++++----------------------------- 1 file changed, 128 insertions(+), 209 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index b78f63c54d0880..e44cef17b184b1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1670,13 +1670,11 @@ cfg_builder_addop_j(cfg_builder *g, location loc, the ASDL name to synthesize the name of the C type and the visit function. */ -#define VISIT(C, TYPE, V) {\ - if (!compiler_visit_ ## TYPE((C), (V))) \ - return ERROR; \ -} +#define VISIT(C, TYPE, V) \ + RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), (V))); #define VISIT_IN_SCOPE(C, TYPE, V) {\ - if (!compiler_visit_ ## TYPE((C), (V))) { \ + if (compiler_visit_ ## TYPE((C), (V)) < 0) { \ compiler_exit_scope(c); \ return ERROR; \ } \ @@ -1687,7 +1685,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ - if (!compiler_visit_ ## TYPE((C), elt)) \ + if (compiler_visit_ ## TYPE((C), elt) < 0) \ return ERROR; \ } \ } @@ -1697,7 +1695,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ - if (!compiler_visit_ ## TYPE((C), elt)) { \ + if (compiler_visit_ ## TYPE((C), elt) < 0) { \ compiler_exit_scope(c); \ return ERROR; \ } \ @@ -1705,92 +1703,6 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } -/*******************/ - - - - -#define _ADDOP(C, LOC, OP) { \ - if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) \ - return 0; \ -} - -#define _ADDOP_LOAD_CONST(C, LOC, O) { \ - if (compiler_addop_load_const((C), (LOC), (O)) < 0) \ - return 0; \ -} - -/* Same as _ADDOP_LOAD_CONST, but steals a reference. */ -#define _ADDOP_LOAD_CONST_NEW(C, LOC, O) { \ - PyObject *__new_const = (O); \ - if (__new_const == NULL) { \ - return 0; \ - } \ - if (compiler_addop_load_const((C), (LOC), __new_const) < 0) { \ - Py_DECREF(__new_const); \ - return 0; \ - } \ - Py_DECREF(__new_const); \ -} - - -#define _ADDOP_NAME(C, LOC, OP, O, TYPE) { \ - if (compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)) < 0) \ - return 0; \ -} - -#define _ADDOP_I(C, LOC, OP, O) { \ - if (cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC)) < 0) \ - return 0; \ -} - -#define _ADDOP_JUMP(C, LOC, OP, O) { \ - if (cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O)) < 0) \ - return 0; \ -} - -#define _ADDOP_COMPARE(C, LOC, CMP) { \ - if (compiler_addcompare((C), (LOC), (cmpop_ty)(CMP)) < 0) \ - return 0; \ -} - -#define _ADDOP_BINARY(C, LOC, BINOP) { \ - if (addop_binary((C), (LOC), (BINOP), false) < 0) \ - return 0; \ - } - - -#define _ADD_YIELD_FROM(C, LOC, await) { \ - if (compiler_add_yield_from((C), (LOC), (await)) < 0) \ - return 0; \ -} - - -#define _ADDOP_YIELD(C, LOC) { \ - if (addop_yield((C), (LOC)) < 0) \ - return 0; \ - } - -/* _VISIT and _VISIT_SEQ takes an ASDL type as their second argument. They use - the ASDL name to synthesize the name of the C type and the visit function. -*/ - -#define _VISIT(C, TYPE, V) {\ - if (!compiler_visit_ ## TYPE((C), (V))) \ - return 0; \ -} - -#define _VISIT_SEQ(C, TYPE, SEQ) { \ - int _i; \ - asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ - for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ - TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ - if (!compiler_visit_ ## TYPE((C), elt)) \ - return 0; \ - } \ -} - - static int compiler_enter_scope(struct compiler *c, identifier name, int scope_type, void *key, int lineno) @@ -2453,17 +2365,15 @@ compiler_visit_kwonlydefaults(struct compiler *c, location loc, goto error; } } - if (!compiler_visit_expr(c, default_)) { - goto error; - } + RETURN_IF_ERROR(compiler_visit_expr(c, default_)); } } if (keys != NULL) { Py_ssize_t default_count = PyList_GET_SIZE(keys); PyObject *keys_tuple = PyList_AsTuple(keys); Py_DECREF(keys); - _ADDOP_LOAD_CONST_NEW(c, loc, keys_tuple); - _ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, default_count); + ADDOP_LOAD_CONST_NEW(c, loc, keys_tuple); + ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, default_count); assert(default_count > 0); return 1; } @@ -2480,8 +2390,8 @@ static int compiler_visit_annexpr(struct compiler *c, expr_ty annotation) { location loc = LOC(annotation); - _ADDOP_LOAD_CONST_NEW(c, loc, _PyAST_ExprAsUnicode(annotation)); - return 1; + ADDOP_LOAD_CONST_NEW(c, loc, _PyAST_ExprAsUnicode(annotation)); + return SUCCESS; } static int @@ -2597,10 +2507,8 @@ compiler_default_arguments(struct compiler *c, location loc, int res = compiler_visit_kwonlydefaults(c, loc, args->kwonlyargs, args->kw_defaults); - if (res < 0) { - return ERROR; - } - else if (res > 0) { + RETURN_IF_ERROR(res); + if (res > 0) { funcflags |= 0x02; } } @@ -2740,10 +2648,8 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) return ERROR; } annotations = compiler_visit_annotations(c, loc, args, returns); - if (annotations == ERROR) { - return ERROR; - } - else if (annotations > 0) { + RETURN_IF_ERROR(annotations); + if (annotations > 0) { funcflags |= 0x04; } @@ -4119,94 +4025,94 @@ compiler_visit_stmt(struct compiler *c, stmt_ty s) switch (s->kind) { case FunctionDef_kind: - return compiler_function(c, s, 0) == SUCCESS ? 1 : 0; + return compiler_function(c, s, 0); case ClassDef_kind: - return compiler_class(c, s) == SUCCESS ? 1 : 0; + return compiler_class(c, s); case Return_kind: - return compiler_return(c, s) == SUCCESS ? 1 : 0; + return compiler_return(c, s); case Delete_kind: - _VISIT_SEQ(c, expr, s->v.Delete.targets) + VISIT_SEQ(c, expr, s->v.Delete.targets) break; case Assign_kind: { Py_ssize_t n = asdl_seq_LEN(s->v.Assign.targets); - _VISIT(c, expr, s->v.Assign.value); + VISIT(c, expr, s->v.Assign.value); for (Py_ssize_t i = 0; i < n; i++) { if (i < n - 1) { - _ADDOP_I(c, LOC(s), COPY, 1); + ADDOP_I(c, LOC(s), COPY, 1); } - _VISIT(c, expr, + VISIT(c, expr, (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); } break; } case AugAssign_kind: - return compiler_augassign(c, s) == SUCCESS ? 1 : 0; + return compiler_augassign(c, s); case AnnAssign_kind: - return compiler_annassign(c, s) == SUCCESS ? 1 : 0; + return compiler_annassign(c, s); case For_kind: - return compiler_for(c, s) == SUCCESS ? 1 : 0; + return compiler_for(c, s); case While_kind: - return compiler_while(c, s) == SUCCESS ? 1 : 0; + return compiler_while(c, s); case If_kind: - return compiler_if(c, s) == SUCCESS ? 1 : 0; + return compiler_if(c, s); case Match_kind: - return compiler_match(c, s) == SUCCESS ? 1 : 0; + return compiler_match(c, s); case Raise_kind: { Py_ssize_t n = 0; if (s->v.Raise.exc) { - _VISIT(c, expr, s->v.Raise.exc); + VISIT(c, expr, s->v.Raise.exc); n++; if (s->v.Raise.cause) { - _VISIT(c, expr, s->v.Raise.cause); + VISIT(c, expr, s->v.Raise.cause); n++; } } - _ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n); + ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n); break; } case Try_kind: - return compiler_try(c, s) == SUCCESS ? 1 : 0; + return compiler_try(c, s); case TryStar_kind: - return compiler_try_star(c, s) == SUCCESS ? 1 : 0; + return compiler_try_star(c, s); case Assert_kind: - return compiler_assert(c, s) == SUCCESS ? 1 : 0; + return compiler_assert(c, s); case Import_kind: - return compiler_import(c, s) == SUCCESS ? 1 : 0; + return compiler_import(c, s); case ImportFrom_kind: - return compiler_from_import(c, s) == SUCCESS ? 1 : 0; + return compiler_from_import(c, s); case Global_kind: case Nonlocal_kind: break; case Expr_kind: { - return compiler_stmt_expr(c, LOC(s), s->v.Expr.value) == SUCCESS ? 1 : 0; + return compiler_stmt_expr(c, LOC(s), s->v.Expr.value); } case Pass_kind: { - _ADDOP(c, LOC(s), NOP); + ADDOP(c, LOC(s), NOP); break; } case Break_kind: { - return compiler_break(c, LOC(s)) == SUCCESS ? 1 : 0; + return compiler_break(c, LOC(s)); } case Continue_kind: { - return compiler_continue(c, LOC(s)) == SUCCESS ? 1 : 0; + return compiler_continue(c, LOC(s)); } case With_kind: - return compiler_with(c, s, 0) == SUCCESS ? 1 : 0; + return compiler_with(c, s, 0); case AsyncFunctionDef_kind: - return compiler_function(c, s, 1) == SUCCESS ? 1 : 0; + return compiler_function(c, s, 1); case AsyncWith_kind: - return compiler_async_with(c, s, 0) == SUCCESS ? 1 : 0; + return compiler_async_with(c, s, 0); case AsyncFor_kind: - return compiler_async_for(c, s) == SUCCESS ? 1 : 0; + return compiler_async_for(c, s); } - return 1; + return SUCCESS; } static int @@ -5647,8 +5553,8 @@ compiler_dictcomp(struct compiler *c, expr_ty e) static int compiler_visit_keyword(struct compiler *c, keyword_ty k) { - _VISIT(c, expr, k->value); - return 1; + VISIT(c, expr, k->value); + return SUCCESS; } @@ -5873,140 +5779,146 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) location loc = LOC(e); switch (e->kind) { case NamedExpr_kind: - _VISIT(c, expr, e->v.NamedExpr.value); - _ADDOP_I(c, loc, COPY, 1); - _VISIT(c, expr, e->v.NamedExpr.target); + VISIT(c, expr, e->v.NamedExpr.value); + ADDOP_I(c, loc, COPY, 1); + VISIT(c, expr, e->v.NamedExpr.target); break; case BoolOp_kind: - return compiler_boolop(c, e) == SUCCESS ? 1 : 0; + return compiler_boolop(c, e); case BinOp_kind: - _VISIT(c, expr, e->v.BinOp.left); - _VISIT(c, expr, e->v.BinOp.right); - _ADDOP_BINARY(c, loc, e->v.BinOp.op); + VISIT(c, expr, e->v.BinOp.left); + VISIT(c, expr, e->v.BinOp.right); + ADDOP_BINARY(c, loc, e->v.BinOp.op); break; case UnaryOp_kind: - _VISIT(c, expr, e->v.UnaryOp.operand); - _ADDOP(c, loc, unaryop(e->v.UnaryOp.op)); + VISIT(c, expr, e->v.UnaryOp.operand); + ADDOP(c, loc, unaryop(e->v.UnaryOp.op)); break; case Lambda_kind: - return compiler_lambda(c, e) == SUCCESS ? 1 : 0; + return compiler_lambda(c, e); case IfExp_kind: - return compiler_ifexp(c, e) == SUCCESS ? 1 : 0; + return compiler_ifexp(c, e); case Dict_kind: - return compiler_dict(c, e) == SUCCESS ? 1 : 0; + return compiler_dict(c, e); case Set_kind: - return compiler_set(c, e) == SUCCESS ? 1 : 0; + return compiler_set(c, e); case GeneratorExp_kind: - return compiler_genexp(c, e) == SUCCESS ? 1 : 0; + return compiler_genexp(c, e); case ListComp_kind: - return compiler_listcomp(c, e) == SUCCESS ? 1 : 0; + return compiler_listcomp(c, e); case SetComp_kind: - return compiler_setcomp(c, e) == SUCCESS ? 1 : 0; + return compiler_setcomp(c, e); case DictComp_kind: - return compiler_dictcomp(c, e) == SUCCESS ? 1 : 0; + return compiler_dictcomp(c, e); case Yield_kind: - if (c->u->u_ste->ste_type != FunctionBlock) - return compiler_error(c, loc, "'yield' outside function"); + if (c->u->u_ste->ste_type != FunctionBlock) { + compiler_error(c, loc, "'yield' outside function"); + return ERROR; + } if (e->v.Yield.value) { - _VISIT(c, expr, e->v.Yield.value); + VISIT(c, expr, e->v.Yield.value); } else { - _ADDOP_LOAD_CONST(c, loc, Py_None); + ADDOP_LOAD_CONST(c, loc, Py_None); } - _ADDOP_YIELD(c, loc); + ADDOP_YIELD(c, loc); break; case YieldFrom_kind: - if (c->u->u_ste->ste_type != FunctionBlock) - return compiler_error(c, loc, "'yield' outside function"); - - if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) - return compiler_error(c, loc, "'yield from' inside async function"); - - _VISIT(c, expr, e->v.YieldFrom.value); - _ADDOP(c, loc, GET_YIELD_FROM_ITER); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 0); + if (c->u->u_ste->ste_type != FunctionBlock) { + compiler_error(c, loc, "'yield' outside function"); + return ERROR; + } + if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) { + compiler_error(c, loc, "'yield from' inside async function"); + return ERROR; + } + VISIT(c, expr, e->v.YieldFrom.value); + ADDOP(c, loc, GET_YIELD_FROM_ITER); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 0); break; case Await_kind: if (!IS_TOP_LEVEL_AWAIT(c)){ if (c->u->u_ste->ste_type != FunctionBlock){ - return compiler_error(c, loc, "'await' outside function"); + compiler_error(c, loc, "'await' outside function"); + return ERROR; } if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && - c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ - return compiler_error(c, loc, "'await' outside async function"); + c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) { + compiler_error(c, loc, "'await' outside async function"); + return ERROR; } } - _VISIT(c, expr, e->v.Await.value); - _ADDOP_I(c, loc, GET_AWAITABLE, 0); - _ADDOP_LOAD_CONST(c, loc, Py_None); - _ADD_YIELD_FROM(c, loc, 1); + VISIT(c, expr, e->v.Await.value); + ADDOP_I(c, loc, GET_AWAITABLE, 0); + ADDOP_LOAD_CONST(c, loc, Py_None); + ADD_YIELD_FROM(c, loc, 1); break; case Compare_kind: - return compiler_compare(c, e) == SUCCESS ? 1 : 0; + return compiler_compare(c, e); case Call_kind: - return compiler_call(c, e) == SUCCESS ? 1 : 0; + return compiler_call(c, e); case Constant_kind: - _ADDOP_LOAD_CONST(c, loc, e->v.Constant.value); + ADDOP_LOAD_CONST(c, loc, e->v.Constant.value); break; case JoinedStr_kind: - return compiler_joined_str(c, e) == SUCCESS ? 1 : 0; + return compiler_joined_str(c, e); case FormattedValue_kind: - return compiler_formatted_value(c, e) == SUCCESS ? 1 : 0; + return compiler_formatted_value(c, e); /* The following exprs can be assignment targets. */ case Attribute_kind: - _VISIT(c, expr, e->v.Attribute.value); + VISIT(c, expr, e->v.Attribute.value); loc = LOC(e); loc = update_start_location_to_match_attr(c, loc, e); switch (e->v.Attribute.ctx) { case Load: - _ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); + ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names); break; case Store: if (forbidden_name(c, loc, e->v.Attribute.attr, e->v.Attribute.ctx)) { - return 0; + return ERROR; } - _ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); + ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names); break; case Del: - _ADDOP_NAME(c, loc, DELETE_ATTR, e->v.Attribute.attr, names); + ADDOP_NAME(c, loc, DELETE_ATTR, e->v.Attribute.attr, names); break; } break; case Subscript_kind: - return compiler_subscript(c, e) == SUCCESS ? 1 : 0; + return compiler_subscript(c, e); case Starred_kind: switch (e->v.Starred.ctx) { case Store: /* In all legitimate cases, the Starred node was already replaced * by compiler_list/compiler_tuple. XXX: is that okay? */ - return compiler_error(c, loc, + compiler_error(c, loc, "starred assignment target must be in a list or tuple"); + return ERROR; default: - return compiler_error(c, loc, + compiler_error(c, loc, "can't use starred expression here"); + return ERROR; } break; case Slice_kind: { int n = compiler_slice(c, e); - if (n < 0) { - return 0; - } - _ADDOP_I(c, loc, BUILD_SLICE, n); + RETURN_IF_ERROR(n); + ADDOP_I(c, loc, BUILD_SLICE, n); break; } case Name_kind: - return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx) == SUCCESS ? 1 : 0; + return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx); /* child nodes of List and Tuple will have expr_context set */ case List_kind: - return compiler_list(c, e) == SUCCESS ? 1 : 0; + return compiler_list(c, e); case Tuple_kind: - return compiler_tuple(c, e) == SUCCESS ? 1 : 0; + return compiler_tuple(c, e); } - return 1; + return SUCCESS; } static int @@ -6791,9 +6703,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, compiler_error(c, LOC(p), e); goto error; } - if (!compiler_visit_expr(c, key)) { - goto error; - } + RETURN_IF_ERROR(compiler_visit_expr(c, key)); } // all keys have been checked; there are no duplicates @@ -8865,6 +8775,19 @@ prepare_localsplus(struct compiler* c, int code_flags) return nlocalsplus; } +static int +add_return_at_end_of_block(struct compiler *c, int addNone) +{ + /* Make sure every block that falls off the end returns None. */ + if (!basicblock_returns(CFG_BUILDER(c)->g_curblock)) { + if (addNone) { + ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); + } + ADDOP(c, NO_LOCATION, RETURN_VALUE); + } + return SUCCESS; +} + static PyCodeObject * assemble(struct compiler *c, int addNone) { @@ -8878,12 +8801,8 @@ assemble(struct compiler *c, int addNone) return NULL; } - /* Make sure every block that falls off the end returns None. */ - if (!basicblock_returns(CFG_BUILDER(c)->g_curblock)) { - if (addNone) { - _ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None); - } - _ADDOP(c, NO_LOCATION, RETURN_VALUE); + if (add_return_at_end_of_block(c, addNone) < 0) { + return NULL; } int nblocks = 0; From d5b624b035d597d48a7cee4b9b4dd94d35b4e30c Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 11:46:14 +0000 Subject: [PATCH 85/93] compiler_error returns ERROR --- Python/compile.c | 108 ++++++++++++++++------------------------------- 1 file changed, 37 insertions(+), 71 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index e44cef17b184b1..8cfac779472b72 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1913,8 +1913,7 @@ compiler_push_fblock(struct compiler *c, location loc, { struct fblockinfo *f; if (c->u->u_nfblocks >= CO_MAXBLOCKS) { - compiler_error(c, loc, "too many statically nested blocks"); - return ERROR; + return compiler_error(c, loc, "too many statically nested blocks"); } f = &c->u->u_fblock[c->u->u_nfblocks++]; f->fb_type = t; @@ -2102,9 +2101,8 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc, } struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; if (top->fb_type == EXCEPTION_GROUP_HANDLER) { - compiler_error(c, *ploc, + return compiler_error(c, *ploc, "'break', 'continue' and 'return' cannot appear in an except* block"); - return ERROR; } if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { *loop = top; @@ -3133,8 +3131,7 @@ compiler_async_for(struct compiler *c, stmt_ty s) if (IS_TOP_LEVEL_AWAIT(c)){ c->u->u_ste->ste_coroutine = 1; } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { - compiler_error(c, loc, "'async for' outside async function"); - return ERROR; + return compiler_error(c, loc, "'async for' outside async function"); } NEW_JUMP_TARGET_LABEL(c, start); @@ -3212,14 +3209,12 @@ compiler_return(struct compiler *c, stmt_ty s) int preserve_tos = ((s->v.Return.value != NULL) && (s->v.Return.value->kind != Constant_kind)); if (c->u->u_ste->ste_type != FunctionBlock) { - compiler_error(c, loc, "'return' outside function"); - return ERROR; + return compiler_error(c, loc, "'return' outside function"); } if (s->v.Return.value != NULL && c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) { - compiler_error(c, loc, "'return' with value in async generator"); - return ERROR; + return compiler_error(c, loc, "'return' with value in async generator"); } if (preserve_tos) { @@ -3256,8 +3251,7 @@ compiler_break(struct compiler *c, location loc) ADDOP(c, loc, NOP); RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop)); if (loop == NULL) { - compiler_error(c, loc, "'break' outside loop"); - return ERROR; + return compiler_error(c, loc, "'break' outside loop"); } RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0)); ADDOP_JUMP(c, loc, JUMP, loop->fb_exit); @@ -3272,8 +3266,7 @@ compiler_continue(struct compiler *c, location loc) ADDOP(c, loc, NOP); RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop)); if (loop == NULL) { - compiler_error(c, loc, "'continue' not properly in loop"); - return ERROR; + return compiler_error(c, loc, "'continue' not properly in loop"); } ADDOP_JUMP(c, loc, JUMP, loop->fb_block); return SUCCESS; @@ -3491,8 +3484,7 @@ compiler_try_except(struct compiler *c, stmt_ty s) s->v.Try.handlers, i); location loc = LOC(handler); if (!handler->v.ExceptHandler.type && i < n-1) { - compiler_error(c, loc, "default 'except:' must be last"); - return ERROR; + return compiler_error(c, loc, "default 'except:' must be last"); } NEW_JUMP_TARGET_LABEL(c, next_except); except = next_except; @@ -3931,9 +3923,8 @@ compiler_from_import(struct compiler *c, stmt_ty s) _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { Py_DECREF(names); - compiler_error(c, LOC(s), "from __future__ imports must occur " - "at the beginning of the file"); - return ERROR; + return compiler_error(c, LOC(s), "from __future__ imports must occur " + "at the beginning of the file"); } ADDOP_LOAD_CONST_NEW(c, LOC(s), names); @@ -4425,18 +4416,16 @@ unpack_helper(struct compiler *c, location loc, asdl_expr_seq *elts) if (elt->kind == Starred_kind && !seen_star) { if ((i >= (1 << 8)) || (n-i-1 >= (INT_MAX >> 8))) { - compiler_error(c, loc, + return compiler_error(c, loc, "too many expressions in " "star-unpacking assignment"); - return ERROR; } ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); seen_star = 1; } else if (elt->kind == Starred_kind) { - compiler_error(c, loc, + return compiler_error(c, loc, "multiple starred expressions in assignment"); - return ERROR; } } if (!seen_star) { @@ -4884,8 +4873,7 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) for (Py_ssize_t j = i + 1; j < nkeywords; j++) { keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { - compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg); - return ERROR; + return compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg); } } } @@ -5614,8 +5602,7 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) if (IS_TOP_LEVEL_AWAIT(c)){ c->u->u_ste->ste_coroutine = 1; } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ - compiler_error(c, loc, "'async with' outside async function"); - return ERROR; + return compiler_error(c, loc, "'async with' outside async function"); } NEW_JUMP_TARGET_LABEL(c, block); @@ -5812,8 +5799,7 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) return compiler_dictcomp(c, e); case Yield_kind: if (c->u->u_ste->ste_type != FunctionBlock) { - compiler_error(c, loc, "'yield' outside function"); - return ERROR; + return compiler_error(c, loc, "'yield' outside function"); } if (e->v.Yield.value) { VISIT(c, expr, e->v.Yield.value); @@ -5825,12 +5811,10 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) break; case YieldFrom_kind: if (c->u->u_ste->ste_type != FunctionBlock) { - compiler_error(c, loc, "'yield' outside function"); - return ERROR; + return compiler_error(c, loc, "'yield' outside function"); } if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) { - compiler_error(c, loc, "'yield from' inside async function"); - return ERROR; + return compiler_error(c, loc, "'yield from' inside async function"); } VISIT(c, expr, e->v.YieldFrom.value); ADDOP(c, loc, GET_YIELD_FROM_ITER); @@ -5840,14 +5824,12 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Await_kind: if (!IS_TOP_LEVEL_AWAIT(c)){ if (c->u->u_ste->ste_type != FunctionBlock){ - compiler_error(c, loc, "'await' outside function"); - return ERROR; + return compiler_error(c, loc, "'await' outside function"); } if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) { - compiler_error(c, loc, "'await' outside async function"); - return ERROR; + return compiler_error(c, loc, "'await' outside async function"); } } @@ -5894,13 +5876,11 @@ compiler_visit_expr1(struct compiler *c, expr_ty e) case Store: /* In all legitimate cases, the Starred node was already replaced * by compiler_list/compiler_tuple. XXX: is that okay? */ - compiler_error(c, loc, + return compiler_error(c, loc, "starred assignment target must be in a list or tuple"); - return ERROR; default: - compiler_error(c, loc, + return compiler_error(c, loc, "can't use starred expression here"); - return ERROR; } break; case Slice_kind: @@ -6142,7 +6122,7 @@ compiler_error(struct compiler *c, location loc, PyObject *msg = PyUnicode_FromFormatV(format, vargs); va_end(vargs); if (msg == NULL) { - return 0; + return ERROR; } PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno); if (loc_obj == NULL) { @@ -6159,7 +6139,7 @@ compiler_error(struct compiler *c, location loc, exit: Py_DECREF(loc_obj); Py_XDECREF(args); - return 0; + return ERROR; } /* Emits a SyntaxWarning and returns 1 on success. @@ -6345,9 +6325,8 @@ emit_and_reset_fail_pop(struct compiler *c, location loc, static int compiler_error_duplicate_store(struct compiler *c, location loc, identifier n) { - compiler_error(c, loc, + return compiler_error(c, loc, "multiple assignments to name %R in pattern", n); - return ERROR; } // Duplicate the effect of 3.10's ROT_* instructions using SWAPs. @@ -6377,8 +6356,7 @@ pattern_helper_store_name(struct compiler *c, location loc, return ERROR; } if (duplicate) { - compiler_error_duplicate_store(c, loc, n); - return ERROR; + return compiler_error_duplicate_store(c, loc, n); } // Rotate this object underneath any items we need to preserve: Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1; @@ -6403,18 +6381,16 @@ pattern_unpack_helper(struct compiler *c, location loc, if (elt->kind == MatchStar_kind && !seen_star) { if ((i >= (1 << 8)) || (n-i-1 >= (INT_MAX >> 8))) { - compiler_error(c, loc, + return compiler_error(c, loc, "too many expressions in " "star-unpacking sequence pattern"); - return ERROR; } ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8))); seen_star = 1; } else if (elt->kind == MatchStar_kind) { - compiler_error(c, loc, + return compiler_error(c, loc, "multiple starred expressions in sequence pattern"); - return ERROR; } } if (!seen_star) { @@ -6505,12 +6481,10 @@ compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc) if (!pc->allow_irrefutable) { if (p->v.MatchAs.name) { const char *e = "name capture %R makes remaining patterns unreachable"; - compiler_error(c, LOC(p), e, p->v.MatchAs.name); - return ERROR; + return compiler_error(c, LOC(p), e, p->v.MatchAs.name); } const char *e = "wildcard makes remaining patterns unreachable"; - compiler_error(c, LOC(p), e); - return ERROR; + return compiler_error(c, LOC(p), e); } return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc); } @@ -6549,8 +6523,7 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_ identifier other = ((identifier)asdl_seq_GET(attrs, j)); if (!PyUnicode_Compare(attr, other)) { location loc = LOC((pattern_ty) asdl_seq_GET(patterns, j)); - compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr); - return ERROR; + return compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr); } } } @@ -6571,13 +6544,11 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern"; - compiler_error(c, LOC(p), e, nattrs, nkwd_patterns); - return ERROR; + return compiler_error(c, LOC(p), e, nattrs, nkwd_patterns); } if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) { const char *e = "too many sub-patterns in class pattern %R"; - compiler_error(c, LOC(p), e, p->v.MatchClass.cls); - return ERROR; + return compiler_error(c, LOC(p), e, p->v.MatchClass.cls); } if (nattrs) { RETURN_IF_ERROR(validate_kwd_attrs(c, kwd_attrs, kwd_patterns)); @@ -6636,8 +6607,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern"; - compiler_error(c, LOC(p), e, size, npatterns); - return ERROR; + return compiler_error(c, LOC(p), e, size, npatterns); } // We have a double-star target if "rest" is set PyObject *star_target = p->v.MatchMapping.rest; @@ -6659,8 +6629,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE)); } if (INT_MAX < size - 1) { - compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern"); - return ERROR; + return compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern"); } // Collect all of the keys into a tuple for MATCH_KEYS and // **rest. They can either be dotted names or literals: @@ -6926,8 +6895,7 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, if (pattern->kind == MatchStar_kind) { if (star >= 0) { const char *e = "multiple starred names in sequence pattern"; - compiler_error(c, LOC(p), e); - return ERROR; + return compiler_error(c, LOC(p), e); } star_wildcard = WILDCARD_STAR_CHECK(pattern); only_wildcard &= star_wildcard; @@ -6978,8 +6946,7 @@ compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc) expr_ty value = p->v.MatchValue.value; if (!MATCH_VALUE_EXPR(value)) { const char *e = "patterns may only match literals and attribute lookups"; - compiler_error(c, LOC(p), e); - return ERROR; + return compiler_error(c, LOC(p), e); } VISIT(c, expr, value); ADDOP_COMPARE(c, LOC(p), Eq); @@ -7021,8 +6988,7 @@ compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc) // AST validator shouldn't let this happen, but if it does, // just fail, don't crash out of the interpreter const char *e = "invalid match pattern node in AST (kind=%d)"; - compiler_error(c, LOC(p), e, p->kind); - return ERROR; + return compiler_error(c, LOC(p), e, p->kind); } static int From ef1001c14921f5a60cf2702ebd35805e61fe5239 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 11:49:27 +0000 Subject: [PATCH 86/93] compiler_warn returns SUCCESS/ERROR --- Python/compile.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 8cfac779472b72..f327ab134e6db1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2841,8 +2841,7 @@ check_compare(struct compiler *c, expr_ty e) const char *msg = (op == Is) ? "\"is\" with a literal. Did you mean \"==\"?" : "\"is not\" with a literal. Did you mean \"!=\"?"; - int ret = compiler_warn(c, LOC(e), msg); - return ret == 0 ? ERROR : SUCCESS; + return compiler_warn(c, LOC(e), msg); } } left = right; @@ -3968,11 +3967,9 @@ compiler_assert(struct compiler *c, stmt_ty s) PyTuple_Check(s->v.Assert.test->v.Constant.value) && PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) { - if (!compiler_warn(c, LOC(s), "assertion is always true, " - "perhaps remove parentheses?")) - { - return ERROR; - } + RETURN_IF_ERROR( + compiler_warn(c, LOC(s), "assertion is always true, " + "perhaps remove parentheses?")); } if (c->c_optimize) { return SUCCESS; @@ -4679,10 +4676,9 @@ check_caller(struct compiler *c, expr_ty e) case JoinedStr_kind: case FormattedValue_kind: { location loc = LOC(e); - int ret = compiler_warn(c, loc, "'%.200s' object is not callable; " - "perhaps you missed a comma?", - infer_type(e)->tp_name); - return ret == 0 ? ERROR : SUCCESS; + return compiler_warn(c, loc, "'%.200s' object is not callable; " + "perhaps you missed a comma?", + infer_type(e)->tp_name); } default: return SUCCESS; @@ -4709,10 +4705,9 @@ check_subscripter(struct compiler *c, expr_ty e) case GeneratorExp_kind: case Lambda_kind: { location loc = LOC(e); - int ret = compiler_warn(c, loc, "'%.200s' object is not subscriptable; " - "perhaps you missed a comma?", - infer_type(e)->tp_name); - return ret == 0 ? ERROR : SUCCESS; + return compiler_warn(c, loc, "'%.200s' object is not subscriptable; " + "perhaps you missed a comma?", + infer_type(e)->tp_name); } default: return SUCCESS; @@ -4744,12 +4739,11 @@ check_index(struct compiler *c, expr_ty e, expr_ty s) case JoinedStr_kind: case FormattedValue_kind: { location loc = LOC(e); - int ret = compiler_warn(c, loc, "%.200s indices must be integers " - "or slices, not %.200s; " - "perhaps you missed a comma?", - infer_type(e)->tp_name, - index_type->tp_name); - return ret == 0 ? ERROR : SUCCESS; + return compiler_warn(c, loc, "%.200s indices must be integers " + "or slices, not %.200s; " + "perhaps you missed a comma?", + infer_type(e)->tp_name, + index_type->tp_name); } default: return SUCCESS; @@ -6155,7 +6149,7 @@ compiler_warn(struct compiler *c, location loc, PyObject *msg = PyUnicode_FromFormatV(format, vargs); va_end(vargs); if (msg == NULL) { - return 0; + return ERROR; } if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, loc.lineno, NULL, NULL) < 0) @@ -6168,10 +6162,10 @@ compiler_warn(struct compiler *c, location loc, compiler_error(c, loc, PyUnicode_AsUTF8(msg)); } Py_DECREF(msg); - return 0; + return ERROR; } Py_DECREF(msg); - return 1; + return SUCCESS; } static int From e1a54146866041ca8d9686a6ddacbff0d127964a Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 13:28:12 +0000 Subject: [PATCH 87/93] use RETURN_IF_ERROR in a few places --- Python/compile.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index f327ab134e6db1..9e3c639be5c76b 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -5308,10 +5308,9 @@ compiler_async_comprehension_generator(struct compiler *c, location loc, USE_LABEL(c, start); /* Runtime will push a block here, so we need to account for that */ - if (compiler_push_fblock(c, loc, ASYNC_COMPREHENSION_GENERATOR, - start, NO_LABEL, NULL) < 0) { - return ERROR; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, loc, ASYNC_COMPREHENSION_GENERATOR, + start, NO_LABEL, NULL)); ADDOP_JUMP(c, loc, SETUP_FINALLY, except); ADDOP(c, loc, GET_ANEXT); @@ -5631,8 +5630,8 @@ compiler_async_with(struct compiler *c, stmt_ty s, int pos) /* BLOCK code */ VISIT_SEQ(c, stmt, s->v.AsyncWith.body) } - else if (compiler_async_with(c, s, pos) == ERROR) { - return ERROR; + else { + RETURN_IF_ERROR(compiler_async_with(c, s, pos)); } compiler_pop_fblock(c, ASYNC_WITH, block); @@ -5725,8 +5724,8 @@ compiler_with(struct compiler *c, stmt_ty s, int pos) /* BLOCK code */ VISIT_SEQ(c, stmt, s->v.With.body) } - else if (compiler_with(c, s, pos) == ERROR) { - return ERROR; + else { + RETURN_IF_ERROR(compiler_with(c, s, pos)); } ADDOP(c, NO_LOCATION, POP_BLOCK); @@ -6346,20 +6345,14 @@ pattern_helper_store_name(struct compiler *c, location loc, } // Can't assign to the same name twice: int duplicate = PySequence_Contains(pc->stores, n); - if (duplicate < 0) { - return ERROR; - } + RETURN_IF_ERROR(duplicate); if (duplicate) { return compiler_error_duplicate_store(c, loc, n); } // Rotate this object underneath any items we need to preserve: Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1; - if (pattern_helper_rotate(c, loc, rotations) < 0) { - return ERROR; - } - if (PyList_Append(pc->stores, n) < 0) { - return ERROR; - } + RETURN_IF_ERROR(pattern_helper_rotate(c, loc, rotations)); + RETURN_IF_ERROR(PyList_Append(pc->stores, n)); return SUCCESS; } @@ -6398,9 +6391,7 @@ pattern_helper_sequence_unpack(struct compiler *c, location loc, asdl_pattern_seq *patterns, Py_ssize_t star, pattern_context *pc) { - if (pattern_unpack_helper(c, loc, patterns) < 0) { - return ERROR; - } + RETURN_IF_ERROR(pattern_unpack_helper(c, loc, patterns)); Py_ssize_t size = asdl_seq_LEN(patterns); // We've now got a bunch of new subjects on the stack. They need to remain // there after each subpattern match: From e2b63688d0f1190b825e8ddb282624488a7024ff Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 13:50:51 +0000 Subject: [PATCH 88/93] minor tweaks --- Python/compile.c | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 9e3c639be5c76b..146c3d52cf4a63 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1318,10 +1318,6 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg) return stack_effect(opcode, oparg, -1); } -/* Add an opcode with no argument. - Returns 0 on failure, 1 on success. -*/ - static int basicblock_addop(basicblock *b, int opcode, int oparg, location loc) { @@ -1605,7 +1601,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP(C, LOC, OP) \ - RETURN_IF_ERROR(cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC))); + RETURN_IF_ERROR(cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC))) #define ADDOP_IN_SCOPE(C, LOC, OP) { \ if (cfg_builder_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) { \ @@ -1615,7 +1611,7 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_LOAD_CONST(C, LOC, O) \ - RETURN_IF_ERROR(compiler_addop_load_const((C), (LOC), (O))); + RETURN_IF_ERROR(compiler_addop_load_const((C), (LOC), (O))) /* Same as ADDOP_LOAD_CONST, but steals a reference. */ #define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \ @@ -1640,31 +1636,31 @@ cfg_builder_addop_j(cfg_builder *g, location loc, } #define ADDOP_NAME(C, LOC, OP, O, TYPE) \ - RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O))); + RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O))) #define ADDOP_I(C, LOC, OP, O) \ - RETURN_IF_ERROR(cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC))); + RETURN_IF_ERROR(cfg_builder_addop_i(CFG_BUILDER(C), (OP), (O), (LOC))) #define ADDOP_JUMP(C, LOC, OP, O) \ - RETURN_IF_ERROR(cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O))); + RETURN_IF_ERROR(cfg_builder_addop_j(CFG_BUILDER(C), (LOC), (OP), (O))) #define ADDOP_COMPARE(C, LOC, CMP) \ - RETURN_IF_ERROR(compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))); + RETURN_IF_ERROR(compiler_addcompare((C), (LOC), (cmpop_ty)(CMP))) #define ADDOP_BINARY(C, LOC, BINOP) \ - RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), false)); + RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), false)) #define ADDOP_INPLACE(C, LOC, BINOP) \ - RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), true)); + RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), true)) -#define ADD_YIELD_FROM(C, LOC, await) \ - RETURN_IF_ERROR(compiler_add_yield_from((C), (LOC), (await))); +#define ADD_YIELD_FROM(C, LOC, await) \ + RETURN_IF_ERROR(compiler_add_yield_from((C), (LOC), (await))) -#define POP_EXCEPT_AND_RERAISE(C, LOC) \ - RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC))); +#define POP_EXCEPT_AND_RERAISE(C, LOC) \ + RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC))) #define ADDOP_YIELD(C, LOC) \ - RETURN_IF_ERROR(addop_yield((C), (LOC))); + RETURN_IF_ERROR(addop_yield((C), (LOC))) /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use the ASDL name to synthesize the name of the C type and the visit function. @@ -1784,17 +1780,13 @@ compiler_enter_scope(struct compiler *c, identifier name, c->c_nestlevel++; cfg_builder *g = CFG_BUILDER(c); - if (cfg_builder_init(g) < 0) { - return ERROR; - } + RETURN_IF_ERROR(cfg_builder_init(g)); if (u->u_scope_type == COMPILER_SCOPE_MODULE) { loc.lineno = 0; } else { - if (compiler_set_qualname(c) < 0){ - return ERROR; - } + RETURN_IF_ERROR(compiler_set_qualname(c)); } ADDOP_I(c, loc, RESUME, 0); @@ -2110,9 +2102,7 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc, } struct fblockinfo copy = *top; c->u->u_nfblocks--; - if (compiler_unwind_fblock(c, ploc, ©, preserve_tos) < 0) { - return ERROR; - } + RETURN_IF_ERROR(compiler_unwind_fblock(c, ploc, ©, preserve_tos)); RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop)); c->u->u_fblock[c->u->u_nfblocks] = copy; c->u->u_nfblocks++; @@ -6204,7 +6194,7 @@ compiler_subscript(struct compiler *c, expr_ty e) } /* Returns the number of the values emitted, - * thus are needed to build the slice, or 0 if there is an error. */ + * thus are needed to build the slice, or -1 if there is an error. */ static int compiler_slice(struct compiler *c, expr_ty s) { From dad47998a1836382a2acfdf1835222bf939a3798 Mon Sep 17 00:00:00 2001 From: Irit Katriel <iritkatriel@yahoo.com> Date: Mon, 5 Dec 2022 14:04:46 +0000 Subject: [PATCH 89/93] reduce diff, revert error --- Python/compile.c | 89 ++++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/Python/compile.c b/Python/compile.c index 146c3d52cf4a63..64d62794b9d9f1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -2007,9 +2007,8 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, /* This POP_BLOCK gets the line number of the unwinding statement */ ADDOP(c, *ploc, POP_BLOCK); if (preserve_tos) { - if (compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL) < 0) { - return ERROR; - } + RETURN_IF_ERROR( + compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL)); } /* Emit the finally block */ VISIT_SEQ(c, stmt, info->fb_datum); @@ -2041,9 +2040,7 @@ compiler_unwind_fblock(struct compiler *c, location *ploc, if (preserve_tos) { ADDOP_I(c, *ploc, SWAP, 2); } - if (compiler_call_exit_with_nones(c, *ploc) < 0) { - return ERROR; - } + RETURN_IF_ERROR(compiler_call_exit_with_nones(c, *ploc)); if (info->fb_type == ASYNC_WITH) { ADDOP_I(c, *ploc, GET_AWAITABLE, 2); ADDOP_LOAD_CONST(c, *ploc, Py_None); @@ -2093,8 +2090,8 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc, } struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; if (top->fb_type == EXCEPTION_GROUP_HANDLER) { - return compiler_error(c, *ploc, - "'break', 'continue' and 'return' cannot appear in an except* block"); + return compiler_error( + c, *ploc, "'break', 'continue' and 'return' cannot appear in an except* block"); } if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { *loop = top; @@ -2353,7 +2350,9 @@ compiler_visit_kwonlydefaults(struct compiler *c, location loc, goto error; } } - RETURN_IF_ERROR(compiler_visit_expr(c, default_)); + if (compiler_visit_expr(c, default_) < 0) { + goto error; + } } } if (keys != NULL) { @@ -2445,26 +2444,30 @@ compiler_visit_annotations(struct compiler *c, location loc, */ Py_ssize_t annotations_len = 0; - if (compiler_visit_argannotations(c, args->args, &annotations_len, loc) < 0) - return ERROR; - if (compiler_visit_argannotations(c, args->posonlyargs, &annotations_len, loc) < 0) - return ERROR; - if (args->vararg && args->vararg->annotation && - compiler_visit_argannotation(c, args->vararg->arg, - args->vararg->annotation, &annotations_len, loc) < 0) - return ERROR; - if (compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len, loc) < 0) - return ERROR; - if (args->kwarg && args->kwarg->annotation && - compiler_visit_argannotation(c, args->kwarg->arg, - args->kwarg->annotation, &annotations_len, loc) < 0) - return ERROR; + RETURN_IF_ERROR( + compiler_visit_argannotations(c, args->args, &annotations_len, loc)); - if (compiler_visit_argannotation(c, &_Py_ID(return), returns, - &annotations_len, loc) < 0) { - return ERROR; + RETURN_IF_ERROR( + compiler_visit_argannotations(c, args->posonlyargs, &annotations_len, loc)); + + if (args->vararg && args->vararg->annotation) { + RETURN_IF_ERROR( + compiler_visit_argannotation(c, args->vararg->arg, + args->vararg->annotation, &annotations_len, loc)); + } + + RETURN_IF_ERROR( + compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len, loc)); + + if (args->kwarg && args->kwarg->annotation) { + RETURN_IF_ERROR( + compiler_visit_argannotation(c, args->kwarg->arg, + args->kwarg->annotation, &annotations_len, loc)); } + RETURN_IF_ERROR( + compiler_visit_argannotation(c, &_Py_ID(return), returns, &annotations_len, loc)); + if (annotations_len) { ADDOP_I(c, loc, BUILD_TUPLE, annotations_len); return 1; @@ -2796,7 +2799,8 @@ compiler_class(struct compiler *c, stmt_ty s) RETURN_IF_ERROR(compiler_apply_decorators(c, decos)); /* 7. store into <name> */ - return compiler_nameop(c, loc, s->v.ClassDef.name, Store); + RETURN_IF_ERROR(compiler_nameop(c, loc, s->v.ClassDef.name, Store)); + return SUCCESS; } /* Return false if the expression is a constant value except named singletons. @@ -3865,9 +3869,7 @@ compiler_import(struct compiler *c, stmt_ty s) if (alias->asname) { r = compiler_import_as(c, loc, alias->name, alias->asname); - if (r == ERROR) { - return r; - } + RETURN_IF_ERROR(r); } else { identifier tmp = alias->name; @@ -4857,7 +4859,8 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) for (Py_ssize_t j = i + 1; j < nkeywords; j++) { keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { - return compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg); + compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg); + return ERROR; } } } @@ -6498,7 +6501,8 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_ identifier other = ((identifier)asdl_seq_GET(attrs, j)); if (!PyUnicode_Compare(attr, other)) { location loc = LOC((pattern_ty) asdl_seq_GET(patterns, j)); - return compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr); + compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr); + return ERROR; } } } @@ -6530,7 +6534,7 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc) } VISIT(c, expr, p->v.MatchClass.cls); PyObject *attr_names = PyTuple_New(nattrs); - if (!attr_names) { + if (attr_names == NULL) { return ERROR; } Py_ssize_t i; @@ -6647,7 +6651,9 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, compiler_error(c, LOC(p), e); goto error; } - RETURN_IF_ERROR(compiler_visit_expr(c, key)); + if (compiler_visit_expr(c, key) < 0) { + goto error; + } } // all keys have been checked; there are no duplicates @@ -6904,12 +6910,10 @@ compiler_pattern_sequence(struct compiler *c, pattern_ty p, ADDOP(c, LOC(p), POP_TOP); } else if (star_wildcard) { - RETURN_IF_ERROR( - pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc)); + RETURN_IF_ERROR(pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc)); } else { - RETURN_IF_ERROR( - pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc)); + RETURN_IF_ERROR(pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc)); } return SUCCESS; } @@ -6982,7 +6986,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) ADDOP_I(c, LOC(m->pattern), COPY, 1); } pc->stores = PyList_New(0); - if (!pc->stores) { + if (pc->stores == NULL) { return ERROR; } // Irrefutable cases must be either guarded, last, or both: @@ -7009,9 +7013,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) // NOTE: Returning macros are safe again. if (m->guard) { RETURN_IF_ERROR(ensure_fail_pop(c, pc, 0)); - RETURN_IF_ERROR( - compiler_jump_if(c, LOC(m->pattern), m->guard, - pc->fail_pop[0], 0)); + RETURN_IF_ERROR(compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0)); } // Success! Pop the subject off, we're done with it: if (i != cases - has_default - 1) { @@ -7037,8 +7039,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc) ADDOP(c, LOC(m->pattern), NOP); } if (m->guard) { - RETURN_IF_ERROR( - compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)); + RETURN_IF_ERROR(compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0)); } VISIT_SEQ(c, stmt, m->body); } From 9c7d675ed2347dcc18730460bed4d7f4ddbd6e42 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 12 Dec 2022 11:27:57 +0000 Subject: [PATCH 90/93] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-12-12-11-27-54.gh-issue-99955.Ix5Rrg.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-12-12-11-27-54.gh-issue-99955.Ix5Rrg.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-12-12-11-27-54.gh-issue-99955.Ix5Rrg.rst b/Misc/NEWS.d/next/Core and Builtins/2022-12-12-11-27-54.gh-issue-99955.Ix5Rrg.rst new file mode 100644 index 00000000000000..e9867b39ad7d1c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-12-12-11-27-54.gh-issue-99955.Ix5Rrg.rst @@ -0,0 +1 @@ +Internal compiler functions (in compile.c) now consistently return -1 on error and 0 on success. From 4271c4389e3653927a88fbdf46ca832f094eb8c0 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 12 Dec 2022 11:31:01 +0000 Subject: [PATCH 91/93] Remove unnecessary () --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index 64d62794b9d9f1..86857744749a8b 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -6810,7 +6810,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc) // Need to NULL this for the PyObject_Free call in the error block. old_pc.fail_pop = NULL; // No match. Pop the remaining copy of the subject and fail: - if ((cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) < 0) || + if (cfg_builder_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) < 0 || jump_to_fail_pop(c, LOC(p), pc, JUMP) < 0) { goto error; } From 48802dc2b88b9594e4b53a6652eb4bcd44567cfe Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 12 Dec 2022 11:32:07 +0000 Subject: [PATCH 92/93] -1 --> ERROR Co-authored-by: Mark Shannon <mark@hotpy.org> --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index 86857744749a8b..fbe7441999ab10 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -60,7 +60,7 @@ #define RETURN_IF_ERROR(X) \ if ((X) == -1) { \ - return -1; \ + return ERROR; \ } /* If we exceed this limit, it should From a47b4821f58c130c7555112cba57b387e9534e01 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Mon, 12 Dec 2022 13:06:33 +0000 Subject: [PATCH 93/93] whitespace --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index fbe7441999ab10..17b164a4d06ef5 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -60,7 +60,7 @@ #define RETURN_IF_ERROR(X) \ if ((X) == -1) { \ - return ERROR; \ + return ERROR; \ } /* If we exceed this limit, it should