@@ -428,18 +428,12 @@ class BinaryData(GenericType):
428428 return the size of the instance using Python's :func:`len`.
429429 """
430430
431- def __init__ (self , value = b'' ): # noqa
431+ def __init__ (self , value = None ): # pylint: disable=useless-super-delegation
432432 """The constructor takes the parameter below.
433433
434434 Args:
435435 value (bytes): The binary data. Defaults to an empty value.
436-
437- Raises:
438- ValueError: If given value is not bytes.
439-
440436 """
441- if not isinstance (value , bytes ):
442- raise ValueError ('BinaryData must contain bytes.' )
443437 super ().__init__ (value )
444438
445439 def pack (self , value = None ):
@@ -449,21 +443,20 @@ def pack(self, value=None):
449443 bytes: The binary representation.
450444
451445 Raises:
452- :exc:`~.exceptions.NotBinaryData`: If value is not :class:` bytes`.
446+ ValueError: If value can't be represented with bytes
453447
454448 """
455- if isinstance (value , type (self )):
456- return value .pack ()
457-
458449 if value is None :
459450 value = self ._value
460451
461- if value :
462- if isinstance (value , bytes ):
463- return value
464- raise ValueError ('BinaryData must contain bytes.' )
465-
466- return b''
452+ if hasattr (value , 'pack' ) and callable (value .pack ):
453+ return value .pack ()
454+ elif isinstance (value , bytes ):
455+ return value
456+ elif value is None :
457+ return b''
458+ else :
459+ raise ValueError (f"BinaryData can't be { type (value )} = '{ value } '" )
467460
468461 def unpack (self , buff , offset = 0 ):
469462 """Unpack a binary message into this object's attributes.
@@ -490,11 +483,12 @@ class attribute of the struct.
490483
491484 """
492485 if value is None :
493- return len (self ._value )
494- elif hasattr (value , 'get_size' ):
486+ value = self ._value
487+
488+ if hasattr (value , 'get_size' ):
495489 return value .get_size ()
496490
497- return len (value )
491+ return len (self . pack ( value ) )
498492
499493
500494class TypeList (list , GenericStruct ):
0 commit comments