[python-bitstring] 01/09: Imported Upstream version 3.1.5

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Thu May 26 07:21:32 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-bitstring.

commit 5b901f694b2468eb2d3bc1d365fc9c39cc38e0ff
Author: Ghislain Antony Vaillant <ghisvail at gmail.com>
Date:   Thu May 26 07:52:27 2016 +0100

    Imported Upstream version 3.1.5
---
 LICENSE                   | 21 +++++++++++++++++++++
 MANIFEST.in               |  2 +-
 bitstring.py              | 21 +++++++++++++++------
 doc/bitstring_classes.rst |  1 +
 doc/creation.rst          |  5 +++--
 release_notes.txt         |  7 +++++++
 setup.py                  |  2 +-
 test/test_bits.py         | 32 +++++++++++++++++++++++++++++++-
 test/test_bitstring.py    |  2 +-
 9 files changed, 81 insertions(+), 12 deletions(-)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..cf901d5
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (c) 2006-2016 Scott Griffiths (dr.scottgriffiths at gmail.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/MANIFEST.in b/MANIFEST.in
index e5867de..4d74e53 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -4,4 +4,4 @@ include release_notes.txt
 include README.rst
 prune doc
 include doc/bitstring_manual.pdf
-
+include LICENSE
diff --git a/bitstring.py b/bitstring.py
index 2e5ae9d..aa078d0 100644
--- a/bitstring.py
+++ b/bitstring.py
@@ -59,7 +59,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 """
 
-__version__ = "3.1.4"
+__version__ = "3.1.5"
 
 __author__ = "Scott Griffiths"
 
@@ -73,6 +73,7 @@ import os
 import struct
 import operator
 import collections
+import array
 
 byteorder = sys.byteorder
 
@@ -731,7 +732,7 @@ class Bits(object):
     def __init__(self, auto=None, length=None, offset=None, **kwargs):
         """Either specify an 'auto' initialiser:
         auto -- a string of comma separated tokens, an integer, a file object,
-                a bytearray, a boolean iterable or another bitstring.
+                a bytearray, a boolean iterable, an array or another bitstring.
 
         Or initialise via **kwargs with one (and only one) of:
         bytes -- raw data as a string, for example read from a binary file.
@@ -1242,7 +1243,7 @@ class Bits(object):
         self._datastore = ByteStore(bytearray(0))
 
     def _setauto(self, s, length, offset):
-        """Set bitstring from a bitstring, file, bool, integer, iterable or string."""
+        """Set bitstring from a bitstring, file, bool, integer, array, iterable or string."""
         # As s can be so many different things it's important to do the checks
         # in the correct order, as some types are also other allowed types.
         # So basestring must be checked before Iterable
@@ -1277,6 +1278,10 @@ class Bits(object):
         if isinstance(s, (bytes, bytearray)):
             self._setbytes_unsafe(bytearray(s), len(s) * 8, 0)
             return
+        if isinstance(s, array.array):
+            b = s.tostring()
+            self._setbytes_unsafe(bytearray(b), len(b) * 8, 0)
+            return
         if isinstance(s, numbers.Integral):
             # Initialise with s zero bits.
             if s < 0:
@@ -1916,9 +1921,13 @@ class Bits(object):
                                            "not multiple of 4 bits.")
         if not length:
             return ''
-        # This monstrosity is the only thing I could get to work for both 2.6 and 3.1.
-        # TODO: Is utf-8 really what we mean here?
-        s = str(binascii.hexlify(self._slice(start, start + length).tobytes()).decode('utf-8'))
+        s = self._slice(start, start + length).tobytes()
+        try:
+            s = s.hex() # Available in Python 3.5
+        except AttributeError:
+            # This monstrosity is the only thing I could get to work for both 2.6 and 3.1.
+            # TODO: Is utf-8 really what we mean here?
+            s = str(binascii.hexlify(s).decode('utf-8'))
         # If there's one nibble too many then cut it off
         return s[:-1] if (length // 4) % 2 else s
 
diff --git a/doc/bitstring_classes.rst b/doc/bitstring_classes.rst
index 96cb25c..d418054 100644
--- a/doc/bitstring_classes.rst
+++ b/doc/bitstring_classes.rst
@@ -44,6 +44,7 @@ The ``auto`` parameter also accepts other types:
 * A positive integer, used to create a bitstring of that many zero bits.
 * A file object, presumably opened in read-binary mode, from which the bitstring will be formed.
 * A ``bytearray`` object.
+* An ``array`` object. This is used after being converted to it's constituent byte data via its ``tostring`` method.
 * In Python 3 only, a ``bytes`` object. Note this won't work for Python 2.7 as ``bytes`` is just a synonym for ``str``.
 
 
diff --git a/doc/creation.rst b/doc/creation.rst
index 8aa1b53..df7e81a 100644
--- a/doc/creation.rst
+++ b/doc/creation.rst
@@ -37,7 +37,7 @@ Using the constructor
 ---------------------
 When initialising a bitstring you need to specify at most one initialiser. These will be explained in full below, but briefly they are:
 
-* ``auto`` : Either a specially formatted string, a list or tuple, a file object, integer, bytearray, bytes or another bitstring.
+* ``auto`` : Either a specially formatted string, a list or tuple, a file object, integer, bytearray, array, bytes or another bitstring.
 * ``bytes`` : A ``bytes`` object (a ``str`` in Python 2), for example read from a binary file.
 * ``hex``, ``oct``, ``bin``: Hexadecimal, octal or binary strings.
 * ``int``, ``uint``: Signed or unsigned bit-wise big-endian binary integers.
@@ -207,7 +207,7 @@ The auto initialiser
 --------------------
 The ``auto`` parameter is the first parameter in the :class:`__init__<Bits>` function and so the ``auto=`` can be omitted when using it. It accepts either a string, an iterable, another bitstring, an integer, a bytearray or a file object.
 
-Strings starting with ``0x`` or ``hex:`` are interpreted as hexadecimal, ``0o`` or ``oct:`` implies octal, and strings starting with ``0b`` or ``bin:`` are interpreted as binary. You can also initialise with the various integer initialisers as described above. If given another bitstring it will create a copy of it, (non string) iterables are interpreted as boolean arrays and file objects acts a source of binary data. Finally you can use an integer to create a zeroed bitstring of that num [...]
+Strings starting with ``0x`` or ``hex:`` are interpreted as hexadecimal, ``0o`` or ``oct:`` implies octal, and strings starting with ``0b`` or ``bin:`` are interpreted as binary. You can also initialise with the various integer initialisers as described above. If given another bitstring it will create a copy of it, (non string) iterables are interpreted as boolean arrays and file objects acts a source of binary data. An ``array`` object will be converted into its constituent bytes. Final [...]
 
     >>> fromhex = BitArray('0x01ffc9')
     >>> frombin = BitArray('0b01')
@@ -220,6 +220,7 @@ Strings starting with ``0x`` or ``hex:`` are interpreted as hexadecimal, ``0o``
     >>> fromfile = BitArray(f)
     >>> zeroed = BitArray(1000)
     >>> frombytes = BitArray(bytearray(b'xyz'))
+    >>> fromarray = BitArray(array.array('h', [3, 17, 10]))
  
 It can also be used to convert between the :class:`BitArray` and :class:`Bits` classes::
 
diff --git a/release_notes.txt b/release_notes.txt
index 664ec0f..b9a8cc1 100644
--- a/release_notes.txt
+++ b/release_notes.txt
@@ -3,6 +3,13 @@ bitstring module version history
 --------------------------------
 
 ---------------------------------------
+May 17th 2016: version 3.1.5 released
+---------------------------------------
+
+* Support initialisation from an array.
+* Added a separate LICENSE file.
+
+---------------------------------------
 March 19th 2016: version 3.1.4 released
 ---------------------------------------
 This is another bug fix release.
diff --git a/setup.py b/setup.py
index 60fdbf4..6f0300e 100644
--- a/setup.py
+++ b/setup.py
@@ -11,7 +11,7 @@ if sys.version_info[:2] < (2, 6):
                     'For Python 2.4 / 2.5 please use bitstring version 1.0 instead.')
 
 setup(name='bitstring',
-      version='3.1.4',
+      version='3.1.5',
       description='Simple construction, analysis and modification of binary data.',
       author='Scott Griffiths',
       author_email='dr.scottgriffiths at gmail.com',
diff --git a/test/test_bits.py b/test/test_bits.py
index 85e1fe4..3dd1840 100644
--- a/test/test_bits.py
+++ b/test/test_bits.py
@@ -5,6 +5,7 @@ import sys
 
 sys.path.insert(0, '..')
 import bitstring
+import array
 from bitstring import MmapByteArray
 from bitstring import Bits, BitArray, ConstByteStore, ByteStore
 
@@ -390,4 +391,33 @@ class WrongTypeBug(unittest.TestCase):
             a.append('0b1')
         self.assertEqual(type(a), Bits)
         b = bitstring.ConstBitStream(bitstring.BitStream())
-        self.assertEqual(type(b), bitstring.ConstBitStream)
\ No newline at end of file
+        self.assertEqual(type(b), bitstring.ConstBitStream)
+
+
+class InitFromArray(unittest.TestCase):
+
+    def testEmptyArray(self):
+        a = array.array('B')
+        b = Bits(a)
+        self.assertEqual(b.length, 0)
+
+    def testSingleByte(self):
+        a = array.array('B', b'\xff')
+        b = Bits(a)
+        self.assertEqual(b.length, 8)
+        self.assertEqual(b.hex, 'ff')
+
+    def testSignedShort(self):
+        a = array.array('h')
+        a.append(10)
+        a.append(-1)
+        b = Bits(a)
+        self.assertEqual(b.length, 32)
+        self.assertEqual(b.bytes, a.tostring())
+
+    def testDouble(self):
+        a = array.array('d', [0.0, 1.0, 2.5])
+        b = Bits(a)
+        self.assertEqual(b.length, 192)
+        c, d, e = b.unpack('3*floatne:64')
+        self.assertEqual((c, d, e), (0.0, 1.0, 2.5))
\ No newline at end of file
diff --git a/test/test_bitstring.py b/test/test_bitstring.py
index fec3dca..c3c4554 100644
--- a/test/test_bitstring.py
+++ b/test/test_bitstring.py
@@ -12,7 +12,7 @@ import copy
 
 class ModuleData(unittest.TestCase):
     def testVersion(self):
-        self.assertEqual(bitstring.__version__, '3.1.4')
+        self.assertEqual(bitstring.__version__, '3.1.5')
 
     def testAll(self):
         exported = ['ConstBitArray', 'ConstBitStream', 'BitStream', 'BitArray',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-bitstring.git



More information about the debian-science-commits mailing list