mirror of
				https://github.com/msgpack/msgpack-python.git
				synced 2025-10-31 01:20:53 +00:00 
			
		
		
		
	slightly change to API
This commit is contained in:
		
							parent
							
								
									5529dfe596
								
							
						
					
					
						commit
						522c4bfc79
					
				
					 2 changed files with 14 additions and 19 deletions
				
			
		|  | @ -398,7 +398,7 @@ class Unpacker(object): | ||||||
|         if typ >= EXTENDED_TYPE: |         if typ >= EXTENDED_TYPE: | ||||||
|             typ -= EXTENDED_TYPE |             typ -= EXTENDED_TYPE | ||||||
|             data = self._fb_read(n, write_bytes) |             data = self._fb_read(n, write_bytes) | ||||||
|             return self.handle_extended_type(typ, data) |             return self.read_extended_type(typ, data) | ||||||
|         assert typ == TYPE_IMMEDIATE |         assert typ == TYPE_IMMEDIATE | ||||||
|         return obj |         return obj | ||||||
| 
 | 
 | ||||||
|  | @ -420,7 +420,7 @@ class Unpacker(object): | ||||||
|         self._fb_consume() |         self._fb_consume() | ||||||
|         return ret |         return ret | ||||||
| 
 | 
 | ||||||
|     def handle_extended_type(self, typecode, data): |     def read_extended_type(self, typecode, data): | ||||||
|         raise NotImplementedError("Cannot decode extended type with typecode=%d" % typecode) |         raise NotImplementedError("Cannot decode extended type with typecode=%d" % typecode) | ||||||
| 
 | 
 | ||||||
|     def read_array_header(self, write_bytes=None): |     def read_array_header(self, write_bytes=None): | ||||||
|  | @ -533,19 +533,19 @@ class Packer(object): | ||||||
|         if isinstance(obj, dict): |         if isinstance(obj, dict): | ||||||
|             return self._fb_pack_map_pairs(len(obj), dict_iteritems(obj), |             return self._fb_pack_map_pairs(len(obj), dict_iteritems(obj), | ||||||
|                                            nest_limit - 1) |                                            nest_limit - 1) | ||||||
|         if self.pack_extended_type(obj): |         if self.handle_unknown_type(obj): | ||||||
|             # it means that obj was succesfully handled by |             # it means that obj was succesfully packed, so we are done | ||||||
|             # handle_extended_type, so we are done |  | ||||||
|             return |             return | ||||||
|         if self._default is not None: |         if self._default is not None: | ||||||
|             return self._pack(self._default(obj), nest_limit - 1) |             return self._pack(self._default(obj), nest_limit - 1) | ||||||
|         raise TypeError("Cannot serialize %r" % obj) |         raise TypeError("Cannot serialize %r" % obj) | ||||||
| 
 | 
 | ||||||
|     def pack_extended_type(self, obj): |     def handle_unknown_type(self, obj): | ||||||
|         res = self.handle_extended_type(obj) |         # by default we don't support any extended type. This can be | ||||||
|         if res is None: |         # overridden by subclasses | ||||||
|             return False |         return None | ||||||
|         fmt, typecode, data = res | 
 | ||||||
|  |     def pack_extended_type(self, fmt, typecode, data): | ||||||
|         # for now we support only this. We should add support for the other |         # for now we support only this. We should add support for the other | ||||||
|         # fixext/ext formats |         # fixext/ext formats | ||||||
|         assert fmt == "ext 32" |         assert fmt == "ext 32" | ||||||
|  | @ -553,12 +553,6 @@ class Packer(object): | ||||||
|         N = len(data) |         N = len(data) | ||||||
|         self._buffer.write(struct.pack('>BIB', 0xc9, N, typecode)) |         self._buffer.write(struct.pack('>BIB', 0xc9, N, typecode)) | ||||||
|         self._buffer.write(data) |         self._buffer.write(data) | ||||||
|         return True |  | ||||||
| 
 |  | ||||||
|     def handle_extended_type(self, obj): |  | ||||||
|         # by default we don't support any extended type. This can be |  | ||||||
|         # overridden by subclasses |  | ||||||
|         return None |  | ||||||
| 
 | 
 | ||||||
|     def pack(self, obj): |     def pack(self, obj): | ||||||
|         self._pack(obj) |         self._pack(obj) | ||||||
|  |  | ||||||
|  | @ -3,15 +3,16 @@ import msgpack | ||||||
| 
 | 
 | ||||||
| def test_extension_type(): | def test_extension_type(): | ||||||
|     class MyPacker(msgpack.Packer): |     class MyPacker(msgpack.Packer): | ||||||
|         def handle_extended_type(self, obj): |         def handle_unknown_type(self, obj): | ||||||
|             if isinstance(obj, array.array): |             if isinstance(obj, array.array): | ||||||
|                 fmt = "ext 32" |                 fmt = "ext 32" | ||||||
|                 typecode = 123 # application specific typecode |                 typecode = 123 # application specific typecode | ||||||
|                 data = obj.tostring() |                 data = obj.tostring() | ||||||
|                 return fmt, typecode, data |                 self.pack_extended_type(fmt, typecode, data) | ||||||
|  |                 return True | ||||||
| 
 | 
 | ||||||
|     class MyUnpacker(msgpack.Unpacker): |     class MyUnpacker(msgpack.Unpacker): | ||||||
|         def handle_extended_type(self, typecode, data): |         def read_extended_type(self, typecode, data): | ||||||
|             assert typecode == 123 |             assert typecode == 123 | ||||||
|             obj = array.array('d') |             obj = array.array('d') | ||||||
|             obj.fromstring(data) |             obj.fromstring(data) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Antonio Cuni
						Antonio Cuni