fix a bug of with

This commit is contained in:
blueloveTH 2023-07-01 00:50:46 +08:00
parent b13581037d
commit fc2a80122b
2 changed files with 12 additions and 8 deletions

View File

@ -839,12 +839,13 @@ __SUBSCR_END:
EXPR(false); EXPR(false);
consume(TK("as")); consume(TK("as"));
consume(TK("@id")); consume(TK("@id"));
StrName name(prev().str()); Expr_ e = make_expr<NameExpr>(prev().str(), name_scope());
ctx()->emit(OP_STORE_NAME, name.index, prev().line); bool ok = e->emit_store(ctx());
ctx()->emit(OP_LOAD_NAME, name.index, prev().line); if(!ok) SyntaxError();
e->emit(ctx());
ctx()->emit(OP_WITH_ENTER, BC_NOARG, prev().line); ctx()->emit(OP_WITH_ENTER, BC_NOARG, prev().line);
compile_block_body(); compile_block_body();
ctx()->emit(OP_LOAD_NAME, name.index, prev().line); e->emit(ctx());
ctx()->emit(OP_WITH_EXIT, BC_NOARG, prev().line); ctx()->emit(OP_WITH_EXIT, BC_NOARG, prev().line);
} break; } break;
/*************************************************/ /*************************************************/

View File

@ -27,10 +27,13 @@ with open('123.bin', 'wb') as f:
f.write('123'.encode()) f.write('123'.encode())
f.write('测试'.encode()) f.write('测试'.encode())
with open('123.bin', 'rb') as f: def f():
b = f.read() with open('123.bin', 'rb') as f:
assert isinstance(b, bytes) b = f.read()
assert b == '123测试'.encode() assert isinstance(b, bytes)
assert b == '123测试'.encode()
f()
assert os.path.exists('123.bin') assert os.path.exists('123.bin')
os.remove('123.bin') os.remove('123.bin')