Compare commits

..

2 Commits

Author SHA1 Message Date
blueloveTH
b0b3bdef86 fix some bug 2025-11-11 16:09:33 +08:00
blueloveTH
3b9493eb1d Update bindings.c 2025-11-11 15:02:18 +08:00
3 changed files with 143 additions and 3 deletions

View File

@ -97,13 +97,73 @@ static bool msgpack_loads(int argc, py_Ref argv) {
return true;
}
// static mpack_node_t py_to_mpack(py_Ref object) {
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");
c11_sv sv = py_tosv(k);
mpack_write_str(writer, sv.data, (size_t)sv.size);
return py_to_mpack(v, writer);
}
static bool py_to_mpack(py_Ref object, mpack_writer_t* writer) {
switch(object->type) {
case tp_NoneType: mpack_write_nil(writer); break;
case tp_bool: mpack_write_bool(writer, py_tobool(object)); break;
case tp_int: mpack_write_int(writer, py_toint(object)); break;
case tp_float: mpack_write_double(writer, py_tofloat(object)); break;
case tp_str: {
c11_sv sv = py_tosv(object);
mpack_write_str(writer, sv.data, (size_t)sv.size);
break;
}
case tp_bytes: {
int size;
unsigned char* data = py_tobytes(object, &size);
mpack_write_bin(writer, (const char*)data, (size_t)size);
break;
}
case tp_list: {
int len = py_list_len(object);
py_Ref data = py_list_data(object);
mpack_build_array(writer);
for(int i = 0; i < len; i++) {
bool ok = py_to_mpack(&data[i], writer);
if(!ok) {
mpack_complete_array(writer);
return false;
}
}
mpack_complete_array(writer);
break;
}
case tp_dict: {
mpack_build_map(writer);
bool ok = py_dict_apply(object, mpack_write_dict_kv, writer);
mpack_complete_map(writer);
if(!ok) return false;
break;
}
default: return TypeError("msgpack: unsupported type '%t'", object->type);
}
return true;
}
static bool msgpack_dumps(int argc, py_Ref argv) {
PY_CHECK_ARGC(1);
return ValueError("not yet");
char* data;
size_t size;
mpack_writer_t writer;
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;
assert(size <= INT32_MAX);
unsigned char* byte_data = py_newbytes(py_retval(), (int)size);
memcpy(byte_data, data, size);
MPACK_FREE(data);
return true;
}
void pk__add_module_msgpack() {

View File

@ -0,0 +1,2 @@
def loads(__b: bytes): ...
def dumps(__o: object) -> bytes: ...

78
tests/72_msgpack.py Normal file
View File

@ -0,0 +1,78 @@
try:
import msgpack
except ImportError:
print('msgpack is not enabled, skipping test...')
exit()
a = {
'a': 1,
'b': 2,
'c': None,
'd': [1, 2, 3],
'e': {
'a': 100,
'b': 2.5,
'c': None,
'd': [142, 2785, 39767],
},
"f": 'This is a string',
'g': [True, False, None],
'h': False
}
import msgpack
assert msgpack.loads(b'\x01') == 1
assert msgpack.loads(b'\xa11') == "1"
assert msgpack.loads(b'\xcb\x00\x00\x00\x00\x00\x00\x00\x00') == 0.0
assert msgpack.loads(b'\x92\x01\x02') == [1, 2]
assert msgpack.loads(b'\xc0') == None
assert msgpack.loads(b'\xc3') == True
assert msgpack.loads(b'\xc2') == False
assert msgpack.loads(b'\x80') == {}
_j = msgpack.dumps(a)
_a = msgpack.loads(_j)
for k, v in a.items():
assert (a[k] == _a[k]), f'{a[k]} != {_a[k]}'
for k, v in _a.items():
assert (a[k] == _a[k]), f'{a[k]} != {_a[k]}'
b = [1, 2, True, None, False]
_j = msgpack.dumps(b)
_b = msgpack.loads(_j)
assert b == _b
c = 1.0
_j = msgpack.dumps(c)
_c = msgpack.loads(_j)
assert c == _c
d = True
_j = msgpack.dumps(d)
_d = msgpack.loads(_j)
assert d == _d
# assert msgpack.dumps((1,)) == '[1]'
# assert msgpack.dumps((1, 2, 3)) == '[1, 2, 3]'
# assert msgpack.dumps(tuple()) == '[]'
assert msgpack.dumps([]) == b'\x90'
assert msgpack.dumps([1, 2, 3]) == b'\x93\x01\x02\x03'
assert msgpack.dumps([1]) == b'\x91\x01'
try:
msgpack.dumps({1: 2})
assert False
except TypeError:
assert True
try:
msgpack.dumps(type)
assert False
except TypeError:
assert True