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);
consume(TK("as"));
consume(TK("@id"));
StrName name(prev().str());
ctx()->emit(OP_STORE_NAME, name.index, prev().line);
ctx()->emit(OP_LOAD_NAME, name.index, prev().line);
Expr_ e = make_expr<NameExpr>(prev().str(), name_scope());
bool ok = e->emit_store(ctx());
if(!ok) SyntaxError();
e->emit(ctx());
ctx()->emit(OP_WITH_ENTER, BC_NOARG, prev().line);
compile_block_body();
ctx()->emit(OP_LOAD_NAME, name.index, prev().line);
e->emit(ctx());
ctx()->emit(OP_WITH_EXIT, BC_NOARG, prev().line);
} break;
/*************************************************/

View File

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