@@ -485,6 +485,10 @@ class Packer(object):
485485 Convert unicode to bytes with this encoding. (default: 'utf-8')
486486 :param str unicode_errors:
487487 Error handler for encoding unicode. (default: 'strict')
488+ :param bool distinguish_tuple:
489+ If set to true, tuples will not be serialized as lists
490+ and will be treated as unsupported type. This is useful when trying
491+ to implement accurate serialization for python types.
488492 :param bool use_single_float:
489493 Use single precision float type for float. (default: False)
490494 :param bool autoreset:
@@ -495,7 +499,9 @@ class Packer(object):
495499 It also enable str8 type for unicode.
496500 """
497501 def __init__ (self , default = None , encoding = 'utf-8' , unicode_errors = 'strict' ,
498- use_single_float = False , autoreset = True , use_bin_type = False ):
502+ distinguish_tuple = False , use_single_float = False , autoreset = True ,
503+ use_bin_type = False ):
504+ self ._distinguish_tuple = distinguish_tuple
499505 self ._use_float = use_single_float
500506 self ._autoreset = autoreset
501507 self ._use_bin_type = use_bin_type
@@ -509,6 +515,7 @@ def __init__(self, default=None, encoding='utf-8', unicode_errors='strict',
509515
510516 def _pack (self , obj , nest_limit = DEFAULT_RECURSE_LIMIT , isinstance = isinstance ):
511517 default_used = False
518+ list_type = list if self ._distinguish_tuple else (list , tuple )
512519 while True :
513520 if nest_limit < 0 :
514521 raise PackValueError ("recursion limit exceeded" )
@@ -599,7 +606,7 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT, isinstance=isinstance):
599606 self ._buffer .write (struct .pack ("b" , code ))
600607 self ._buffer .write (data )
601608 return
602- if isinstance (obj , ( list , tuple ) ):
609+ if isinstance (obj , list_type ):
603610 n = len (obj )
604611 self ._fb_pack_array_header (n )
605612 for i in xrange (n ):
0 commit comments