8000 implement Packer.pack_extended_type also in the cython version of the… · zhurs/msgpack-python@5467515 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5467515

Browse files
committed
implement Packer.pack_extended_type also in the cython version of the code
1 parent afa28fb commit 5467515

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

msgpack/_packer.pyx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from cpython cimport *
55
from libc.stdlib cimport *
66
from libc.string cimport *
77
from libc.limits cimport *
8+
from libc.stdint cimport int8_t
89

910
from msgpack.exceptions import PackValueError
1011

@@ -27,6 +28,7 @@ cdef extern from "pack.h":
2728
int msgpack_pack_map(msgpack_packer* pk, size_t l)
2829
int msgpack_pack_raw(msgpack_packer* pk, size_t l)
2930
int msgpack_pack_raw_body(msgpack_packer* pk, char* body, size_t l)
31+
int msgpack_pack_ext(msgpack_packer* pk, int8_t typecode, size_t l)
3032

3133
cdef int DEFAULT_RECURSE_LIMIT=511
3234

@@ -193,6 +195,10 @@ cdef class Packer(object):
193195
self.pk.length = 0
194196
return buf
195197

198+
def pack_extended_type(self, typecode, data):
199+
msgpack_pack_ext(&self.pk, typecode, len(data))
200+
msgpack_pack_raw_body(&self.pk, data, len(data))
201+
196202
def pack_array_header(self, size_t size):
197203
cdef int ret = msgpack_pack_array(&self.pk, size)
198204
if ret == -1:

msgpack/pack.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static inline int msgpack_pack_map(msgpack_packer* pk, unsigned int n);
7070
static inline int msgpack_pack_raw(msgpack_packer* pk, size_t l);
7171
static inline int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
7272

73+
static inline int msgpack_pack_ext(msgpack_packer* pk, int8_t typecode, size_t l);
74+
7375
static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_t l)
7476
{
7577
char* buf = pk->buf;

msgpack/pack_template.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,66 @@ static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t
683683
msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
684684
}
685685

686+
/*
687+
* Ext
688+
*/
689+
690+
static inline int msgpack_pack_ext(msgpack_packer* x, int8_t typecode, size_t l)
691+
{
692+
if (l == 1) {
693+
unsigned char buf[2];
694+
buf[0] = 0xd4;
695+
buf[1] = (unsigned char)typecode;
696+
msgpack_pack_append_buffer(x, buf, 2);
697+
}
698+
else if(l == 2) {
699+
unsigned char buf[2];
700+
buf[0] = 0xd5;
701+
buf[1] = (unsigned char)typecode;
702+
msgpack_pack_append_buffer(x, buf, 2);
703+
}
704+
else if(l == 4) {
705+
unsigned char buf[2];
706+
buf[0] = 0xd6;
707+
buf[1] = (unsigned char)typecode;
708+
msgpack_pack_append_buffer(x, buf, 2);
709+
}
710+
else if(l == 8) {
711+
unsigned char buf[2];
712+
buf[0] = 0xd7;
713+
buf[1] = (unsigned char)typecode;
714+
msgpack_pack_append_buffer(x, buf, 2);
715+
}
716+
else if(l == 16) {
717+
unsigned char buf[2];
718+
buf[0] = 0xd8;
719+
buf[1] = (unsigned char)typecode;
720+
msgpack_pack_append_buffer(x, buf, 2);
721+
}
722+
else if(l < 256) {
723+
unsigned char buf[3];
724+
buf[0] = 0xc7;
725+
buf[1] = l;
726+
buf[2] = (unsigned char)typecode;
727+
msgpack_pack_append_buffer(x, buf, 3);
728+
} else if(l < 65536) {
729+
unsigned char buf[4];
730+
buf[0] = 0xc8;
731+
_msgpack_store16(&buf[1], (uint16_t)l);
732+
buf[3] = (unsigned char)typecode;
733+
msgpack_pack_append_buffer(x, buf, 4);
734+
} else {
735+
unsigned char buf[6];
736+
buf[0] = 0xc9;
737+
_msgpack_store32(&buf[1], (uint32_t)l);
738+
buf[5] = (unsigned char)typecode;
739+
msgpack_pack_append_buffer(x, buf, 6);
740+
}
6D47
741+
742+
}
743+
744+
745+
686746
#undef msgpack_pack_append_buffer
687747

688748
#undef TAKE8_8

test/test_extension.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def test_pack_extended_type():
66
def p(s):
77
packer = msgpack.Packer()
88
packer.pack_extended_type(0x42, s)
9-
return packer._buffer.getvalue()
9+
return packer.bytes()
1010
assert p('A') == '\xd4\x42A' # fixext 1
1111
assert p('AB') == '\xd5\x42AB' # fixext 2
1212
assert p('ABCD') == '\xd6\x42ABCD' # fixext 4

0 commit comments

Comments
 (0)
0