4545MAX_VALID_SUB_DEVICE = 0x0200
4646ALL_SUB_DEVICES = 0xffff
4747
48- # The two types of commands classes
48+ # The different types of commands classes
4949RDM_GET , RDM_SET , RDM_DISCOVERY = range (3 )
5050
5151
@@ -637,6 +637,12 @@ def Pack(self, args):
637637 arg = args [0 ]
638638 arg_size = len (arg )
639639
640+ # Handle the fact a UTF-8 character could be multi-byte
641+ if sys .version_info >= (3 , 2 ):
642+ arg_size = max (arg_size , len (bytes (arg , 'utf-8' )))
643+ else :
644+ arg_size = max (arg_size , len (arg .encode ('utf-8' )))
645+
640646 if self .max is not None and arg_size > self .max :
641647 raise ArgsValidationError ('%s can be at most %d,' %
642648 (self .name , self .max ))
@@ -647,9 +653,9 @@ def Pack(self, args):
647653
648654 try :
649655 if sys .version_info >= (3 , 2 ):
650- data = struct .unpack ('%ds' % arg_size , bytes (arg , 'utf8 ' ))
656+ data = struct .unpack ('%ds' % arg_size , bytes (arg , 'utf-8 ' ))
651657 else :
652- data = struct .unpack ('%ds' % arg_size , arg )
658+ data = struct .unpack ('%ds' % arg_size , arg . encode ( 'utf-8' ) )
653659 except struct .error as e :
654660 raise ArgsValidationError ("Can't pack data: %s" % e )
655661 return data [0 ], 1
@@ -669,10 +675,12 @@ def Unpack(self, data):
669675 except struct .error as e :
670676 raise UnpackException (e )
671677
672- if sys .version_info >= (3 , 2 ):
673- return value [0 ].rstrip (b'\x00 ' ).decode ('utf-8' )
674- else :
675- return value [0 ].rstrip (b'\x00 ' )
678+ try :
679+ value = value [0 ].rstrip (b'\x00 ' ).decode ('utf-8' )
680+ except UnicodeDecodeError as e :
681+ raise UnpackException (e )
682+
683+ return value
676684
677685 def GetDescription (self , indent = 0 ):
678686 indent = ' ' * indent
@@ -878,7 +886,7 @@ def Unpack(self, data):
878886 'Too many repeated group_count for %s, limit is %d, found %d' %
879887 (self .name , self .max , group_count ))
880888
881- if self .max is not None and group_count < self .min :
889+ if self .min is not None and group_count < self .min :
882890 raise UnpackException (
883891 'Too few repeated group_count for %s, limit is %d, found %d' %
884892 (self .name , self .min , group_count ))
0 commit comments