mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-12 22:50:17 +00:00
fix some bug
This commit is contained in:
parent
3b9493eb1d
commit
b0b3bdef86
@ -130,7 +130,10 @@ static bool py_to_mpack(py_Ref object, mpack_writer_t* writer) {
|
||||
mpack_build_array(writer);
|
||||
for(int i = 0; i < len; i++) {
|
||||
bool ok = py_to_mpack(&data[i], writer);
|
||||
if(!ok) return false;
|
||||
if(!ok) {
|
||||
mpack_complete_array(writer);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
mpack_complete_array(writer);
|
||||
break;
|
||||
@ -138,8 +141,8 @@ static bool py_to_mpack(py_Ref object, mpack_writer_t* writer) {
|
||||
case tp_dict: {
|
||||
mpack_build_map(writer);
|
||||
bool ok = py_dict_apply(object, mpack_write_dict_kv, writer);
|
||||
if(!ok) return false;
|
||||
mpack_complete_map(writer);
|
||||
if(!ok) return false;
|
||||
break;
|
||||
}
|
||||
default: return TypeError("msgpack: unsupported type '%t'", object->type);
|
||||
@ -153,10 +156,9 @@ static bool msgpack_dumps(int argc, py_Ref argv) {
|
||||
size_t size;
|
||||
mpack_writer_t writer;
|
||||
mpack_writer_init_growable(&writer, &data, &size);
|
||||
py_to_mpack(argv, &writer);
|
||||
if(mpack_writer_destroy(&writer) != mpack_ok) {
|
||||
return ValueError("msgpack: writer destroy failed");
|
||||
}
|
||||
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);
|
||||
|
||||
2
include/typings/msgpack.pyi
Normal file
2
include/typings/msgpack.pyi
Normal file
@ -0,0 +1,2 @@
|
||||
def loads(__b: bytes): ...
|
||||
def dumps(__o: object) -> bytes: ...
|
||||
78
tests/72_msgpack.py
Normal file
78
tests/72_msgpack.py
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user