mirror of
				https://github.com/msgpack/msgpack-python.git
				synced 2025-10-31 09:30:53 +00:00 
			
		
		
		
	Merge pull request #54 from msgpack/remove-macros
Remove macros for readability.
This commit is contained in:
		
						commit
						e8f6d2a030
					
				
					 5 changed files with 105 additions and 227 deletions
				
			
		|  | @ -30,26 +30,26 @@ cdef extern from "unpack.h": | |||
|         char *encoding | ||||
|         char *unicode_errors | ||||
| 
 | ||||
|     ctypedef struct template_context: | ||||
|     ctypedef struct unpack_context: | ||||
|         msgpack_user user | ||||
|         PyObject* obj | ||||
|         size_t count | ||||
|         unsigned int ct | ||||
|         PyObject* key | ||||
| 
 | ||||
|     ctypedef int (*execute_fn)(template_context* ctx, const_char_ptr data, | ||||
|     ctypedef int (*execute_fn)(unpack_context* ctx, const_char_ptr data, | ||||
|                                size_t len, size_t* off) except? -1 | ||||
|     execute_fn template_construct | ||||
|     execute_fn template_skip | ||||
|     execute_fn unpack_construct | ||||
|     execute_fn unpack_skip | ||||
|     execute_fn read_array_header | ||||
|     execute_fn read_map_header | ||||
|     void template_init(template_context* ctx) | ||||
|     object template_data(template_context* ctx) | ||||
|     void unpack_init(unpack_context* ctx) | ||||
|     object unpack_data(unpack_context* ctx) | ||||
| 
 | ||||
| cdef inline init_ctx(template_context *ctx, | ||||
| cdef inline init_ctx(unpack_context *ctx, | ||||
|                      object object_hook, object object_pairs_hook, object list_hook, | ||||
|                      bint use_list, char* encoding, char* unicode_errors): | ||||
|     template_init(ctx) | ||||
|     unpack_init(ctx) | ||||
|     ctx.user.use_list = use_list | ||||
|     ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL | ||||
| 
 | ||||
|  | @ -88,7 +88,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None, | |||
| 
 | ||||
|     See :class:`Unpacker` for options. | ||||
|     """ | ||||
|     cdef template_context ctx | ||||
|     cdef unpack_context ctx | ||||
|     cdef size_t off = 0 | ||||
|     cdef int ret | ||||
| 
 | ||||
|  | @ -110,9 +110,9 @@ def unpackb(object packed, object object_hook=None, object list_hook=None, | |||
|         cerr = PyBytes_AsString(unicode_errors) | ||||
| 
 | ||||
|     init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr) | ||||
|     ret = template_construct(&ctx, buf, buf_len, &off) | ||||
|     ret = unpack_construct(&ctx, buf, buf_len, &off) | ||||
|     if ret == 1: | ||||
|         obj = template_data(&ctx) | ||||
|         obj = unpack_data(&ctx) | ||||
|         if off < buf_len: | ||||
|             raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off)) | ||||
|         return obj | ||||
|  | @ -194,7 +194,7 @@ cdef class Unpacker(object): | |||
|             for o in unpacker: | ||||
|                 process(o) | ||||
|     """ | ||||
|     cdef template_context ctx | ||||
|     cdef unpack_context ctx | ||||
|     cdef char* buf | ||||
|     cdef size_t buf_size, buf_head, buf_tail | ||||
|     cdef object file_like | ||||
|  | @ -324,8 +324,8 @@ cdef class Unpacker(object): | |||
|                 write_bytes(PyBytes_FromStringAndSize(self.buf + prev_head, self.buf_head - prev_head)) | ||||
| 
 | ||||
|             if ret == 1: | ||||
|                 obj = template_data(&self.ctx) | ||||
|                 template_init(&self.ctx) | ||||
|                 obj = unpack_data(&self.ctx) | ||||
|                 unpack_init(&self.ctx) | ||||
|                 return obj | ||||
|             elif ret == 0: | ||||
|                 if self.file_like is not None: | ||||
|  | @ -357,7 +357,7 @@ cdef class Unpacker(object): | |||
| 
 | ||||
|         Raises `OutOfData` when there are no more bytes to unpack. | ||||
|         """ | ||||
|         return self._unpack(template_construct, write_bytes) | ||||
|         return self._unpack(unpack_construct, write_bytes) | ||||
| 
 | ||||
|     def skip(self, object write_bytes=None): | ||||
|         """ | ||||
|  | @ -368,7 +368,7 @@ cdef class Unpacker(object): | |||
| 
 | ||||
|         Raises `OutOfData` when there are no more bytes to unpack. | ||||
|         """ | ||||
|         return self._unpack(template_skip, write_bytes) | ||||
|         return self._unpack(unpack_skip, write_bytes) | ||||
| 
 | ||||
|     def read_array_header(self, object write_bytes=None): | ||||
|         """assuming the next object is an array, return its size n, such that | ||||
|  | @ -390,7 +390,7 @@ cdef class Unpacker(object): | |||
|         return self | ||||
| 
 | ||||
|     def __next__(self): | ||||
|         return self._unpack(template_construct, None, 1) | ||||
|         return self._unpack(unpack_construct, None, 1) | ||||
| 
 | ||||
|     # for debug. | ||||
|     #def _buf(self): | ||||
|  |  | |||
|  | @ -45,7 +45,7 @@ static inline int msgpack_pack_long_long(msgpack_packer* pk, long long d); | |||
| static inline int msgpack_pack_unsigned_short(msgpack_packer* pk, unsigned short d); | ||||
| static inline int msgpack_pack_unsigned_int(msgpack_packer* pk, unsigned int d); | ||||
| static inline int msgpack_pack_unsigned_long(msgpack_packer* pk, unsigned long d); | ||||
| static inline int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d); | ||||
| //static inline int msgpack_pack_unsigned_long_long(msgpack_packer* pk, unsigned long long d);
 | ||||
| 
 | ||||
| static inline int msgpack_pack_uint8(msgpack_packer* pk, uint8_t d); | ||||
| static inline int msgpack_pack_uint16(msgpack_packer* pk, uint16_t d); | ||||
|  | @ -90,14 +90,6 @@ static inline int msgpack_pack_write(msgpack_packer* pk, const char *data, size_ | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| #define msgpack_pack_inline_func(name) \ | ||||
| 	static inline int msgpack_pack ## name | ||||
| 
 | ||||
| #define msgpack_pack_inline_func_cint(name) \ | ||||
| 	static inline int msgpack_pack ## name | ||||
| 
 | ||||
| #define msgpack_pack_user msgpack_packer* | ||||
| 
 | ||||
| #define msgpack_pack_append_buffer(user, buf, len) \ | ||||
|         return msgpack_pack_write(user, (const char*)buf, len) | ||||
| 
 | ||||
|  |  | |||
|  | @ -28,14 +28,6 @@ | |||
| #define TAKE8_64(d) ((uint8_t*)&d)[7] | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_pack_inline_func | ||||
| #error msgpack_pack_inline_func template is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_pack_user | ||||
| #error msgpack_pack_user type is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_pack_append_buffer | ||||
| #error msgpack_pack_append_buffer callback is not defined | ||||
| #endif | ||||
|  | @ -272,110 +264,50 @@ do { \ | |||
| } while(0) | ||||
| 
 | ||||
| 
 | ||||
| #ifdef msgpack_pack_inline_func_fixint | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_uint8)(msgpack_pack_user x, uint8_t d) | ||||
| { | ||||
| 	unsigned char buf[2] = {0xcc, TAKE8_8(d)}; | ||||
| 	msgpack_pack_append_buffer(x, buf, 2); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_uint16)(msgpack_pack_user x, uint16_t d) | ||||
| { | ||||
| 	unsigned char buf[3]; | ||||
| 	buf[0] = 0xcd; _msgpack_store16(&buf[1], d); | ||||
| 	msgpack_pack_append_buffer(x, buf, 3); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_uint32)(msgpack_pack_user x, uint32_t d) | ||||
| { | ||||
| 	unsigned char buf[5]; | ||||
| 	buf[0] = 0xce; _msgpack_store32(&buf[1], d); | ||||
| 	msgpack_pack_append_buffer(x, buf, 5); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_uint64)(msgpack_pack_user x, uint64_t d) | ||||
| { | ||||
| 	unsigned char buf[9]; | ||||
| 	buf[0] = 0xcf; _msgpack_store64(&buf[1], d); | ||||
| 	msgpack_pack_append_buffer(x, buf, 9); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_int8)(msgpack_pack_user x, int8_t d) | ||||
| { | ||||
| 	unsigned char buf[2] = {0xd0, TAKE8_8(d)}; | ||||
| 	msgpack_pack_append_buffer(x, buf, 2); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_int16)(msgpack_pack_user x, int16_t d) | ||||
| { | ||||
| 	unsigned char buf[3]; | ||||
| 	buf[0] = 0xd1; _msgpack_store16(&buf[1], d); | ||||
| 	msgpack_pack_append_buffer(x, buf, 3); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_int32)(msgpack_pack_user x, int32_t d) | ||||
| { | ||||
| 	unsigned char buf[5]; | ||||
| 	buf[0] = 0xd2; _msgpack_store32(&buf[1], d); | ||||
| 	msgpack_pack_append_buffer(x, buf, 5); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_fixint(_int64)(msgpack_pack_user x, int64_t d) | ||||
| { | ||||
| 	unsigned char buf[9]; | ||||
| 	buf[0] = 0xd3; _msgpack_store64(&buf[1], d); | ||||
| 	msgpack_pack_append_buffer(x, buf, 9); | ||||
| } | ||||
| 
 | ||||
| #undef msgpack_pack_inline_func_fixint | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| msgpack_pack_inline_func(_uint8)(msgpack_pack_user x, uint8_t d) | ||||
| static inline int msgpack_pack_uint8(msgpack_packer* x, uint8_t d) | ||||
| { | ||||
| 	msgpack_pack_real_uint8(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_uint16)(msgpack_pack_user x, uint16_t d) | ||||
| static inline int msgpack_pack_uint16(msgpack_packer* x, uint16_t d) | ||||
| { | ||||
| 	msgpack_pack_real_uint16(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_uint32)(msgpack_pack_user x, uint32_t d) | ||||
| static inline int msgpack_pack_uint32(msgpack_packer* x, uint32_t d) | ||||
| { | ||||
| 	msgpack_pack_real_uint32(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_uint64)(msgpack_pack_user x, uint64_t d) | ||||
| static inline int msgpack_pack_uint64(msgpack_packer* x, uint64_t d) | ||||
| { | ||||
| 	msgpack_pack_real_uint64(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_int8)(msgpack_pack_user x, int8_t d) | ||||
| static inline int msgpack_pack_int8(msgpack_packer* x, int8_t d) | ||||
| { | ||||
| 	msgpack_pack_real_int8(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_int16)(msgpack_pack_user x, int16_t d) | ||||
| static inline int msgpack_pack_int16(msgpack_packer* x, int16_t d) | ||||
| { | ||||
| 	msgpack_pack_real_int16(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_int32)(msgpack_pack_user x, int32_t d) | ||||
| static inline int msgpack_pack_int32(msgpack_packer* x, int32_t d) | ||||
| { | ||||
| 	msgpack_pack_real_int32(x, d); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_int64)(msgpack_pack_user x, int64_t d) | ||||
| static inline int msgpack_pack_int64(msgpack_packer* x, int64_t d) | ||||
| { | ||||
| 	msgpack_pack_real_int64(x, d); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| #ifdef msgpack_pack_inline_func_cint | ||||
| //#ifdef msgpack_pack_inline_func_cint
 | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_short)(msgpack_pack_user x, short d) | ||||
| static inline int msgpack_pack_short(msgpack_packer* x, short d) | ||||
| { | ||||
| #if defined(SIZEOF_SHORT) | ||||
| #if SIZEOF_SHORT == 2 | ||||
|  | @ -406,7 +338,7 @@ if(sizeof(short) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_int)(msgpack_pack_user x, int d) | ||||
| static inline int msgpack_pack_int(msgpack_packer* x, int d) | ||||
| { | ||||
| #if defined(SIZEOF_INT) | ||||
| #if SIZEOF_INT == 2 | ||||
|  | @ -437,7 +369,7 @@ if(sizeof(int) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_long)(msgpack_pack_user x, long d) | ||||
| static inline int msgpack_pack_long(msgpack_packer* x, long d) | ||||
| { | ||||
| #if defined(SIZEOF_LONG) | ||||
| #if SIZEOF_LONG == 2 | ||||
|  | @ -468,7 +400,7 @@ if(sizeof(long) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_long_long)(msgpack_pack_user x, long long d) | ||||
| static inline int msgpack_pack_long_long(msgpack_packer* x, long long d) | ||||
| { | ||||
| #if defined(SIZEOF_LONG_LONG) | ||||
| #if SIZEOF_LONG_LONG == 2 | ||||
|  | @ -499,7 +431,7 @@ if(sizeof(long long) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_unsigned_short)(msgpack_pack_user x, unsigned short d) | ||||
| static inline int msgpack_pack_unsigned_short(msgpack_packer* x, unsigned short d) | ||||
| { | ||||
| #if defined(SIZEOF_SHORT) | ||||
| #if SIZEOF_SHORT == 2 | ||||
|  | @ -530,7 +462,7 @@ if(sizeof(unsigned short) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_unsigned_int)(msgpack_pack_user x, unsigned int d) | ||||
| static inline int msgpack_pack_unsigned_int(msgpack_packer* x, unsigned int d) | ||||
| { | ||||
| #if defined(SIZEOF_INT) | ||||
| #if SIZEOF_INT == 2 | ||||
|  | @ -561,7 +493,7 @@ if(sizeof(unsigned int) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_unsigned_long)(msgpack_pack_user x, unsigned long d) | ||||
| static inline int msgpack_pack_unsigned_long(msgpack_packer* x, unsigned long d) | ||||
| { | ||||
| #if defined(SIZEOF_LONG) | ||||
| #if SIZEOF_LONG == 2 | ||||
|  | @ -592,7 +524,7 @@ if(sizeof(unsigned long) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func_cint(_unsigned_long_long)(msgpack_pack_user x, unsigned long long d) | ||||
| static inline int msgpack_pack_unsigned_long_long(msgpack_packer* x, unsigned long long d) | ||||
| { | ||||
| #if defined(SIZEOF_LONG_LONG) | ||||
| #if SIZEOF_LONG_LONG == 2 | ||||
|  | @ -623,8 +555,8 @@ if(sizeof(unsigned long long) == 2) { | |||
| #endif | ||||
| } | ||||
| 
 | ||||
| #undef msgpack_pack_inline_func_cint | ||||
| #endif | ||||
| //#undef msgpack_pack_inline_func_cint
 | ||||
| //#endif
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | @ -632,7 +564,7 @@ if(sizeof(unsigned long long) == 2) { | |||
|  * Float | ||||
|  */ | ||||
| 
 | ||||
| msgpack_pack_inline_func(_float)(msgpack_pack_user x, float d) | ||||
| static inline int msgpack_pack_float(msgpack_packer* x, float d) | ||||
| { | ||||
| 	union { float f; uint32_t i; } mem; | ||||
| 	mem.f = d; | ||||
|  | @ -641,7 +573,7 @@ msgpack_pack_inline_func(_float)(msgpack_pack_user x, float d) | |||
| 	msgpack_pack_append_buffer(x, buf, 5); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_double)(msgpack_pack_user x, double d) | ||||
| static inline int msgpack_pack_double(msgpack_packer* x, double d) | ||||
| { | ||||
| 	union { double f; uint64_t i; } mem; | ||||
| 	mem.f = d; | ||||
|  | @ -660,7 +592,7 @@ msgpack_pack_inline_func(_double)(msgpack_pack_user x, double d) | |||
|  * Nil | ||||
|  */ | ||||
| 
 | ||||
| msgpack_pack_inline_func(_nil)(msgpack_pack_user x) | ||||
| static inline int msgpack_pack_nil(msgpack_packer* x) | ||||
| { | ||||
| 	static const unsigned char d = 0xc0; | ||||
| 	msgpack_pack_append_buffer(x, &d, 1); | ||||
|  | @ -671,13 +603,13 @@ msgpack_pack_inline_func(_nil)(msgpack_pack_user x) | |||
|  * Boolean | ||||
|  */ | ||||
| 
 | ||||
| msgpack_pack_inline_func(_true)(msgpack_pack_user x) | ||||
| static inline int msgpack_pack_true(msgpack_packer* x) | ||||
| { | ||||
| 	static const unsigned char d = 0xc3; | ||||
| 	msgpack_pack_append_buffer(x, &d, 1); | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_false)(msgpack_pack_user x) | ||||
| static inline int msgpack_pack_false(msgpack_packer* x) | ||||
| { | ||||
| 	static const unsigned char d = 0xc2; | ||||
| 	msgpack_pack_append_buffer(x, &d, 1); | ||||
|  | @ -688,7 +620,7 @@ msgpack_pack_inline_func(_false)(msgpack_pack_user x) | |||
|  * Array | ||||
|  */ | ||||
| 
 | ||||
| msgpack_pack_inline_func(_array)(msgpack_pack_user x, unsigned int n) | ||||
| static inline int msgpack_pack_array(msgpack_packer* x, unsigned int n) | ||||
| { | ||||
| 	if(n < 16) { | ||||
| 		unsigned char d = 0x90 | n; | ||||
|  | @ -709,7 +641,7 @@ msgpack_pack_inline_func(_array)(msgpack_pack_user x, unsigned int n) | |||
|  * Map | ||||
|  */ | ||||
| 
 | ||||
| msgpack_pack_inline_func(_map)(msgpack_pack_user x, unsigned int n) | ||||
| static inline int msgpack_pack_map(msgpack_packer* x, unsigned int n) | ||||
| { | ||||
| 	if(n < 16) { | ||||
| 		unsigned char d = 0x80 | n; | ||||
|  | @ -730,7 +662,7 @@ msgpack_pack_inline_func(_map)(msgpack_pack_user x, unsigned int n) | |||
|  * Raw | ||||
|  */ | ||||
| 
 | ||||
| msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l) | ||||
| static inline int msgpack_pack_raw(msgpack_packer* x, size_t l) | ||||
| { | ||||
| 	if(l < 32) { | ||||
| 		unsigned char d = 0xa0 | (uint8_t)l; | ||||
|  | @ -746,13 +678,11 @@ msgpack_pack_inline_func(_raw)(msgpack_pack_user x, size_t l) | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| msgpack_pack_inline_func(_raw_body)(msgpack_pack_user x, const void* b, size_t l) | ||||
| static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t l) | ||||
| { | ||||
| 	msgpack_pack_append_buffer(x, (const unsigned char*)b, l); | ||||
| } | ||||
| 
 | ||||
| #undef msgpack_pack_inline_func | ||||
| #undef msgpack_pack_user | ||||
| #undef msgpack_pack_append_buffer | ||||
| 
 | ||||
| #undef TAKE8_8 | ||||
|  |  | |||
|  | @ -28,31 +28,17 @@ typedef struct unpack_user { | |||
|     const char *unicode_errors; | ||||
| } unpack_user; | ||||
| 
 | ||||
| typedef PyObject* msgpack_unpack_object; | ||||
| struct unpack_context; | ||||
| typedef struct unpack_context unpack_context; | ||||
| typedef int (*execute_fn)(unpack_context *ctx, const char* data, size_t len, size_t* off); | ||||
| 
 | ||||
| #define msgpack_unpack_struct(name) \ | ||||
| 	struct template ## name | ||||
| 
 | ||||
| #define msgpack_unpack_func(ret, name) \ | ||||
| 	static inline ret template ## name | ||||
| 
 | ||||
| #define msgpack_unpack_callback(name) \ | ||||
| 	template_callback ## name | ||||
| 
 | ||||
| #define msgpack_unpack_object PyObject* | ||||
| 
 | ||||
| #define msgpack_unpack_user unpack_user | ||||
| 
 | ||||
| typedef int (*execute_fn)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off); | ||||
| 
 | ||||
| struct template_context; | ||||
| typedef struct template_context template_context; | ||||
| 
 | ||||
| static inline msgpack_unpack_object template_callback_root(unpack_user* u) | ||||
| static inline msgpack_unpack_object unpack_callback_root(unpack_user* u) | ||||
| { | ||||
|     return NULL; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_uint16(unpack_user* u, uint16_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p = PyInt_FromLong((long)d); | ||||
|     if (!p) | ||||
|  | @ -60,13 +46,13 @@ static inline int template_callback_uint16(unpack_user* u, uint16_t d, msgpack_u | |||
|     *o = p; | ||||
|     return 0; | ||||
| } | ||||
| static inline int template_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_uint8(unpack_user* u, uint8_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     return template_callback_uint16(u, d, o); | ||||
|     return unpack_callback_uint16(u, d, o); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static inline int template_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_uint32(unpack_user* u, uint32_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p; | ||||
|     if (d > LONG_MAX) { | ||||
|  | @ -80,7 +66,7 @@ static inline int template_callback_uint32(unpack_user* u, uint32_t d, msgpack_u | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_uint64(unpack_user* u, uint64_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p = PyLong_FromUnsignedLongLong(d); | ||||
|     if (!p) | ||||
|  | @ -89,7 +75,7 @@ static inline int template_callback_uint64(unpack_user* u, uint64_t d, msgpack_u | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_int32(unpack_user* u, int32_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p = PyInt_FromLong(d); | ||||
|     if (!p) | ||||
|  | @ -98,17 +84,17 @@ static inline int template_callback_int32(unpack_user* u, int32_t d, msgpack_unp | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_int16(unpack_user* u, int16_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     return template_callback_int32(u, d, o); | ||||
|     return unpack_callback_int32(u, d, o); | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_int8(unpack_user* u, int8_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     return template_callback_int32(u, d, o); | ||||
|     return unpack_callback_int32(u, d, o); | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_int64(unpack_user* u, int64_t d, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p = PyLong_FromLongLong(d); | ||||
|     if (!p) | ||||
|  | @ -117,7 +103,7 @@ static inline int template_callback_int64(unpack_user* u, int64_t d, msgpack_unp | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_double(unpack_user* u, double d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_double(unpack_user* u, double d, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p = PyFloat_FromDouble(d); | ||||
|     if (!p) | ||||
|  | @ -126,21 +112,21 @@ static inline int template_callback_double(unpack_user* u, double d, msgpack_unp | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_float(unpack_user* u, float d, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_float(unpack_user* u, float d, msgpack_unpack_object* o) | ||||
| { | ||||
|     return template_callback_double(u, d, o); | ||||
|     return unpack_callback_double(u, d, o); | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_nil(unpack_user* u, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_nil(unpack_user* u, msgpack_unpack_object* o) | ||||
| { Py_INCREF(Py_None); *o = Py_None; return 0; } | ||||
| 
 | ||||
| static inline int template_callback_true(unpack_user* u, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_true(unpack_user* u, msgpack_unpack_object* o) | ||||
| { Py_INCREF(Py_True); *o = Py_True; return 0; } | ||||
| 
 | ||||
| static inline int template_callback_false(unpack_user* u, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_false(unpack_user* u, msgpack_unpack_object* o) | ||||
| { Py_INCREF(Py_False); *o = Py_False; return 0; } | ||||
| 
 | ||||
| static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p = u->use_list ? PyList_New(n) : PyTuple_New(n); | ||||
| 
 | ||||
|  | @ -150,7 +136,7 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o) | ||||
| static inline int unpack_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o) | ||||
| { | ||||
|     if (u->use_list) | ||||
|         PyList_SET_ITEM(*c, current, o); | ||||
|  | @ -159,7 +145,7 @@ static inline int template_callback_array_item(unpack_user* u, unsigned int curr | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_array_end(unpack_user* u, msgpack_unpack_object* c) | ||||
| static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c) | ||||
| { | ||||
|     if (u->list_hook) { | ||||
|         PyObject *new_c = PyEval_CallFunction(u->list_hook, "(O)", *c); | ||||
|  | @ -171,7 +157,7 @@ static inline int template_callback_array_end(unpack_user* u, msgpack_unpack_obj | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *p; | ||||
|     if (u->has_pairs_hook) { | ||||
|  | @ -186,7 +172,7 @@ static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_ | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_map_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v) | ||||
| static inline int unpack_callback_map_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v) | ||||
| { | ||||
|     if (u->has_pairs_hook) { | ||||
|         msgpack_unpack_object item = PyTuple_Pack(2, k, v); | ||||
|  | @ -205,7 +191,7 @@ static inline int template_callback_map_item(unpack_user* u, unsigned int curren | |||
|     return -1; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_map_end(unpack_user* u, msgpack_unpack_object* c) | ||||
| static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c) | ||||
| { | ||||
|     if (u->object_hook) { | ||||
|         PyObject *new_c = PyEval_CallFunction(u->object_hook, "(O)", *c); | ||||
|  | @ -218,7 +204,7 @@ static inline int template_callback_map_end(unpack_user* u, msgpack_unpack_objec | |||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| static inline int template_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o) | ||||
| static inline int unpack_callback_raw(unpack_user* u, const char* b, const char* p, unsigned int l, msgpack_unpack_object* o) | ||||
| { | ||||
|     PyObject *py; | ||||
|     if(u->encoding) { | ||||
|  |  | |||
|  | @ -16,59 +16,35 @@ | |||
|  *    limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| #ifndef msgpack_unpack_func | ||||
| #error msgpack_unpack_func template is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_unpack_callback | ||||
| #error msgpack_unpack_callback template is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_unpack_struct | ||||
| #error msgpack_unpack_struct template is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_unpack_struct_decl | ||||
| #define msgpack_unpack_struct_decl(name) msgpack_unpack_struct(name) | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_unpack_object | ||||
| #error msgpack_unpack_object type is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef msgpack_unpack_user | ||||
| #error msgpack_unpack_user type is not defined | ||||
| #endif | ||||
| 
 | ||||
| #ifndef USE_CASE_RANGE | ||||
| #if !defined(_MSC_VER) | ||||
| #define USE_CASE_RANGE | ||||
| #endif | ||||
| #endif | ||||
| 
 | ||||
| msgpack_unpack_struct_decl(_stack) { | ||||
| 	msgpack_unpack_object obj; | ||||
| typedef struct unpack_stack { | ||||
| 	PyObject* obj; | ||||
| 	size_t size; | ||||
| 	size_t count; | ||||
| 	unsigned int ct; | ||||
| 	msgpack_unpack_object map_key; | ||||
| }; | ||||
| 	PyObject* map_key; | ||||
| } unpack_stack; | ||||
| 
 | ||||
| msgpack_unpack_struct_decl(_context) { | ||||
| 	msgpack_unpack_user user; | ||||
| struct unpack_context { | ||||
| 	unpack_user user; | ||||
| 	unsigned int cs; | ||||
| 	unsigned int trail; | ||||
| 	unsigned int top; | ||||
| 	/*
 | ||||
| 	msgpack_unpack_struct(_stack)* stack; | ||||
| 	unpack_stack* stack; | ||||
| 	unsigned int stack_size; | ||||
| 	msgpack_unpack_struct(_stack) embed_stack[MSGPACK_EMBED_STACK_SIZE]; | ||||
| 	unpack_stack embed_stack[MSGPACK_EMBED_STACK_SIZE]; | ||||
| 	*/ | ||||
| 	msgpack_unpack_struct(_stack) stack[MSGPACK_EMBED_STACK_SIZE]; | ||||
| 	unpack_stack stack[MSGPACK_EMBED_STACK_SIZE]; | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx) | ||||
| static inline void unpack_init(unpack_context* ctx) | ||||
| { | ||||
| 	ctx->cs = CS_HEADER; | ||||
| 	ctx->trail = 0; | ||||
|  | @ -77,11 +53,11 @@ msgpack_unpack_func(void, _init)(msgpack_unpack_struct(_context)* ctx) | |||
| 	ctx->stack = ctx->embed_stack; | ||||
| 	ctx->stack_size = MSGPACK_EMBED_STACK_SIZE; | ||||
| 	*/ | ||||
| 	ctx->stack[0].obj = msgpack_unpack_callback(_root)(&ctx->user); | ||||
| 	ctx->stack[0].obj = unpack_callback_root(&ctx->user); | ||||
| } | ||||
| 
 | ||||
| /*
 | ||||
| msgpack_unpack_func(void, _destroy)(msgpack_unpack_struct(_context)* ctx) | ||||
| static inline void unpack_destroy(unpack_context* ctx) | ||||
| { | ||||
| 	if(ctx->stack_size != MSGPACK_EMBED_STACK_SIZE) { | ||||
| 		free(ctx->stack); | ||||
|  | @ -89,14 +65,14 @@ msgpack_unpack_func(void, _destroy)(msgpack_unpack_struct(_context)* ctx) | |||
| } | ||||
| */ | ||||
| 
 | ||||
| msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context)* ctx) | ||||
| static inline PyObject* unpack_data(unpack_context* ctx) | ||||
| { | ||||
| 	return (ctx)->stack[0].obj; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| template <bool construct> | ||||
| msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off) | ||||
| static inline int unpack_execute(unpack_context* ctx, const char* data, size_t len, size_t* off) | ||||
| { | ||||
| 	assert(len >= *off); | ||||
| 
 | ||||
|  | @ -107,19 +83,19 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c | |||
| 	unsigned int trail = ctx->trail; | ||||
| 	unsigned int cs = ctx->cs; | ||||
| 	unsigned int top = ctx->top; | ||||
| 	msgpack_unpack_struct(_stack)* stack = ctx->stack; | ||||
| 	unpack_stack* stack = ctx->stack; | ||||
| 	/*
 | ||||
| 	unsigned int stack_size = ctx->stack_size; | ||||
| 	*/ | ||||
| 	msgpack_unpack_user* user = &ctx->user; | ||||
| 	unpack_user* user = &ctx->user; | ||||
| 
 | ||||
| 	msgpack_unpack_object obj; | ||||
| 	msgpack_unpack_struct(_stack)* c = NULL; | ||||
| 	PyObject* obj; | ||||
| 	unpack_stack* c = NULL; | ||||
| 
 | ||||
| 	int ret; | ||||
| 
 | ||||
| #define construct_cb(name) \ | ||||
|     construct && msgpack_unpack_callback(name) | ||||
|     construct && unpack_callback ## name | ||||
| 
 | ||||
| #define push_simple_value(func) \ | ||||
| 	if(construct_cb(func)(user, &obj) < 0) { goto _failed; } \ | ||||
|  | @ -157,16 +133,16 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c | |||
| 	/* FIXME \
 | ||||
| 	if(top >= stack_size) { \ | ||||
| 		if(stack_size == MSGPACK_EMBED_STACK_SIZE) { \ | ||||
| 			size_t csize = sizeof(msgpack_unpack_struct(_stack)) * MSGPACK_EMBED_STACK_SIZE; \ | ||||
| 			size_t csize = sizeof(unpack_stack) * MSGPACK_EMBED_STACK_SIZE; \ | ||||
| 			size_t nsize = csize * 2; \ | ||||
| 			msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)malloc(nsize); \ | ||||
| 			unpack_stack* tmp = (unpack_stack*)malloc(nsize); \ | ||||
| 			if(tmp == NULL) { goto _failed; } \ | ||||
| 			memcpy(tmp, ctx->stack, csize); \ | ||||
| 			ctx->stack = stack = tmp; \ | ||||
| 			ctx->stack_size = stack_size = MSGPACK_EMBED_STACK_SIZE * 2; \ | ||||
| 		} else { \ | ||||
| 			size_t nsize = sizeof(msgpack_unpack_struct(_stack)) * ctx->stack_size * 2; \ | ||||
| 			msgpack_unpack_struct(_stack)* tmp = (msgpack_unpack_struct(_stack)*)realloc(ctx->stack, nsize); \ | ||||
| 			size_t nsize = sizeof(unpack_stack) * ctx->stack_size * 2; \ | ||||
| 			unpack_stack* tmp = (unpack_stack*)realloc(ctx->stack, nsize); \ | ||||
| 			if(tmp == NULL) { goto _failed; } \ | ||||
| 			ctx->stack = stack = tmp; \ | ||||
| 			ctx->stack_size = stack_size = stack_size * 2; \ | ||||
|  | @ -382,7 +358,7 @@ _header_again: | |||
| 
 | ||||
| _finish: | ||||
| 	if (!construct) | ||||
| 		msgpack_unpack_callback(_nil)(user, &obj); | ||||
| 		unpack_callback_nil(user, &obj); | ||||
| 	stack[0].obj = obj; | ||||
| 	++p; | ||||
| 	ret = 1; | ||||
|  | @ -420,7 +396,7 @@ _end: | |||
| #undef start_container | ||||
| 
 | ||||
| template <unsigned int fixed_offset, unsigned int var_offset> | ||||
| msgpack_unpack_func(int, _container_header)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off) | ||||
| static inline int unpack_container_header(unpack_context* ctx, const char* data, size_t len, size_t* off) | ||||
| { | ||||
| 	assert(len >= *off); | ||||
| 	uint32_t size; | ||||
|  | @ -467,7 +443,7 @@ msgpack_unpack_func(int, _container_header)(msgpack_unpack_struct(_context)* ctx | |||
| 		PyErr_SetString(PyExc_ValueError, "Unexpected type header on stream"); | ||||
| 		return -1; | ||||
|     } | ||||
| 	msgpack_unpack_callback(_uint32)(&ctx->user, size, &ctx->stack[0].obj); | ||||
| 	unpack_callback_uint32(&ctx->user, size, &ctx->stack[0].obj); | ||||
| 	return 1; | ||||
| } | ||||
| 
 | ||||
|  | @ -476,16 +452,10 @@ msgpack_unpack_func(int, _container_header)(msgpack_unpack_struct(_context)* ctx | |||
| #undef SWITCH_RANGE_DEFAULT | ||||
| #undef SWITCH_RANGE_END | ||||
| 
 | ||||
| static const execute_fn template_construct = &template_execute<true>; | ||||
| static const execute_fn template_skip = &template_execute<false>; | ||||
| static const execute_fn read_array_header = &template_container_header<0x90, 0xdc>; | ||||
| static const execute_fn read_map_header = &template_container_header<0x80, 0xde>; | ||||
| 
 | ||||
| #undef msgpack_unpack_func | ||||
| #undef msgpack_unpack_callback | ||||
| #undef msgpack_unpack_struct | ||||
| #undef msgpack_unpack_object | ||||
| #undef msgpack_unpack_user | ||||
| static const execute_fn unpack_construct = &unpack_execute<true>; | ||||
| static const execute_fn unpack_skip = &unpack_execute<false>; | ||||
| static const execute_fn read_array_header = &unpack_container_header<0x90, 0xdc>; | ||||
| static const execute_fn read_map_header = &unpack_container_header<0x80, 0xde>; | ||||
| 
 | ||||
| #undef NEXT_CS | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 INADA Naoki
						INADA Naoki