diff -r 45713818fd81 Include/opcode.h --- a/Include/opcode.h Thu Nov 24 23:31:59 2016 +0100 +++ b/Include/opcode.h Fri Nov 25 11:43:40 2016 +0100 @@ -12,6 +12,7 @@ extern "C" { #define ROT_THREE 3 #define DUP_TOP 4 #define DUP_TOP_TWO 5 +#define RETURN_NONE 6 #define NOP 9 #define UNARY_POSITIVE 10 #define UNARY_NEGATIVE 11 diff -r 45713818fd81 Lib/opcode.py --- a/Lib/opcode.py Thu Nov 24 23:31:59 2016 +0100 +++ b/Lib/opcode.py Fri Nov 25 11:43:40 2016 +0100 @@ -60,6 +60,7 @@ def_op('ROT_TWO', 2) def_op('ROT_THREE', 3) def_op('DUP_TOP', 4) def_op('DUP_TOP_TWO', 5) +def_op('RETURN_NONE', 6) def_op('NOP', 9) def_op('UNARY_POSITIVE', 10) diff -r 45713818fd81 Python/ceval.c --- a/Python/ceval.c Thu Nov 24 23:31:59 2016 +0100 +++ b/Python/ceval.c Fri Nov 25 11:43:40 2016 +0100 @@ -1851,6 +1851,13 @@ PyObject* _Py_HOT_FUNCTION goto fast_block_end; } + TARGET(RETURN_NONE) { + Py_INCREF(Py_None); + retval = Py_None; + why = WHY_RETURN; + goto fast_block_end; + } + TARGET(GET_AITER) { unaryfunc getter = NULL; PyObject *iter = NULL; diff -r 45713818fd81 Python/compile.c --- a/Python/compile.c Thu Nov 24 23:31:59 2016 +0100 +++ b/Python/compile.c Fri Nov 25 11:43:40 2016 +0100 @@ -66,7 +66,7 @@ typedef struct basicblock_ { struct basicblock_ *b_next; /* b_seen is used to perform a DFS of basicblocks. */ unsigned b_seen : 1; - /* b_return is true if a RETURN_VALUE opcode is inserted. */ + /* b_return is true if a RETURN_VALUE or RETURN_NONE opcode is inserted. */ unsigned b_return : 1; /* depth of stack upon entry of block, computed by stackdepth() */ int b_startdepth; @@ -942,6 +942,8 @@ PyCompile_OpcodeStackEffect(int opcode, return -1; /* XXX Sometimes more */ case RETURN_VALUE: return -1; + case RETURN_NONE: + return 0; case IMPORT_STAR: return -1; case SETUP_ANNOTATIONS: @@ -1102,7 +1104,7 @@ compiler_addop(struct compiler *c, int o i = &b->b_instr[off]; i->i_opcode = opcode; i->i_oparg = 0; - if (opcode == RETURN_VALUE) + if (opcode == RETURN_VALUE || opcode == RETURN_NONE) b->b_return = 1; compiler_set_lineno(c, off); return 1; @@ -2815,10 +2817,11 @@ compiler_visit_stmt(struct compiler *c, return compiler_error( c, "'return' with value in async generator"); VISIT(c, expr, s->v.Return.value); + ADDOP(c, RETURN_VALUE); } - else - ADDOP_O(c, LOAD_CONST, Py_None, consts); - ADDOP(c, RETURN_VALUE); + else { + ADDOP(c, RETURN_NONE); + } break; case Delete_kind: VISIT_SEQ(c, expr, s->v.Delete.targets) @@ -5321,9 +5324,12 @@ assemble(struct compiler *c, int addNone */ if (!c->u->u_curblock->b_return) { NEXT_BLOCK(c); - if (addNone) - ADDOP_O(c, LOAD_CONST, Py_None, consts); - ADDOP(c, RETURN_VALUE); + if (addNone) { + ADDOP(c, RETURN_NONE); + } + else { + ADDOP(c, RETURN_VALUE); + } } nblocks = 0; diff -r 45713818fd81 Python/opcode_targets.h --- a/Python/opcode_targets.h Thu Nov 24 23:31:59 2016 +0100 +++ b/Python/opcode_targets.h Fri Nov 25 11:43:40 2016 +0100 @@ -5,7 +5,7 @@ static void *opcode_targets[256] = { &&TARGET_ROT_THREE, &&TARGET_DUP_TOP, &&TARGET_DUP_TOP_TWO, - &&_unknown_opcode, + &&TARGET_RETURN_NONE, &&_unknown_opcode, &&_unknown_opcode, &&TARGET_NOP, diff -r 45713818fd81 Python/peephole.c --- a/Python/peephole.c Thu Nov 24 23:31:59 2016 +0100 +++ b/Python/peephole.c Fri Nov 25 11:43:40 2016 +0100 @@ -683,8 +683,10 @@ PyCode_Optimize(PyObject *code, PyObject tgt = find_op(codestr, h); /* Replace JUMP_* to a RETURN into just a RETURN */ if (UNCONDITIONAL_JUMP(opcode) && - _Py_OPCODE(codestr[tgt]) == RETURN_VALUE) { - codestr[op_start] = PACKOPARG(RETURN_VALUE, 0); + (_Py_OPCODE(codestr[tgt]) == RETURN_VALUE + || _Py_OPCODE(codestr[tgt]) == RETURN_NONE)) { + int ret_opcode = _Py_OPCODE(codestr[tgt]); + codestr[op_start] = PACKOPARG(ret_opcode, 0); fill_nops(codestr, op_start + 1, i + 1); } else if (UNCONDITIONAL_JUMP(_Py_OPCODE(codestr[tgt]))) { j = GETJUMPTGT(codestr, tgt); @@ -701,8 +703,9 @@ PyCode_Optimize(PyObject *code, PyObject } break; - /* Remove unreachable ops after RETURN */ + /* Remove unreachable ops after RETURN */ case RETURN_VALUE: + case RETURN_NONE: h = i + 1; while (h < codelen && ISBASICBLOCK(blocks, i, h)) { h++;