mirror of
https://github.com/pocketpy/pocketpy
synced 2026-03-21 20:50:16 +00:00
Compare commits
5 Commits
2078b72ab2
...
53f0164b7e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
53f0164b7e | ||
|
|
24428793fc | ||
|
|
d5b711f50c | ||
|
|
cbbe319520 | ||
|
|
c048ec9faf |
@ -60,16 +60,18 @@ def filter(f, iterable):
|
|||||||
if f(i):
|
if f(i):
|
||||||
yield i
|
yield i
|
||||||
|
|
||||||
def zip(a, b):
|
class zip:
|
||||||
a = iter(a)
|
def __init__(self, *iterables):
|
||||||
b = iter(b)
|
self.iterables = [iter(it) for it in iterables]
|
||||||
while True:
|
|
||||||
try:
|
def __iter__(self):
|
||||||
ai = next(a)
|
return self
|
||||||
bi = next(b)
|
|
||||||
except StopIteration:
|
def __next__(self):
|
||||||
break
|
result = []
|
||||||
yield ai, bi
|
for it in self.iterables:
|
||||||
|
result.append(next(it))
|
||||||
|
return tuple(result)
|
||||||
|
|
||||||
def reversed(iterable):
|
def reversed(iterable):
|
||||||
a = list(iterable)
|
a = list(iterable)
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@ -2301,6 +2301,9 @@ static Error* _compile_f_args(Compiler* self, FuncDecl* decl, bool is_lambda) {
|
|||||||
int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
|
int state = 0; // 0 for args, 1 for *args, 2 for k=v, 3 for **kwargs
|
||||||
Error* err;
|
Error* err;
|
||||||
do {
|
do {
|
||||||
|
// allow trailing comma
|
||||||
|
if(!is_lambda && curr()->type == TK_RPAREN) break;
|
||||||
|
|
||||||
if(state >= 3) return SyntaxError(self, "**kwargs should be the last argument");
|
if(state >= 3) return SyntaxError(self, "**kwargs should be the last argument");
|
||||||
if(match(TK_MUL)) {
|
if(match(TK_MUL)) {
|
||||||
if(state < 1)
|
if(state < 1)
|
||||||
@ -2798,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)) {
|
||||||
@ -2806,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;
|
||||||
/*************************************************/
|
/*************************************************/
|
||||||
|
|||||||
@ -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();
|
||||||
}
|
}
|
||||||
///////////
|
///////////
|
||||||
|
|||||||
@ -156,6 +156,17 @@ def f(a,
|
|||||||
|
|
||||||
assert f(1, 2) == 3
|
assert f(1, 2) == 3
|
||||||
|
|
||||||
|
|
||||||
|
# https://github.com/pocketpy/pocketpy/issues/458
|
||||||
|
def f(
|
||||||
|
x: int,
|
||||||
|
y: int,
|
||||||
|
):
|
||||||
|
return x + y
|
||||||
|
|
||||||
|
assert f(1, 2) == 3
|
||||||
|
|
||||||
|
|
||||||
# try:
|
# try:
|
||||||
# f(a=1)
|
# f(a=1)
|
||||||
# exit(1)
|
# exit(1)
|
||||||
|
|||||||
@ -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}"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -381,6 +381,21 @@ for x, y in zip(d, e):
|
|||||||
# verify that original order and values are retained.
|
# verify that original order and values are retained.
|
||||||
assertEqual(x is y, True)
|
assertEqual(x is y, True)
|
||||||
|
|
||||||
|
# https://github.com/pocketpy/pocketpy/issues/447
|
||||||
|
names = ["Alice", "Bob", "Charlie"]
|
||||||
|
ages = [25, 30, 35]
|
||||||
|
cities = ["NY", "LA", "SF"]
|
||||||
|
result = []
|
||||||
|
for name, age, city in zip(names, ages, cities):
|
||||||
|
result.append((name, age, city))
|
||||||
|
assertEqual(result, [("Alice", 25, "NY"), ("Bob", 30, "LA"), ("Charlie", 35, "SF")])
|
||||||
|
|
||||||
|
cities.pop()
|
||||||
|
result = []
|
||||||
|
for name, age, city in zip(names, ages, cities):
|
||||||
|
result.append((name, age, city))
|
||||||
|
assertEqual(result, [("Alice", 25, "NY"), ("Bob", 30, "LA")])
|
||||||
|
|
||||||
########### test repr#############
|
########### test repr#############
|
||||||
d = deque(range(200))
|
d = deque(range(200))
|
||||||
e = eval(repr(d))
|
e = eval(repr(d))
|
||||||
|
|||||||
@ -187,10 +187,27 @@ DEALINGS IN THE SOFTWARE.
|
|||||||
}
|
}
|
||||||
|
|
||||||
#run-button {
|
#run-button {
|
||||||
padding-left: 10px;
|
padding-left: 20px;
|
||||||
padding-right: 10px;
|
padding-right: 20px;
|
||||||
|
padding-bottom: 1px;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
background-color: #007bff;
|
||||||
|
color: #ffffff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
outline: none;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
#run-button:hover,
|
||||||
|
#run-button:focus {
|
||||||
|
background-color: #0056b3;
|
||||||
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||||
|
}
|
||||||
|
#run-button:active {
|
||||||
|
background-color: #004085;
|
||||||
|
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
|
||||||
|
transform: translateY(1px);
|
||||||
}
|
}
|
||||||
|
|
||||||
#code-editor.hljs {
|
#code-editor.hljs {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user