[Python-modules-commits] [bitstruct] 01/03: Import bitstruct_3.1.0.orig.tar.gz
Brian May
bam at moszumanska.debian.org
Wed Apr 6 02:14:01 UTC 2016
This is an automated email from the git hooks/post-receive script.
bam pushed a commit to branch master
in repository bitstruct.
commit 87088f591245497616b8ee1618f8327a30d53b9b
Author: Brian May <bam at debian.org>
Date: Wed Apr 6 12:03:55 2016 +1000
Import bitstruct_3.1.0.orig.tar.gz
---
PKG-INFO | 36 ++++++++--------
README.rst | 32 +++++++-------
bitstruct.egg-info/PKG-INFO | 36 ++++++++--------
bitstruct.py | 42 +++++++++---------
docs/conf.py | 6 ++-
setup.py | 5 ++-
tests/test_bitstruct.py | 103 ++++++++++++++++++++++++++------------------
7 files changed, 143 insertions(+), 117 deletions(-)
diff --git a/PKG-INFO b/PKG-INFO
index e14c2df..ce3a6ca 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,7 +1,7 @@
Metadata-Version: 1.1
Name: bitstruct
-Version: 2.1.3
-Summary: This module performs conversions between Python values and C bit field structs represented as Python bytearrays.
+Version: 3.1.0
+Summary: This module performs conversions between Python values and C bit field structs represented as Python byte strings.
Home-page: https://github.com/eerimoq/bitstruct
Author: Erik Moqvist, Ilya Petukhov
Author-email: erik.moqvist at gmail.com
@@ -37,8 +37,8 @@ Description: |buildstatus|_
>>> from bitstruct import *
>>> pack('u1u3u4s16', 1, 2, 3, -4)
- bytearray(b'\xa3\xff\xfc')
- >>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+ b'\xa3\xff\xfc'
+ >>> unpack('u1u3u4s16', b'\xa3\xff\xfc')
(1, 2, 3, -4)
>>> calcsize('u1u3u4s16')
24
@@ -51,7 +51,7 @@ Description: |buildstatus|_
>>> from bitstruct import *
>>> from collections import namedtuple
>>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
- >>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+ >>> unpacked = unpack('u1u3u4s16', b'\xa3\xff\xfc')
>>> myname = MyName(*unpacked)
>>> myname
myname(a=1, b=2, c=3, d=-4)
@@ -59,15 +59,15 @@ Description: |buildstatus|_
3
An example of packing/unpacking a unsinged integer, a signed integer,
- a float, a boolean and a bytearray:
+ a float, a boolean and a byte string:
.. code-block:: python
>>> from bitstruct import *
- >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
- bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
- >>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
- (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+ >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, b'\xff\xff')
+ b'\x0f\xd0\x1c\x00\x00?\xff'
+ >>> unpack('u5s5f32b1r13', b'\x0f\xd0\x1c\x00\x00?\xff')
+ (1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('u5s5f32b1r13')
56
@@ -78,10 +78,10 @@ Description: |buildstatus|_
.. code-block:: python
>>> from bitstruct import *
- >>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
- bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff')
- >>> unpack('<u5s5f32b1r13', bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff'))
- (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+ >>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, b'\xff\xff')
+ b'\x87\xc0\x00\x03\x80\xbf\xff'
+ >>> unpack('<u5s5f32b1r13', b'\x87\xc0\x00\x03\x80\xbf\xff')
+ (1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('<u5s5f32b1r13')
56
@@ -90,13 +90,13 @@ Description: |buildstatus|_
.. code-block:: python
>>> from bitstruct import *
- >>> unpack('s17s13r24', bytearray('0123456789abcdef'.decode('hex')))
- (582, -3751, bytearray(b'\xe2j\xf3'))
+ >>> unpack('s17s13r24', '0123456789abcdef'.decode('hex'))
+ (582, -3751, b'\xe2j\xf3')
>>> with open("test.bin", "rb") as fin:
- ... unpack('s17s13r24', bytearray(fin.read(8)))
+ ... unpack('s17s13r24', fin.read(8))
...
...
- (582, -3751, bytearray(b'\xe2j\xf3'))
+ (582, -3751, b'\xe2j\xf3')
Change endianness of the data with byteswap(), and then unpack the
values:
diff --git a/README.rst b/README.rst
index 3bc2172..79c2353 100644
--- a/README.rst
+++ b/README.rst
@@ -29,8 +29,8 @@ A basic example of packing/unpacking four integers:
>>> from bitstruct import *
>>> pack('u1u3u4s16', 1, 2, 3, -4)
- bytearray(b'\xa3\xff\xfc')
- >>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+ b'\xa3\xff\xfc'
+ >>> unpack('u1u3u4s16', b'\xa3\xff\xfc')
(1, 2, 3, -4)
>>> calcsize('u1u3u4s16')
24
@@ -43,7 +43,7 @@ wrapping the result in a named tuple:
>>> from bitstruct import *
>>> from collections import namedtuple
>>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
- >>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+ >>> unpacked = unpack('u1u3u4s16', b'\xa3\xff\xfc')
>>> myname = MyName(*unpacked)
>>> myname
myname(a=1, b=2, c=3, d=-4)
@@ -51,15 +51,15 @@ wrapping the result in a named tuple:
3
An example of packing/unpacking a unsinged integer, a signed integer,
-a float, a boolean and a bytearray:
+a float, a boolean and a byte string:
.. code-block:: python
>>> from bitstruct import *
- >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
- bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
- >>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
- (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+ >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, b'\xff\xff')
+ b'\x0f\xd0\x1c\x00\x00?\xff'
+ >>> unpack('u5s5f32b1r13', b'\x0f\xd0\x1c\x00\x00?\xff')
+ (1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('u5s5f32b1r13')
56
@@ -70,10 +70,10 @@ Significant Bit) first:
.. code-block:: python
>>> from bitstruct import *
- >>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
- bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff')
- >>> unpack('<u5s5f32b1r13', bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff'))
- (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+ >>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, b'\xff\xff')
+ b'\x87\xc0\x00\x03\x80\xbf\xff'
+ >>> unpack('<u5s5f32b1r13', b'\x87\xc0\x00\x03\x80\xbf\xff')
+ (1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('<u5s5f32b1r13')
56
@@ -82,13 +82,13 @@ An example of unpacking values from a hexstring and a binary file:
.. code-block:: python
>>> from bitstruct import *
- >>> unpack('s17s13r24', bytearray('0123456789abcdef'.decode('hex')))
- (582, -3751, bytearray(b'\xe2j\xf3'))
+ >>> unpack('s17s13r24', '0123456789abcdef'.decode('hex'))
+ (582, -3751, b'\xe2j\xf3')
>>> with open("test.bin", "rb") as fin:
- ... unpack('s17s13r24', bytearray(fin.read(8)))
+ ... unpack('s17s13r24', fin.read(8))
...
...
- (582, -3751, bytearray(b'\xe2j\xf3'))
+ (582, -3751, b'\xe2j\xf3')
Change endianness of the data with byteswap(), and then unpack the
values:
diff --git a/bitstruct.egg-info/PKG-INFO b/bitstruct.egg-info/PKG-INFO
index e14c2df..ce3a6ca 100644
--- a/bitstruct.egg-info/PKG-INFO
+++ b/bitstruct.egg-info/PKG-INFO
@@ -1,7 +1,7 @@
Metadata-Version: 1.1
Name: bitstruct
-Version: 2.1.3
-Summary: This module performs conversions between Python values and C bit field structs represented as Python bytearrays.
+Version: 3.1.0
+Summary: This module performs conversions between Python values and C bit field structs represented as Python byte strings.
Home-page: https://github.com/eerimoq/bitstruct
Author: Erik Moqvist, Ilya Petukhov
Author-email: erik.moqvist at gmail.com
@@ -37,8 +37,8 @@ Description: |buildstatus|_
>>> from bitstruct import *
>>> pack('u1u3u4s16', 1, 2, 3, -4)
- bytearray(b'\xa3\xff\xfc')
- >>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+ b'\xa3\xff\xfc'
+ >>> unpack('u1u3u4s16', b'\xa3\xff\xfc')
(1, 2, 3, -4)
>>> calcsize('u1u3u4s16')
24
@@ -51,7 +51,7 @@ Description: |buildstatus|_
>>> from bitstruct import *
>>> from collections import namedtuple
>>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
- >>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
+ >>> unpacked = unpack('u1u3u4s16', b'\xa3\xff\xfc')
>>> myname = MyName(*unpacked)
>>> myname
myname(a=1, b=2, c=3, d=-4)
@@ -59,15 +59,15 @@ Description: |buildstatus|_
3
An example of packing/unpacking a unsinged integer, a signed integer,
- a float, a boolean and a bytearray:
+ a float, a boolean and a byte string:
.. code-block:: python
>>> from bitstruct import *
- >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
- bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
- >>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
- (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+ >>> pack('u5s5f32b1r13', 1, -1, 3.75, True, b'\xff\xff')
+ b'\x0f\xd0\x1c\x00\x00?\xff'
+ >>> unpack('u5s5f32b1r13', b'\x0f\xd0\x1c\x00\x00?\xff')
+ (1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('u5s5f32b1r13')
56
@@ -78,10 +78,10 @@ Description: |buildstatus|_
.. code-block:: python
>>> from bitstruct import *
- >>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
- bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff')
- >>> unpack('<u5s5f32b1r13', bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff'))
- (1, -1, 3.75, True, bytearray(b'\xff\xf8'))
+ >>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, b'\xff\xff')
+ b'\x87\xc0\x00\x03\x80\xbf\xff'
+ >>> unpack('<u5s5f32b1r13', b'\x87\xc0\x00\x03\x80\xbf\xff')
+ (1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('<u5s5f32b1r13')
56
@@ -90,13 +90,13 @@ Description: |buildstatus|_
.. code-block:: python
>>> from bitstruct import *
- >>> unpack('s17s13r24', bytearray('0123456789abcdef'.decode('hex')))
- (582, -3751, bytearray(b'\xe2j\xf3'))
+ >>> unpack('s17s13r24', '0123456789abcdef'.decode('hex'))
+ (582, -3751, b'\xe2j\xf3')
>>> with open("test.bin", "rb") as fin:
- ... unpack('s17s13r24', bytearray(fin.read(8)))
+ ... unpack('s17s13r24', fin.read(8))
...
...
- (582, -3751, bytearray(b'\xe2j\xf3'))
+ (582, -3751, b'\xe2j\xf3')
Change endianness of the data with byteswap(), and then unpack the
values:
diff --git a/bitstruct.py b/bitstruct.py
index 7ab0c53..c02d778 100644
--- a/bitstruct.py
+++ b/bitstruct.py
@@ -1,6 +1,8 @@
import re
import struct
+__version__ = "3.1.0"
+
def _parse_format(fmt):
parsed_infos = re.findall(r'([<>]?)([a-zA-Z])(\d+)', fmt)
@@ -74,6 +76,7 @@ def _unpack_float(size, bits):
return value
+
def _unpack_bytearray(size, bits):
value = bytearray()
for i in range(size // 8):
@@ -85,14 +88,14 @@ def _unpack_bytearray(size, bits):
def pack(fmt, *args):
- """Return a bytearray containing the values v1, v2, ... packed
+ """Return a byte string containing the values v1, v2, ... packed
according to the given format. If the total number of bits are not
a multiple of 8, padding will be added at the end of the last
byte.
:param fmt: Bitstruct format string. See format description below.
:param args: Variable argument list of values to pack.
- :returns: A bytearray of the packed values.
+ :returns: A byte string of the packed values.
`fmt` is a string of bitorder-type-length groups. Bitorder may be
omitted.
@@ -108,7 +111,7 @@ def pack(fmt, *args):
- 's' -- signed integer
- 'f' -- floating point number of 32 or 64 bits
- 'b' -- boolean
- - 'r' -- raw, bytearray
+ - 'r' -- raw, bytes
- 'p' -- padding, ignore
Length is the number of bits to pack the value into.
@@ -132,7 +135,7 @@ def pack(fmt, *args):
elif _type == 'b':
value_bits = _pack_boolean(size, args[i])
elif _type == 'r':
- value_bits = _pack_bytearray(size, args[i])
+ value_bits = _pack_bytearray(size, bytearray(args[i]))
else:
raise ValueError("bad type '{}' in format".format(_type))
@@ -148,22 +151,22 @@ def pack(fmt, *args):
if tail != 0:
bits += (8 - tail) * '0'
- return bytearray([int(''.join(bits[i:i+8]), 2)
- for i in range(0, len(bits), 8)])
+ return bytes(bytearray([int(''.join(bits[i:i+8]), 2)
+ for i in range(0, len(bits), 8)]))
def unpack(fmt, data):
- """Unpack the bytearray (presumably packed by pack(fmt, ...))
+ """Unpack `data` (byte string, bytearray or list of integers)
according to the given format. The result is a tuple even if it
contains exactly one item.
:param fmt: Bitstruct format string. See pack() for details.
- :param data: Bytearray of values to unpack.
+ :param data: Byte string of values to unpack.
:returns: A tuple of the unpacked values.
"""
- bits = ''.join(['{:08b}'.format(b) for b in data])
+ bits = ''.join(['{:08b}'.format(b) for b in bytearray(data)])
infos = _parse_format(fmt)
res = []
i = 0
@@ -185,7 +188,7 @@ def unpack(fmt, data):
elif _type == 'b':
value = _unpack_boolean(value_bits)
elif _type == 'r':
- value = _unpack_bytearray(size, value_bits)
+ value = bytes(_unpack_bytearray(size, value_bits))
else:
raise ValueError("bad type '{}' in format".format(_type))
res.append(value)
@@ -195,8 +198,7 @@ def unpack(fmt, data):
def calcsize(fmt):
- """Return the size of the bitstruct (and hence of the bytearray)
- corresponding to the given format.
+ """Calculate the number of bits in given format.
:param fmt: Bitstruct format string.
:returns: Number of bits in format string.
@@ -207,26 +209,26 @@ def calcsize(fmt):
def byteswap(fmt, data, offset = 0):
- """In place swap bytes in `data` according to `fmt`, starting at byte
+ """Swap bytes in `data` according to `fmt`, starting at byte
`offset`. `fmt` must be an iterable, iterating over number of
bytes to swap. For example, the format string "24" applied to the
- bytearray "\x00\x11\x22\x33\x44\x55" will produce the result
- "\x11\x00\x55\x44\x33\x22".
+ byte string b'\x00\x11\x22\x33\x44\x55' will produce the result
+ b'\x11\x00\x55\x44\x33\x22'.
:param fmt: Swap format string.
- :param data: Bytearray of data to swap.
+ :param data: Byte string of data to swap.
:param offset: Start offset into `data`.
- :returns: Bytearray of swapped bytes.
+ :returns: Byte string of swapped bytes.
"""
i = offset
+ data_swapped = b''
for f in fmt:
length = int(f)
value = data[i:i+length]
- value.reverse()
- data[i:i+length] = value
+ data_swapped += value[::-1]
i += length
- return data
+ return data_swapped
diff --git a/docs/conf.py b/docs/conf.py
index 94c0fdc..0bd2b99 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -21,6 +21,8 @@ import shlex
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('..'))
+import bitstruct
+
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
@@ -58,9 +60,9 @@ author = u'Erik Moqvist'
# built documents.
#
# The short X.Y version.
-version = '2.1.3'
+version = bitstruct.__version__
# The full version, including alpha/beta/rc tags.
-release = '2.1.3'
+release = bitstruct.__version__
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
diff --git a/setup.py b/setup.py
index 33424b2..c9024a8 100755
--- a/setup.py
+++ b/setup.py
@@ -1,12 +1,13 @@
#!/usr/bin/env python
from setuptools import setup
+import bitstruct
setup(name='bitstruct',
- version='2.1.3',
+ version=bitstruct.__version__,
description=('This module performs conversions between Python values '
'and C bit field structs represented as Python '
- 'bytearrays.'),
+ 'byte strings.'),
long_description=open('README.rst', 'r').read(),
author='Erik Moqvist, Ilya Petukhov',
author_email='erik.moqvist at gmail.com',
diff --git a/tests/test_bitstruct.py b/tests/test_bitstruct.py
index c27b355..5011f96 100644
--- a/tests/test_bitstruct.py
+++ b/tests/test_bitstruct.py
@@ -10,83 +10,103 @@ class BitStructTest(unittest.TestCase):
"""
packed = pack('u1u1s6u7u9', 0, 0, -2, 65, 22)
- self.assertEqual(packed, bytearray(b'\x3e\x82\x16'))
+ self.assertEqual(packed, b'\x3e\x82\x16')
packed = pack('u1', 1)
- self.assertEqual(packed, bytearray(b'\x80'))
+ self.assertEqual(packed, b'\x80')
packed = pack('u77', 0x100000000001000000)
- ref = bytearray(b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00')
+ ref = b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00'
self.assertEqual(packed, ref)
packed = pack('p1u1s6u7u9', 0, -2, 65, 22)
- self.assertEqual(packed, bytearray(b'\x3e\x82\x16'))
+ self.assertEqual(packed, b'\x3e\x82\x16')
packed = pack('p1u1s6p7u9', 0, -2, 22)
- self.assertEqual(packed, bytearray(b'\x3e\x00\x16'))
+ self.assertEqual(packed, b'\x3e\x00\x16')
- packed = pack('u1s6f32r43', 0, -2, 3.75, bytearray(b'\x00\xff\x00\xff\x00\xff'))
- self.assertEqual(packed, bytearray(b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0'))
+ packed = pack('u1s6f32r43', 0, -2, 3.75, b'\x00\xff\x00\xff\x00\xff')
+ self.assertEqual(packed, b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0')
packed = pack('b1', True)
- self.assertEqual(packed, bytearray(b'\x80'))
+ self.assertEqual(packed, b'\x80')
packed = pack('b1p6b1', True, True)
- self.assertEqual(packed, bytearray(b'\x81'))
+ self.assertEqual(packed, b'\x81')
packed = pack('u5b2u1', -1, False, 1)
- self.assertEqual(packed, bytearray(b'\xf9'))
+ self.assertEqual(packed, b'\xf9')
def test_unpack(self):
"""Unpack values.
"""
- unpacked = unpack('u1u1s6u7u9', bytearray(b'\x3e\x82\x16'))
+ unpacked = unpack('u1u1s6u7u9', b'\x3e\x82\x16')
self.assertEqual(unpacked, (0, 0, -2, 65, 22))
unpacked = unpack('u1', bytearray(b'\x80'))
self.assertEqual(unpacked, (1,))
- packed = bytearray(b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00')
+ packed = b'\x00\x80\x00\x00\x00\x00\x08\x00\x00\x00'
unpacked = unpack('u77', packed)
self.assertEqual(unpacked, (0x100000000001000000,))
- packed = bytearray(b'\x3e\x82\x16')
+ packed = b'\x3e\x82\x16'
unpacked = unpack('p1u1s6u7u9', packed)
self.assertEqual(unpacked, (0, -2, 65, 22))
- packed = bytearray(b'\x3e\x82\x16')
+ packed = b'\x3e\x82\x16'
unpacked = unpack('p1u1s6p7u9', packed)
self.assertEqual(unpacked, (0, -2, 22))
- packed = bytearray(b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0')
+ packed = b'\x7c\x80\xe0\x00\x00\x01\xfe\x01\xfe\x01\xc0'
unpacked = unpack('u1s6f32r43', packed)
- self.assertEqual(unpacked, (0, -2, 3.75, bytearray(b'\x00\xff\x00\xff\x00\xe0')))
+ self.assertEqual(unpacked, (0, -2, 3.75, b'\x00\xff\x00\xff\x00\xe0'))
- packed = bytearray(b'\x80')
+ packed = [0x80]
unpacked = unpack('b1', packed)
self.assertEqual(unpacked, (True,))
- packed = bytearray(b'\x80')
+ packed = b'\x80'
unpacked = unpack('b1p6b1', packed)
self.assertEqual(unpacked, (True, False))
- packed = bytearray(b'\x06')
+ packed = b'\x06'
unpacked = unpack('u5b2u1', packed)
self.assertEqual(unpacked, (0, True, 0))
- packed = bytearray(b'\x04')
+ packed = b'\x04'
unpacked = unpack('u5b2u1', packed)
self.assertEqual(unpacked, (0, True, 0))
# bad float size
try:
- unpack('f33', bytearray(b'\x00\x00\x00\x00\x00'))
+ unpack('f33', b'\x00\x00\x00\x00\x00')
self.fail()
except ValueError:
pass
+ # gcc packed struct with bitfields
+ #
+ # struct foo_t {
+ # int a;
+ # char b;
+ # uint32_t c : 7;
+ # uint32_t d : 25;
+ # } foo;
+ #
+ # foo.a = 1;
+ # foo.b = 1;
+ # foo.c = 0x67;
+ # foo.d = 0x12345;
+ unpacked = unpack('s32s8u25u7',
+ byteswap('414',
+ b'\x01\x00\x00\x00\x01\xe7\xa2\x91\x00'))
+ self.assertEqual(unpacked, (1, 1, 0x12345, 0x67))
+
+
+
def test_pack_unpack(self):
"""Pack and unpack values.
@@ -125,8 +145,8 @@ class BitStructTest(unittest.TestCase):
"""
- res = bytearray(b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a')
- ref = bytearray(b'\x01\x03\x02\x04\x08\x07\x06\x05\x0a\x09')
+ res = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a'
+ ref = b'\x01\x03\x02\x04\x08\x07\x06\x05\x0a\x09'
self.assertEqual(byteswap('12142', ref), res)
packed = pack('u1u5u2u16', 1, 2, 3, 4)
@@ -139,39 +159,40 @@ class BitStructTest(unittest.TestCase):
"""
# big endian
- ref = b"\x02\x46\x9a\xfe\x00\x00\x00"
- packed = pack(">u19s3f32", 0x1234, -2, -1.0)
+ ref = b'\x02\x46\x9a\xfe\x00\x00\x00'
+ packed = pack('>u19s3f32', 0x1234, -2, -1.0)
self.assertEqual(packed, ref)
- unpacked = unpack(">u19s3f32", packed)
+ unpacked = unpack('>u19s3f32', packed)
self.assertEqual(unpacked, (0x1234, -2, -1.0))
# little endian
- ref = b"\x2c\x48\x0c\x00\x00\x07\xf4"
- packed = pack("<u19s3f32", 0x1234, -2, -1.0)
+ ref = b'\x2c\x48\x0c\x00\x00\x07\xf4'
+ packed = pack('<u19s3f32', 0x1234, -2, -1.0)
self.assertEqual(packed, ref)
- unpacked = unpack("<u19s3f32", packed)
+ unpacked = unpack('<u19s3f32', packed)
self.assertEqual(unpacked, (0x1234, -2, -1.0))
# 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, bytearray(b"\x80"))
+ 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, bytearray(b"\x80")))
+ unpacked = unpack('>u19<s5>f64r3p4', packed)
+ self.assertEqual(unpacked, (1, -2, 1.0, b'\x80'))
- # 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, bytearray(b"\x80"))
+ # 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, bytearray(b"\x80")))
+ unpacked = unpack('<u19>s5<f64r3p4', packed)
+ self.assertEqual(unpacked, (1, -2, 1.0, b'\x80'))
# pack as big endian, unpack as little endian
- ref = b"\x40"
- packed = pack("u2", 1)
+ ref = b'\x40'
+ packed = pack('u2', 1)
self.assertEqual(packed, ref)
- unpacked = unpack("<u2", packed)
+ unpacked = unpack('<u2', packed)
self.assertEqual(unpacked, (2, ))
+
if __name__ == '__main__':
unittest.main()
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/bitstruct.git
More information about the Python-modules-commits
mailing list