mirror of
https://github.com/pocketpy/pocketpy
synced 2026-06-21 07:57:10 +08:00
Compare commits
4 Commits
4fbbd8ec13
...
b2961ae5df
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2961ae5df | ||
|
|
d527b4dbc9 | ||
|
|
8c4ed3bc34 | ||
|
|
c048ec9faf |
@ -56,8 +56,10 @@ static bool mpack_to_py(mpack_node_t node) {
|
|||||||
for(size_t i = 0; i < count; i++) {
|
for(size_t i = 0; i < count; i++) {
|
||||||
mpack_node_t key_node = mpack_node_map_key_at(node, i);
|
mpack_node_t key_node = mpack_node_map_key_at(node, i);
|
||||||
mpack_node_t val_node = mpack_node_map_value_at(node, i);
|
mpack_node_t val_node = mpack_node_map_value_at(node, i);
|
||||||
if(mpack_node_type(key_node) != mpack_type_str) {
|
mpack_type_t key_type = mpack_node_type(key_node);
|
||||||
return TypeError("msgpack: key must be strings");
|
if(key_type != mpack_type_str && key_type != mpack_type_int &&
|
||||||
|
key_type != mpack_type_uint) {
|
||||||
|
return TypeError("msgpack: key must be string or integer");
|
||||||
}
|
}
|
||||||
if(!mpack_to_py(key_node)) return false;
|
if(!mpack_to_py(key_node)) return false;
|
||||||
if(!mpack_to_py(val_node)) return false;
|
if(!mpack_to_py(val_node)) return false;
|
||||||
@ -101,9 +103,14 @@ static bool py_to_mpack(py_Ref object, mpack_writer_t* writer);
|
|||||||
|
|
||||||
static bool mpack_write_dict_kv(py_Ref k, py_Ref v, void* ctx) {
|
static bool mpack_write_dict_kv(py_Ref k, py_Ref v, void* ctx) {
|
||||||
mpack_writer_t* writer = ctx;
|
mpack_writer_t* writer = ctx;
|
||||||
if(k->type != tp_str) return TypeError("msgpack: key must be strings");
|
if(k->type == tp_str) {
|
||||||
c11_sv sv = py_tosv(k);
|
c11_sv sv = py_tosv(k);
|
||||||
mpack_write_str(writer, sv.data, (size_t)sv.size);
|
mpack_write_str(writer, sv.data, (size_t)sv.size);
|
||||||
|
} else if(k->type == tp_int) {
|
||||||
|
mpack_write_int(writer, py_toint(k));
|
||||||
|
} else {
|
||||||
|
return TypeError("msgpack: key must be string or integer");
|
||||||
|
}
|
||||||
bool ok = py_to_mpack(v, writer);
|
bool ok = py_to_mpack(v, writer);
|
||||||
if(!ok) mpack_write_nil(writer);
|
if(!ok) mpack_write_nil(writer);
|
||||||
return ok;
|
return ok;
|
||||||
@ -160,7 +167,10 @@ static bool msgpack_dumps(int argc, py_Ref argv) {
|
|||||||
mpack_writer_init_growable(&writer, &data, &size);
|
mpack_writer_init_growable(&writer, &data, &size);
|
||||||
bool ok = py_to_mpack(argv, &writer);
|
bool ok = py_to_mpack(argv, &writer);
|
||||||
if(mpack_writer_destroy(&writer) != mpack_ok) { assert(false); }
|
if(mpack_writer_destroy(&writer) != mpack_ok) { assert(false); }
|
||||||
if(!ok) return false;
|
if(!ok) {
|
||||||
|
MPACK_FREE(data);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
assert(size <= INT32_MAX);
|
assert(size <= INT32_MAX);
|
||||||
unsigned char* byte_data = py_newbytes(py_retval(), (int)size);
|
unsigned char* byte_data = py_newbytes(py_retval(), (int)size);
|
||||||
memcpy(byte_data, data, size);
|
memcpy(byte_data, data, size);
|
||||||
|
|||||||
@ -228,7 +228,8 @@ static bool str__getitem__(int argc, py_Ref argv) {
|
|||||||
py_Ref _1 = py_arg(1);
|
py_Ref _1 = py_arg(1);
|
||||||
if(_1->type == tp_int) {
|
if(_1->type == tp_int) {
|
||||||
int index = py_toint(py_arg(1));
|
int index = py_toint(py_arg(1));
|
||||||
if(!pk__normalize_index(&index, self.size)) return false;
|
int u8_len = c11_sv__u8_length(self);
|
||||||
|
if(!pk__normalize_index(&index, u8_len)) return false;
|
||||||
c11_sv res = c11_sv__u8_getitem(self, index);
|
c11_sv res = c11_sv__u8_getitem(self, index);
|
||||||
py_newstrv(py_retval(), res);
|
py_newstrv(py_retval(), res);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@ -104,8 +104,10 @@ c11_sv c11_sv__slice(c11_sv sv, int start) { return c11_sv__slice2(sv, start, sv
|
|||||||
|
|
||||||
c11_sv c11_sv__slice2(c11_sv sv, int start, int stop) {
|
c11_sv c11_sv__slice2(c11_sv sv, int start, int stop) {
|
||||||
if(start < 0) start = 0;
|
if(start < 0) start = 0;
|
||||||
if(stop < start) stop = start;
|
if(start > sv.size) start = sv.size;
|
||||||
|
if(stop < 0) stop = 0;
|
||||||
if(stop > sv.size) stop = sv.size;
|
if(stop > sv.size) stop = sv.size;
|
||||||
|
if(stop < start) stop = start;
|
||||||
return (c11_sv){sv.data + start, stop - start};
|
return (c11_sv){sv.data + start, stop - start};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -997,7 +999,6 @@ const static c11_u32_range kLoRanges[] = {
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
bool c11__is_unicode_Lo_char(int c) {
|
bool c11__is_unicode_Lo_char(int c) {
|
||||||
if(c == 0x1f955) return true;
|
|
||||||
const char* data =
|
const char* data =
|
||||||
c11__search_u32_ranges(c, kLoRanges, sizeof(kLoRanges) / sizeof(c11_u32_range));
|
c11__search_u32_ranges(c, kLoRanges, sizeof(kLoRanges) / sizeof(c11_u32_range));
|
||||||
return data != NULL;
|
return data != NULL;
|
||||||
|
|||||||
@ -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;
|
||||||
/*************************************************/
|
/*************************************************/
|
||||||
|
|||||||
@ -1126,14 +1126,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();
|
||||||
}
|
}
|
||||||
///////////
|
///////////
|
||||||
|
|||||||
@ -501,10 +501,10 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
|
|||||||
bool py_pickle_loads(const unsigned char* data, int size) {
|
bool py_pickle_loads(const unsigned char* data, int size) {
|
||||||
const unsigned char* p = data;
|
const unsigned char* p = data;
|
||||||
|
|
||||||
// \xf0\x9f\xa5\x95
|
// PK
|
||||||
if(size < 4 || p[0] != 240 || p[1] != 159 || p[2] != 165 || p[3] != 149)
|
if(size < 2 || p[0] != 'P' || p[1] != 'K')
|
||||||
return ValueError("invalid pickle data");
|
return ValueError("invalid pickle data");
|
||||||
p += 4;
|
p += 2;
|
||||||
|
|
||||||
c11_smallmap_d2d type_mapping;
|
c11_smallmap_d2d type_mapping;
|
||||||
c11_smallmap_d2d__ctor(&type_mapping);
|
c11_smallmap_d2d__ctor(&type_mapping);
|
||||||
@ -780,7 +780,7 @@ bool py_pickle_loads_body(const unsigned char* p, int memo_length, c11_smallmap_
|
|||||||
static bool PickleObject__py_submit(PickleObject* self, py_OutRef out) {
|
static bool PickleObject__py_submit(PickleObject* self, py_OutRef out) {
|
||||||
c11_sbuf cleartext;
|
c11_sbuf cleartext;
|
||||||
c11_sbuf__ctor(&cleartext);
|
c11_sbuf__ctor(&cleartext);
|
||||||
c11_sbuf__write_cstr(&cleartext, "\xf0\x9f\xa5\x95");
|
c11_sbuf__write_cstr(&cleartext, "PK");
|
||||||
// line 1: type mapping
|
// line 1: type mapping
|
||||||
for(py_Type type = 0; type < self->used_types_length; type++) {
|
for(py_Type type = 0; type < self->used_types_length; type++) {
|
||||||
if(self->used_types[type]) {
|
if(self->used_types[type]) {
|
||||||
|
|||||||
@ -221,6 +221,11 @@ assert chr(0x1f955) == '🥕'
|
|||||||
assert ord('测') == 27979
|
assert ord('测') == 27979
|
||||||
assert chr(27979) == '测'
|
assert chr(27979) == '测'
|
||||||
|
|
||||||
|
assert '测试'[0] == '测'
|
||||||
|
assert '测试'[1] == '试'
|
||||||
|
assert '测试'[-1] == '试'
|
||||||
|
assert '测试'[-2] == '测'
|
||||||
|
|
||||||
# test format()
|
# test format()
|
||||||
assert "Hello, {}!".format("World") == "Hello, World!"
|
assert "Hello, {}!".format("World") == "Hello, World!"
|
||||||
assert "{} {} {}".format("I", "love", "Python") == "I love Python"
|
assert "{} {} {}".format("I", "love", "Python") == "I love Python"
|
||||||
|
|||||||
@ -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