fix context bug

This commit is contained in:
blueloveTH 2024-12-06 13:22:02 +08:00
parent 72e88892e5
commit 93c2608c19
3 changed files with 13 additions and 0 deletions

View File

@ -2694,6 +2694,7 @@ static Error* compile_stmt(Compiler* self) {
vtdelete((Expr*)as_name); vtdelete((Expr*)as_name);
if(!ok) return SyntaxError(self, "invalid syntax"); if(!ok) return SyntaxError(self, "invalid syntax");
} else { } else {
// discard `__enter__()`'s return value
Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE); Ctx__emit_(ctx(), OP_POP_TOP, BC_NOARG, BC_KEEPLINE);
} }
check(compile_block_body(self, compile_stmt)); check(compile_block_body(self, compile_stmt));

View File

@ -1020,6 +1020,7 @@ FrameResult VM__run_top_frame(VM* self) {
goto __ERROR; goto __ERROR;
} }
if(!py_vectorcall(0, 0)) goto __ERROR; if(!py_vectorcall(0, 0)) goto __ERROR;
POP();
DISPATCH(); DISPATCH();
} }
/////////// ///////////

View File

@ -92,3 +92,14 @@ class fixed(TValue[int]):
return super().__new__(cls, int(value)) return super().__new__(cls, int(value))
assert fixed('123').value == 123 assert fixed('123').value == 123
# context bug
class Context:
def __enter__(self):
return 1
def __exit__(self, *_):
pass
for _ in range(5):
with Context() as x:
assert x == 1