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

Commit fcb49b9

Browse files
committed
Change BinaryData.pack method
- Fixed test - Removed _pack method - BinaryData.pack accept only packable values, None and bytes
1 parent 04dd2a3 commit fcb49b9

2 files changed

Lines changed: 27 additions & 33 deletions

File tree

pyof/foundation/basic_types.py

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

504494
class TypeList(list, GenericStruct):

tests/test_foundation/test_basic_types.py

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

175-
def test_none_value(self):
176-
"""Pack of BinaryData initialized with None should be empty bytes."""
175+
def test_pack_none_value(self):
176+
"""Test packing None value."""
177177
expected = b''
178178
actual = BinaryData(None).pack()
179179
self.assertEqual(expected, actual)
180180

181-
def test_pack_bytes(self):
181+
def test_pack_bytes_value(self):
182182
"""Test packing some bytes."""
183183
expected = b'forty two'
184184
actual = BinaryData(expected).pack()
@@ -190,10 +190,14 @@ def test_pack_empty_bytes(self):
190190
actual = BinaryData(expected).pack()
191191
self.assertEqual(expected, actual)
192192

193-
def test_unexpected_value(self):
194-
"""Should raise ValueError if constructor value is not bytes."""
195-
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)
196199

197200
def test_unexpected_value_as_parameter(self):
198201
"""Should raise ValueError if pack value is not bytes."""
199-
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)