mirror of
				https://github.com/msgpack/msgpack-python.git
				synced 2025-11-04 11:30:57 +00:00 
			
		
		
		
	implement Packer.pack_extended_type also in the cython version of the code
This commit is contained in:
		
							parent
							
								
									afa28fb205
								
							
						
					
					
						commit
						5467515065
					
				
					 4 changed files with 69 additions and 1 deletions
				
			
		| 
						 | 
					@ -5,6 +5,7 @@ from cpython cimport *
 | 
				
			||||||
from libc.stdlib cimport *
 | 
					from libc.stdlib cimport *
 | 
				
			||||||
from libc.string cimport *
 | 
					from libc.string cimport *
 | 
				
			||||||
from libc.limits cimport *
 | 
					from libc.limits cimport *
 | 
				
			||||||
 | 
					from libc.stdint cimport int8_t
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from msgpack.exceptions import PackValueError
 | 
					from msgpack.exceptions import PackValueError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,6 +28,7 @@ cdef extern from "pack.h":
 | 
				
			||||||
    int msgpack_pack_map(msgpack_packer* pk, size_t l)
 | 
					    int msgpack_pack_map(msgpack_packer* pk, size_t l)
 | 
				
			||||||
    int msgpack_pack_raw(msgpack_packer* pk, size_t l)
 | 
					    int msgpack_pack_raw(msgpack_packer* pk, size_t l)
 | 
				
			||||||
    int msgpack_pack_raw_body(msgpack_packer* pk, char* body, size_t l)
 | 
					    int msgpack_pack_raw_body(msgpack_packer* pk, char* body, size_t l)
 | 
				
			||||||
 | 
					    int msgpack_pack_ext(msgpack_packer* pk, int8_t typecode, size_t l)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
cdef int DEFAULT_RECURSE_LIMIT=511
 | 
					cdef int DEFAULT_RECURSE_LIMIT=511
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -193,6 +195,10 @@ cdef class Packer(object):
 | 
				
			||||||
            self.pk.length = 0
 | 
					            self.pk.length = 0
 | 
				
			||||||
            return buf
 | 
					            return buf
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def pack_extended_type(self, typecode, data):
 | 
				
			||||||
 | 
					        msgpack_pack_ext(&self.pk, typecode, len(data))
 | 
				
			||||||
 | 
					        msgpack_pack_raw_body(&self.pk, data, len(data))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def pack_array_header(self, size_t size):
 | 
					    def pack_array_header(self, size_t size):
 | 
				
			||||||
        cdef int ret = msgpack_pack_array(&self.pk, size)
 | 
					        cdef int ret = msgpack_pack_array(&self.pk, size)
 | 
				
			||||||
        if ret == -1:
 | 
					        if ret == -1:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,6 +70,8 @@ static inline int msgpack_pack_map(msgpack_packer* pk, unsigned int n);
 | 
				
			||||||
static inline int msgpack_pack_raw(msgpack_packer* pk, size_t l);
 | 
					static inline int msgpack_pack_raw(msgpack_packer* pk, size_t l);
 | 
				
			||||||
static inline int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
 | 
					static inline int msgpack_pack_raw_body(msgpack_packer* pk, const void* b, size_t l);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline int msgpack_pack_ext(msgpack_packer* pk, int8_t typecode, size_t l);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_t l)
 | 
					static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_t l)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    char* buf = pk->buf;
 | 
					    char* buf = pk->buf;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -683,6 +683,66 @@ static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t
 | 
				
			||||||
    msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
 | 
					    msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					 * Ext
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static inline int msgpack_pack_ext(msgpack_packer* x, int8_t typecode, size_t l)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    if (l == 1) {
 | 
				
			||||||
 | 
					        unsigned char buf[2];
 | 
				
			||||||
 | 
					        buf[0] = 0xd4;
 | 
				
			||||||
 | 
					        buf[1] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 2);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(l == 2) {
 | 
				
			||||||
 | 
					        unsigned char buf[2];
 | 
				
			||||||
 | 
					        buf[0] = 0xd5;
 | 
				
			||||||
 | 
					        buf[1] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 2);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(l == 4) {
 | 
				
			||||||
 | 
					        unsigned char buf[2];
 | 
				
			||||||
 | 
					        buf[0] = 0xd6;
 | 
				
			||||||
 | 
					        buf[1] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 2);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(l == 8) {
 | 
				
			||||||
 | 
					        unsigned char buf[2];
 | 
				
			||||||
 | 
					        buf[0] = 0xd7;
 | 
				
			||||||
 | 
					        buf[1] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 2);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(l == 16) {
 | 
				
			||||||
 | 
					        unsigned char buf[2];
 | 
				
			||||||
 | 
					        buf[0] = 0xd8;
 | 
				
			||||||
 | 
					        buf[1] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 2);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else if(l < 256) {
 | 
				
			||||||
 | 
					        unsigned char buf[3];
 | 
				
			||||||
 | 
					        buf[0] = 0xc7;
 | 
				
			||||||
 | 
					        buf[1] = l;
 | 
				
			||||||
 | 
					        buf[2] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 3);
 | 
				
			||||||
 | 
					    } else if(l < 65536) {
 | 
				
			||||||
 | 
					        unsigned char buf[4];
 | 
				
			||||||
 | 
					        buf[0] = 0xc8;
 | 
				
			||||||
 | 
					        _msgpack_store16(&buf[1], (uint16_t)l);
 | 
				
			||||||
 | 
					        buf[3] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 4);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        unsigned char buf[6];
 | 
				
			||||||
 | 
					        buf[0] = 0xc9;
 | 
				
			||||||
 | 
					        _msgpack_store32(&buf[1], (uint32_t)l);
 | 
				
			||||||
 | 
					        buf[5] = (unsigned char)typecode;
 | 
				
			||||||
 | 
					        msgpack_pack_append_buffer(x, buf, 6);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#undef msgpack_pack_append_buffer
 | 
					#undef msgpack_pack_append_buffer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#undef TAKE8_8
 | 
					#undef TAKE8_8
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,7 +6,7 @@ def test_pack_extended_type():
 | 
				
			||||||
    def p(s):
 | 
					    def p(s):
 | 
				
			||||||
        packer = msgpack.Packer()
 | 
					        packer = msgpack.Packer()
 | 
				
			||||||
        packer.pack_extended_type(0x42, s)
 | 
					        packer.pack_extended_type(0x42, s)
 | 
				
			||||||
        return packer._buffer.getvalue()
 | 
					        return packer.bytes()
 | 
				
			||||||
    assert p('A')        == '\xd4\x42A'          # fixext 1
 | 
					    assert p('A')        == '\xd4\x42A'          # fixext 1
 | 
				
			||||||
    assert p('AB')       == '\xd5\x42AB'         # fixext 2
 | 
					    assert p('AB')       == '\xd5\x42AB'         # fixext 2
 | 
				
			||||||
    assert p('ABCD')     == '\xd6\x42ABCD'       # fixext 4
 | 
					    assert p('ABCD')     == '\xd6\x42ABCD'       # fixext 4
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue