Compare commits

...

4 Commits

Author SHA1 Message Date
Kanika Kapoor
834986dd5b
Merge c048ec9faf4bfe02da004dce69471897933c3617 into 35917fc5f4b25375169e5e2417ae659ef0528200 2026-03-07 15:14:46 +05:30
blueloveTH
35917fc5f4 fix #468 2026-03-04 14:30:35 +08:00
Tanisha Vasudeva
1d33267092
feat: implement dict.popitem() method (#443) (#467)
* Added popitem()

* fix

---------

Co-authored-by: blueloveTH <blueloveTH@foxmail.com>
2026-03-03 14:55:56 +08:00
Kanika Kapoor
c048ec9faf Fix context manager __exit__ not being called on exception (#395)
Problem: When an exception occurs in a WITH block, __exit__ was not called,
preventing proper cleanup of context managers.

Solution:
1. Wrap WITH block body in try-except structure
2. On normal exit: call __exit__(None, None, None)
3. On exception: call __exit__ with exception info before re-raising

Changes:
- compiler.c: Wrap WITH body in try-except, ensure __exit__ called in both paths
- ceval.c: Update OP_WITH_EXIT to accept three arguments (exc_type, exc_val, exc_tb)
- tests/520_context.py: Add test to verify __exit__ called on exceptions
2025-12-27 01:12:15 +05:30
9 changed files with 108 additions and 8 deletions

View File

@ -477,12 +477,14 @@ static bool str_rjust(int argc, py_Ref argv) { return str__widthjust_impl(false,
static bool str_find(int argc, py_Ref argv) { static bool str_find(int argc, py_Ref argv) {
if(argc > 3) return TypeError("find() takes at most 3 arguments"); if(argc > 3) return TypeError("find() takes at most 3 arguments");
c11_string* self = pk_tostr(&argv[0]);
int start = 0; int start = 0;
if(argc == 3) { if(argc == 3) {
PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(2, tp_int);
start = py_toint(py_arg(2)); start = py_toint(py_arg(2));
if(start < 0) start += c11_sv__u8_length(c11_string__sv(self));
if(start < 0) start = 0;
} }
c11_string* self = pk_tostr(&argv[0]);
PY_CHECK_ARG_TYPE(1, tp_str); PY_CHECK_ARG_TYPE(1, tp_str);
c11_string* sub = pk_tostr(&argv[1]); c11_string* sub = pk_tostr(&argv[1]);
int res = c11_sv__index2(c11_string__sv(self), c11_string__sv(sub), start); int res = c11_sv__index2(c11_string__sv(self), c11_string__sv(sub), start);

View File

@ -2801,6 +2801,8 @@ static Error* compile_stmt(Compiler* self) {
case TK_WITH: { case TK_WITH: {
check(EXPR(self)); // [ <expr> ] check(EXPR(self)); // [ <expr> ]
Ctx__s_emit_top(ctx()); Ctx__s_emit_top(ctx());
// Save context manager for later __exit__ call
Ctx__emit_(ctx(), OP_DUP_TOP, BC_NOARG, prev()->line);
Ctx__enter_block(ctx(), CodeBlockType_WITH); Ctx__enter_block(ctx(), CodeBlockType_WITH);
NameExpr* as_name = NULL; NameExpr* as_name = NULL;
if(match(TK_AS)) { if(match(TK_AS)) {
@ -2809,17 +2811,33 @@ static Error* compile_stmt(Compiler* self) {
as_name = NameExpr__new(prev()->line, name, name_scope(self)); as_name = NameExpr__new(prev()->line, name, name_scope(self));
} }
Ctx__emit_(ctx(), OP_WITH_ENTER, BC_NOARG, prev()->line); Ctx__emit_(ctx(), OP_WITH_ENTER, BC_NOARG, prev()->line);
// [ <expr> <expr>.__enter__() ]
if(as_name) { if(as_name) {
bool ok = vtemit_store((Expr*)as_name, ctx()); bool ok = vtemit_store((Expr*)as_name, ctx());
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);
} }
// Wrap body in try-except to ensure __exit__ is called even on exception
Ctx__enter_block(ctx(), CodeBlockType_TRY);
Ctx__emit_(ctx(), OP_BEGIN_TRY, BC_NOARG, prev()->line);
check(compile_block_body(self)); check(compile_block_body(self));
Ctx__emit_(ctx(), OP_END_TRY, BC_NOARG, BC_KEEPLINE);
// Normal exit: call __exit__(None, None, None)
Ctx__emit_(ctx(), OP_LOAD_NONE, BC_NOARG, prev()->line);
Ctx__emit_(ctx(), OP_LOAD_NONE, BC_NOARG, prev()->line);
Ctx__emit_(ctx(), OP_LOAD_NONE, BC_NOARG, prev()->line);
Ctx__emit_(ctx(), OP_WITH_EXIT, BC_NOARG, prev()->line); Ctx__emit_(ctx(), OP_WITH_EXIT, BC_NOARG, prev()->line);
int jump_patch = Ctx__emit_(ctx(), OP_JUMP_FORWARD, BC_NOARG, BC_KEEPLINE);
Ctx__exit_block(ctx());
// Exception handler: call __exit__ with exception info, then re-raise
Ctx__emit_(ctx(), OP_PUSH_EXCEPTION, BC_NOARG, BC_KEEPLINE);
Ctx__emit_(ctx(), OP_LOAD_NONE, BC_NOARG, BC_KEEPLINE); // exc_type
Ctx__emit_(ctx(), OP_ROT_TWO, BC_NOARG, BC_KEEPLINE); // reorder: [cm, None, exc]
Ctx__emit_(ctx(), OP_LOAD_NONE, BC_NOARG, BC_KEEPLINE); // exc_tb
Ctx__emit_(ctx(), OP_WITH_EXIT, BC_NOARG, prev()->line);
Ctx__emit_(ctx(), OP_RE_RAISE, BC_NOARG, BC_KEEPLINE);
Ctx__patch_jump(ctx(), jump_patch);
Ctx__exit_block(ctx()); Ctx__exit_block(ctx());
} break; } break;
/*************************************************/ /*************************************************/

View File

@ -1122,14 +1122,35 @@ __NEXT_STEP:
DISPATCH(); DISPATCH();
} }
case OP_WITH_EXIT: { case OP_WITH_EXIT: {
// [expr] // Stack: [cm, exc_type, exc_val, exc_tb]
py_push(TOP()); // Call cm.__exit__(exc_type, exc_val, exc_tb)
py_Ref exc_tb = TOP();
py_Ref exc_val = SECOND();
py_Ref exc_type = THIRD();
py_Ref cm = FOURTH();
// Save all values from stack
py_TValue saved_cm = *cm;
py_TValue saved_exc_type = *exc_type;
py_TValue saved_exc_val = *exc_val;
py_TValue saved_exc_tb = *exc_tb;
self->stack.sp -= 4;
// Push cm and get __exit__ method
py_push(&saved_cm);
if(!py_pushmethod(__exit__)) { if(!py_pushmethod(__exit__)) {
TypeError("'%t' object does not support the context manager protocol", TOP()->type); TypeError("'%t' object does not support the context manager protocol", saved_cm.type);
goto __ERROR; goto __ERROR;
} }
if(!py_vectorcall(0, 0)) goto __ERROR;
POP(); // Push arguments: exc_type, exc_val, exc_tb
PUSH(&saved_exc_type);
PUSH(&saved_exc_val);
PUSH(&saved_exc_tb);
// Call __exit__(exc_type, exc_val, exc_tb)
if(!py_vectorcall(3, 0)) goto __ERROR;
py_pop(); // discard return value
DISPATCH(); DISPATCH();
} }
/////////// ///////////

View File

@ -569,6 +569,24 @@ static bool dict_pop(int argc, py_Ref argv) {
return true; return true;
} }
static bool dict_popitem(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv);
for(int i = self->entries.length - 1; i >= 0; i--) {
DictEntry* entry = c11__at(DictEntry, &self->entries, i);
if(py_isnil(&entry->key)) continue;
py_Ref p = py_newtuple(py_pushtmp(), 2);
p[0] = entry->key;
p[1] = entry->val;
int res = Dict__pop(self, &p[0]);
c11__rtassert(res == 1);
py_assign(py_retval(), py_peek(-1));
py_pop();
return true;
}
return KeyError(py_None());
}
static bool dict_keys(int argc, py_Ref argv) { static bool dict_keys(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
Dict* self = py_touserdata(argv); Dict* self = py_touserdata(argv);
@ -616,6 +634,7 @@ py_Type pk_dict__register() {
py_bindmethod(type, "update", dict_update); py_bindmethod(type, "update", dict_update);
py_bindmethod(type, "get", dict_get); py_bindmethod(type, "get", dict_get);
py_bindmethod(type, "pop", dict_pop); py_bindmethod(type, "pop", dict_pop);
py_bindmethod(type, "popitem", dict_popitem);
py_bindmethod(type, "keys", dict_keys); py_bindmethod(type, "keys", dict_keys);
py_bindmethod(type, "values", dict_values); py_bindmethod(type, "values", dict_values);
py_bindmethod(type, "items", dict_items); py_bindmethod(type, "items", dict_items);

View File

@ -317,6 +317,8 @@ static bool list_index(int argc, py_Ref argv) {
if(argc == 3) { if(argc == 3) {
PY_CHECK_ARG_TYPE(2, tp_int); PY_CHECK_ARG_TYPE(2, tp_int);
start = py_toint(py_arg(2)); start = py_toint(py_arg(2));
if(start < 0) start += py_list_len(py_arg(0));
if(start < 0) start = 0;
} }
for(int i = start; i < py_list_len(py_arg(0)); i++) { for(int i = start; i < py_list_len(py_arg(0)); i++) {
int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1)); int res = py_equal(py_list_getitem(py_arg(0), i), py_arg(1));

View File

@ -169,6 +169,7 @@ assert a.index('23') == 1
assert a.index('2', 1) == 1 assert a.index('2', 1) == 1
assert a.index('1', 0) == 0 assert a.index('1', 0) == 0
assert a.index('2', -2) == 1
assert a.find('1') == 0 assert a.find('1') == 0
assert a.find('1', 1) == -1 assert a.find('1', 1) == -1

View File

@ -75,6 +75,7 @@ assert a.index(3) == 2
assert a.index(2, 1) == 1 assert a.index(2, 1) == 1
assert a.index(1, 0) == 0 assert a.index(1, 0) == 0
assert a.index(2, -2) == 1
a, b = [1, 2] a, b = [1, 2]
assert a == 1 and b == 2 assert a == 1 and b == 2

View File

@ -142,6 +142,17 @@ for i in range(n):
del a[str(i)] del a[str(i)]
assert len(a) == 0 assert len(a) == 0
# test popitem
n = 2 ** 17
a = {}
for i in range(n):
a[str(i)] = i
for i in range(n):
k, v = a.popitem()
assert k == str(n - 1 - i)
assert v == n - 1 - i
assert len(a) == 0
# test del with int keys # test del with int keys
if 0: if 0:
n = 2 ** 17 n = 2 ** 17

View File

@ -27,4 +27,29 @@ assert path == ['enter', 'in', 'exit']
path.clear() path.clear()
# Test that __exit__ is called even when an exception occurs
class B:
def __init__(self):
self.path = []
def __enter__(self):
path.append('enter')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
path.append('exit')
if exc_type is not None:
path.append('exception')
return False # propagate exception
try:
with B():
path.append('before_raise')
raise ValueError('test')
path.append('after_raise') # should not be reached
except ValueError:
pass
assert path == ['enter', 'before_raise', 'exit', 'exception'], f"Expected ['enter', 'before_raise', 'exit', 'exception'], got {path}"