From 18fc4c02d308abd9ac7f5d76a8a958892eff73eb Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 29 Sep 2024 17:24:33 +0800 Subject: [PATCH] support `yield` implicit None --- src/compiler/compiler.c | 12 ++++++++---- src/interpreter/ceval.c | 8 ++++++-- tests/51_yield.py | 2 ++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 35566882..6b6acc1b 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -2498,10 +2498,14 @@ static Error* compile_stmt(Compiler* self) { break; case TK_YIELD: if(self->contexts.length <= 1) return SyntaxError(self, "'yield' outside function"); - check(EXPR_TUPLE(self)); - Ctx__s_emit_top(ctx()); - Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line); - consume_end_stmt(); + if(match_end_stmt(self)) { + Ctx__emit_(ctx(), OP_YIELD_VALUE, 1, kw_line); + } else { + check(EXPR_TUPLE(self)); + Ctx__s_emit_top(ctx()); + Ctx__emit_(ctx(), OP_YIELD_VALUE, BC_NOARG, kw_line); + consume_end_stmt(); + } break; case TK_YIELD_FROM: check(compile_yield_from(self, kw_line)); diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index e75d3039..49e21500 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -715,8 +715,12 @@ FrameResult VM__run_top_frame(VM* self) { DISPATCH(); } case OP_YIELD_VALUE: { - py_assign(py_retval(), TOP()); - POP(); + if(byte.arg == 1) { + py_newnone(py_retval()); + } else { + py_assign(py_retval(), TOP()); + POP(); + } return RES_YIELD; } ///////// diff --git a/tests/51_yield.py b/tests/51_yield.py index 880ea20e..d218e05c 100644 --- a/tests/51_yield.py +++ b/tests/51_yield.py @@ -1,10 +1,12 @@ def g(): yield 1 yield 2 + yield a = g() assert next(a) == 1 assert next(a, None) == 2 +assert next(a) == None try: next(a)