mirror of
https://github.com/pocketpy/pocketpy
synced 2026-06-21 07:57:10 +08:00
Compare commits
3 Commits
4fbbd8ec13
...
b2961ae5df
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b2961ae5df | ||
|
|
d527b4dbc9 | ||
|
|
8c4ed3bc34 |
@ -56,8 +56,10 @@ static bool mpack_to_py(mpack_node_t node) {
|
||||
for(size_t i = 0; i < count; 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);
|
||||
if(mpack_node_type(key_node) != mpack_type_str) {
|
||||
return TypeError("msgpack: key must be strings");
|
||||
mpack_type_t key_type = mpack_node_type(key_node);
|
||||
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(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) {
|
||||
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);
|
||||
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);
|
||||
if(!ok) mpack_write_nil(writer);
|
||||
return ok;
|
||||
@ -160,7 +167,10 @@ static bool msgpack_dumps(int argc, py_Ref argv) {
|
||||
mpack_writer_init_growable(&writer, &data, &size);
|
||||
bool ok = py_to_mpack(argv, &writer);
|
||||
if(mpack_writer_destroy(&writer) != mpack_ok) { assert(false); }
|
||||
if(!ok) return false;
|
||||
if(!ok) {
|
||||
MPACK_FREE(data);
|
||||
return false;
|
||||
}
|
||||
assert(size <= INT32_MAX);
|
||||
unsigned char* byte_data = py_newbytes(py_retval(), (int)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);
|
||||
if(_1->type == tp_int) {
|
||||
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);
|
||||
py_newstrv(py_retval(), res);
|
||||
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) {
|
||||
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 < start) stop = start;
|
||||
return (c11_sv){sv.data + start, stop - start};
|
||||
}
|
||||
|
||||
@ -997,7 +999,6 @@ const static c11_u32_range kLoRanges[] = {
|
||||
// clang-format on
|
||||
|
||||
bool c11__is_unicode_Lo_char(int c) {
|
||||
if(c == 0x1f955) return true;
|
||||
const char* data =
|
||||
c11__search_u32_ranges(c, kLoRanges, sizeof(kLoRanges) / sizeof(c11_u32_range));
|
||||
return data != NULL;
|
||||
|
||||
@ -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) {
|
||||
const unsigned char* p = data;
|
||||
|
||||
// \xf0\x9f\xa5\x95
|
||||
if(size < 4 || p[0] != 240 || p[1] != 159 || p[2] != 165 || p[3] != 149)
|
||||
// PK
|
||||
if(size < 2 || p[0] != 'P' || p[1] != 'K')
|
||||
return ValueError("invalid pickle data");
|
||||
p += 4;
|
||||
p += 2;
|
||||
|
||||
c11_smallmap_d2d 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) {
|
||||
c11_sbuf cleartext;
|
||||
c11_sbuf__ctor(&cleartext);
|
||||
c11_sbuf__write_cstr(&cleartext, "\xf0\x9f\xa5\x95");
|
||||
c11_sbuf__write_cstr(&cleartext, "PK");
|
||||
// line 1: type mapping
|
||||
for(py_Type type = 0; type < self->used_types_length; type++) {
|
||||
if(self->used_types[type]) {
|
||||
|
||||
@ -221,6 +221,11 @@ assert chr(0x1f955) == '🥕'
|
||||
assert ord('测') == 27979
|
||||
assert chr(27979) == '测'
|
||||
|
||||
assert '测试'[0] == '测'
|
||||
assert '测试'[1] == '试'
|
||||
assert '测试'[-1] == '试'
|
||||
assert '测试'[-2] == '测'
|
||||
|
||||
# test format()
|
||||
assert "Hello, {}!".format("World") == "Hello, World!"
|
||||
assert "{} {} {}".format("I", "love", "Python") == "I love Python"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user