Skip to content

Commit

Permalink
Comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed May 8, 2018
1 parent 9411d3e commit 3242881
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
11 changes: 6 additions & 5 deletions bitstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ def _pack_value(self, type_, size, value, endianness, bits):
else:
raise Error("bad type '{}' in format".format(type_))

# reverse the bit order in little endian values
# Reverse the bit order in little endian values.
if endianness == "<":
value_bits = value_bits[::-1]

# reverse bytes order for least significant byte first
# Reverse bytes order for least significant byte first.
if self._byte_order == ">":
bits += value_bits
else:
Expand Down Expand Up @@ -219,7 +219,7 @@ def pack(self, *args):
bits = self._pack_value(type_, size, args[i], endianness, bits)
i += 1

# padding of last byte
# Padding of last byte.
tail = len(bits) % 8

if tail != 0:
Expand Down Expand Up @@ -314,7 +314,8 @@ def unpack_from(self, data, offset=0):
if type_ in 'pP':
pass
else:
# reverse bytes order for least significant byte first
# Reverse bytes order for least significant byte
# first.
if self._byte_order == ">":
value_bits = bits[offset:offset + size]
else:
Expand All @@ -329,7 +330,7 @@ def unpack_from(self, data, offset=0):

value_bits += value_bits_tmp

# reverse the bit order in little endian values
# Reverse the bit order in little endian values.
if endianness == "<":
value_bits = value_bits[::-1]

Expand Down
24 changes: 12 additions & 12 deletions tests/test_bitstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,35 +267,35 @@ def test_endianness(self):
"""

# big endian
# Big endian.
ref = b'\x02\x46\x9a\xfe\x00\x00\x00'
packed = pack('>u19s3f32', 0x1234, -2, -1.0)
self.assertEqual(packed, ref)
unpacked = unpack('>u19s3f32', packed)
self.assertEqual(unpacked, (0x1234, -2, -1.0))

# little endian
# Little endian.
ref = b'\x2c\x48\x0c\x00\x00\x07\xf4'
packed = pack('<u19s3f32', 0x1234, -2, -1.0)
self.assertEqual(packed, ref)
unpacked = unpack('<u19s3f32', packed)
self.assertEqual(unpacked, (0x1234, -2, -1.0))

# mixed endianness
# Mixed endianness.
ref = b'\x00\x00\x2f\x3f\xf0\x00\x00\x00\x00\x00\x00\x80'
packed = pack('>u19<s5>f64r3p4', 1, -2, 1.0, b'\x80')
self.assertEqual(packed, ref)
unpacked = unpack('>u19<s5>f64r3p4', packed)
self.assertEqual(unpacked, (1, -2, 1.0, b'\x80'))

# opposite endianness of the 'mixed endianness' test
# Opposite endianness of the 'mixed endianness' test.
ref = b'\x80\x00\x1e\x00\x00\x00\x00\x00\x00\x0f\xfc\x20'
packed = pack('<u19>s5<f64r3p4', 1, -2, 1.0, b'\x80')
self.assertEqual(packed, ref)
unpacked = unpack('<u19>s5<f64r3p4', packed)
self.assertEqual(unpacked, (1, -2, 1.0, b'\x80'))

# pack as big endian, unpack as little endian
# Pack as big endian, unpack as little endian.
ref = b'\x40'
packed = pack('u2', 1)
self.assertEqual(packed, ref)
Expand All @@ -307,49 +307,49 @@ def test_byte_order(self):
"""

# most significant byte first (default)
# Most significant byte first (default).
ref = b'\x02\x46\x9a\xfe\x00\x00\x00'
packed = pack('u19s3f32>', 0x1234, -2, -1.0)
self.assertEqual(packed, ref)
unpacked = unpack('u19s3f32>', packed)
self.assertEqual(unpacked, (0x1234, -2, -1.0))

# least significant byte first
# Least significant byte first.
ref = b'\x34\x12\x18\x00\x00\xe0\xbc'
packed = pack('u19s3f32<', 0x1234, -2, -1.0)
self.assertEqual(packed, ref)
unpacked = unpack('u19s3f32<', packed)
self.assertEqual(unpacked, (0x1234, -2, -1.0))

# least significant byte first
# Least significant byte first.
ref = b'\x34\x12'
packed = pack('u8s8<', 0x34, 0x12)
self.assertEqual(packed, ref)
unpacked = unpack('u8s8<', packed)
self.assertEqual(unpacked, (0x34, 0x12))

# least significant byte first
# Least significant byte first.
ref = b'\x34\x22'
packed = pack('u3u12<', 1, 0x234)
self.assertEqual(packed, ref)
unpacked = unpack('u3s12<', packed)
self.assertEqual(unpacked, (1, 0x234))

# least significant byte first
# Least significant byte first.
ref = b'\x34\x11\x00'
packed = pack('u3u17<', 1, 0x234)
self.assertEqual(packed, ref)
unpacked = unpack('u3s17<', packed)
self.assertEqual(unpacked, (1, 0x234))

# least significant byte first
# Least significant byte first.
ref = b'\x80'
packed = pack('u1<', 1)
self.assertEqual(packed, ref)
unpacked = unpack('u1<', packed)
self.assertEqual(unpacked, (1, ))

# least significant byte first
# Least significant byte first.
ref = b'\x45\x23\x25\x82'
packed = pack('u19u5u1u7<', 0x12345, 5, 1, 2)
self.assertEqual(packed, ref)
Expand Down

0 comments on commit 3242881

Please sign in to comment.