@@ -428,46 +428,35 @@ class BinaryData(GenericType):
428428 return the size of the instance using Python's :func:`len`.
429429 """
430430
431- def __init__ (self , value = None ): # 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- value = self ._pack (value )
442437 super ().__init__ (value )
443438
444- @staticmethod
445- def _pack (value ):
446- if hasattr (value , 'pack' ) and callable (value .pack ):
447- value = value .pack ()
448- elif value is None :
449- value = b''
450-
451- if not isinstance (value , bytes ):
452- msg = 'BinaryData value must contain bytes or have pack method. '
453- msg += 'Received type {} value: "{}"' .format (type (value ), value )
454- raise ValueError (msg )
455-
456- return value
457-
458439 def pack (self , value = None ):
459440 """Pack the value as a binary representation.
460441
461442 Returns:
462443 bytes: The binary representation.
463444
464445 Raises:
465- :exc:`~.exceptions.NotBinaryData`: If value is not :class:` bytes`.
446+ ValueError: If value can't be represented with bytes
466447
467448 """
468449 if value is None :
469- return self ._value
470- return self ._pack (value )
450+ value = self ._value
451+
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 } '" )
471460
472461 def unpack (self , buff , offset = 0 ):
473462 """Unpack a binary message into this object's attributes.
@@ -494,11 +483,12 @@ class attribute of the struct.
494483
495484 """
496485 if value is None :
497- return len (self ._value )
498- elif hasattr (value , 'get_size' ):
486+ value = self ._value
487+
488+ if hasattr (value , 'get_size' ):
499489 return value .get_size ()
500490
501- return len (value )
491+ return len (self . pack ( value ) )
502492
503493
504494class TypeList (list , GenericStruct ):
0 commit comments