ソースを参照

fix context bug

blueloveTH 1 年間 前
コミット
93c2608c19
3 ファイル変更13 行追加0 行削除
  1. 1 0
      src/compiler/compiler.c
  2. 1 0
      src/interpreter/ceval.c
  3. 11 0
      tests/99_extras.py

+ 1 - 0
src/compiler/compiler.c

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

+ 1 - 0
src/interpreter/ceval.c

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

+ 11 - 0
tests/99_extras.py

@@ -92,3 +92,14 @@ class fixed(TValue[int]):
         return super().__new__(cls, int(value))
     
 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