Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit fcdfda7

Browse files
authored
Merge pull request #445 from macartur/binarydata_fixes
Binarydata fixes
2 parents 61e2ba4 + fcb49b9 commit fcdfda7

2 files changed

Lines changed: 29 additions & 25 deletions

File tree

pyof/foundation/basic_types.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

500494
class TypeList(list, GenericStruct):

tests/test_foundation/test_basic_types.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ def test_default_value(self):
172172
actual = BinaryData().pack()
173173
self.assertEqual(expected, actual)
174174

175-
def test_pack_bytes(self):
175+
def test_pack_none_value(self):
176+
"""Test packing None value."""
177+
expected = b''
178+
actual = BinaryData(None).pack()
179+
self.assertEqual(expected, actual)
180+
181+
def test_pack_bytes_value(self):
176182
"""Test packing some bytes."""
177183
expected = b'forty two'
178184
actual = BinaryData(expected).pack()
@@ -184,10 +190,14 @@ def test_pack_empty_bytes(self):
184190
actual = BinaryData(expected).pack()
185191
self.assertEqual(expected, actual)
186192

187-
def test_unexpected_value(self):
188-
"""Should raise ValueError if constructor value is not bytes."""
189-
self.assertRaises(ValueError, BinaryData, "can't be string")
193+
def test_pack_packable_value(self):
194+
"""Test packing packable value."""
195+
hw_addr = basic_types.HWAddress('0a:d3:98:a5:30:47')
196+
expected = hw_addr.pack()
197+
actual = BinaryData(hw_addr).pack()
198+
self.assertEqual(expected, actual)
190199

191200
def test_unexpected_value_as_parameter(self):
192201
"""Should raise ValueError if pack value is not bytes."""
193-
self.assertRaises(ValueError, BinaryData().pack, "can't be string")
202+
data= BinaryData('Some string')
203+
self.assertRaises(ValueError, data.pack, "can't be string")

0 commit comments

Comments
 (0)