mirror of
https://github.com/pocketpy/pocketpy
synced 2026-05-15 06:33:38 +00:00
Compare commits
5 Commits
e076d16892
...
be36b1781c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be36b1781c | ||
|
|
2600eb5352 | ||
|
|
2a2e9ae1f2 | ||
|
|
6e3ed84a9e | ||
|
|
c048ec9faf |
@ -5,7 +5,7 @@ intptr = int
|
|||||||
def malloc(size: int) -> intptr: ...
|
def malloc(size: int) -> intptr: ...
|
||||||
def free(ptr: intptr) -> None: ...
|
def free(ptr: intptr) -> None: ...
|
||||||
|
|
||||||
def memcpy(dst: intptr, src: intptr, n: int) -> None: ...
|
def memcpy(dst: intptr, src: intptr | bytes, n: int) -> None: ...
|
||||||
def memset(s: intptr, c: int, n: int) -> None: ...
|
def memset(s: intptr, c: int, n: int) -> None: ...
|
||||||
def memcmp(s1: intptr, s2: intptr, n: int) -> int: ...
|
def memcmp(s1: intptr, s2: intptr, n: int) -> int: ...
|
||||||
|
|
||||||
|
|||||||
@ -2847,6 +2847,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)) {
|
||||||
@ -2855,17 +2857,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();
|
||||||
}
|
}
|
||||||
///////////
|
///////////
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include "pocketpy/common/utils.h"
|
||||||
#include "pocketpy/pocketpy.h"
|
#include "pocketpy/pocketpy.h"
|
||||||
#include "lz4/lib/lz4.h"
|
#include "lz4/lib/lz4.h"
|
||||||
|
|
||||||
@ -10,13 +11,13 @@ static bool lz4_compress(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARG_TYPE(0, tp_bytes);
|
PY_CHECK_ARG_TYPE(0, tp_bytes);
|
||||||
int src_size;
|
int src_size;
|
||||||
const void* src = py_tobytes(argv, &src_size);
|
const void* src = py_tobytes(argv, &src_size);
|
||||||
int dst_capacity = LZ4_compressBound(src_size);
|
uint32_t dst_capacity = LZ4_compressBound(src_size);
|
||||||
char* p = (char*)py_newbytes(py_retval(), sizeof(int) + dst_capacity);
|
char* p = (char*)py_newbytes(py_retval(), sizeof(uint32_t) + dst_capacity);
|
||||||
memcpy(p, &src_size, sizeof(int));
|
memcpy(p, &src_size, sizeof(uint32_t));
|
||||||
char* dst = p + sizeof(int);
|
char* dst = p + sizeof(uint32_t);
|
||||||
int dst_size = LZ4_compress_default(src, dst, src_size, dst_capacity);
|
int dst_size = LZ4_compress_default(src, dst, src_size, dst_capacity);
|
||||||
if(dst_size <= 0) return ValueError("LZ4 compression failed");
|
if(dst_size <= 0) return ValueError("LZ4 compression failed");
|
||||||
py_bytes_resize(py_retval(), sizeof(int) + dst_size);
|
py_bytes_resize(py_retval(), sizeof(uint32_t) + dst_size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,15 +25,16 @@ static bool lz4_decompress(int argc, py_Ref argv) {
|
|||||||
PY_CHECK_ARGC(1);
|
PY_CHECK_ARGC(1);
|
||||||
PY_CHECK_ARG_TYPE(0, tp_bytes);
|
PY_CHECK_ARG_TYPE(0, tp_bytes);
|
||||||
int total_size;
|
int total_size;
|
||||||
const int* p = (int*)py_tobytes(argv, &total_size);
|
const uint32_t* p = (uint32_t*)py_tobytes(argv, &total_size);
|
||||||
const char* src = (const char*)(p + 1);
|
const char* src = (const char*)(p + 1);
|
||||||
if(total_size < sizeof(int)) return ValueError("invalid LZ4 data");
|
if(total_size < sizeof(uint32_t)) return ValueError("invalid LZ4 data");
|
||||||
int uncompressed_size = *p;
|
uint32_t uncompressed_size;
|
||||||
if(uncompressed_size < 0) return ValueError("invalid LZ4 data");
|
memcpy(&uncompressed_size, p, sizeof(uint32_t));
|
||||||
|
if(uncompressed_size >= INT32_MAX) return ValueError("invalid LZ4 data");
|
||||||
char* dst = (char*)py_newbytes(py_retval(), uncompressed_size);
|
char* dst = (char*)py_newbytes(py_retval(), uncompressed_size);
|
||||||
int dst_size = LZ4_decompress_safe(src, dst, total_size - sizeof(int), uncompressed_size);
|
int dst_size = LZ4_decompress_safe(src, dst, total_size - sizeof(uint32_t), uncompressed_size);
|
||||||
if(dst_size < 0) return ValueError("LZ4 decompression failed");
|
if(dst_size < 0) return ValueError("LZ4 decompression failed");
|
||||||
assert(dst_size == uncompressed_size);
|
c11__rtassert(dst_size == uncompressed_size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -124,12 +124,19 @@ static bool stdc_free(int argc, py_Ref argv) {
|
|||||||
|
|
||||||
static bool stdc_memcpy(int argc, py_Ref argv) {
|
static bool stdc_memcpy(int argc, py_Ref argv) {
|
||||||
PY_CHECK_ARGC(3);
|
PY_CHECK_ARGC(3);
|
||||||
PY_CHECK_ARG_TYPE(0, tp_int);
|
PY_CHECK_ARG_TYPE(0, tp_int); // dst
|
||||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
|
||||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
|
||||||
void* dst = (void*)(intptr_t)py_toint(&argv[0]);
|
void* dst = (void*)(intptr_t)py_toint(&argv[0]);
|
||||||
void* src = (void*)(intptr_t)py_toint(&argv[1]);
|
PY_CHECK_ARG_TYPE(2, tp_int); // n
|
||||||
py_i64 n = py_toint(&argv[2]);
|
py_i64 n = py_toint(&argv[2]);
|
||||||
|
void* src;
|
||||||
|
if(py_istype(&argv[1], tp_bytes)) {
|
||||||
|
int size;
|
||||||
|
src = py_tobytes(&argv[1], &size);
|
||||||
|
if(size < n) n = size;
|
||||||
|
} else {
|
||||||
|
PY_CHECK_ARG_TYPE(1, tp_int); // src
|
||||||
|
src = (void*)(intptr_t)py_toint(&argv[1]);
|
||||||
|
}
|
||||||
memcpy(dst, src, (size_t)n);
|
memcpy(dst, src, (size_t)n);
|
||||||
py_newnone(py_retval());
|
py_newnone(py_retval());
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -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}"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user