Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
  • 8 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
1  AUTHORS
@@ -6,6 +6,7 @@ suggestions, and discussions:
- Roland Puntaier (Porting to Python 3.x)
- Daniel Stutzbach (for help with reference counts)
- Lluís Pàmies (C level itersearch)
+ - Vangelis Koukis (Python 2.4 support)
- Kris Kennaway
- Travis Oliphant
- Gael Varoquaux
View
1  CHANGE_LOG
@@ -1,5 +1,6 @@
2012-XX-XX 0.8.0:
-------------------
+ * add Python 2.4 support
* add (module level) function bitdiff for calculating the difference
between two bitarrays
View
11 README.rst
@@ -13,15 +13,15 @@ such as portable bitmap image files (.pbm). Also, when dealing with
compressed data which uses variable bit length encoding, you may find
this module useful.
-Requires Python 2.5 or greater (including Py3k),
-see `PEP 353 <http://www.python.org/dev/peps/pep-0353/>`_.
-
Key features
------------
- * On 32bit machines, a bitarray object can contain up to 2^34 elements,
- that is 16 Gbits (on 64bit machines up to 2^63 elements in theory).
+ * On 32bit systems, a bitarray object can contain up to 2^34 elements,
+ that is 16 Gbits (on 64bit machines up to 2^63 elements in theory --
+ on Python 2.4 only 2^31 elements,
+ see `PEP 353 <http://www.python.org/dev/peps/pep-0353/>`_
+ (added in Python 2.5)).
* All crucial functionality implemented in C.
@@ -539,6 +539,7 @@ Change log
2012-XX-XX 0.8.0:
+ * add Python 2.4 support
* add (module level) function bitdiff for calculating the difference
between two bitarrays
View
4 bitarray/__init__.py
@@ -8,7 +8,7 @@
Author: Ilan Schnell
"""
-from ._bitarray import _bitarray, bitdiff, bits2bytes, _sysinfo
+from bitarray._bitarray import _bitarray, bitdiff, bits2bytes, _sysinfo
__version__ = '0.8.0'
@@ -124,5 +124,5 @@ def test(verbosity=1, repeat=1):
Run self-test, and return unittest.runner.TextTestResult object.
"""
- from . import test_bitarray
+ from bitarray import test_bitarray
return test_bitarray.run(verbosity=verbosity, repeat=repeat)
View
23 bitarray/_bitarray.c
@@ -23,6 +23,25 @@
#define Py_TPFLAGS_HAVE_WEAKREFS 0
#endif
+#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
+/* Py_ssize_t was introduced in Python 2.5, substitute long for it */
+typedef long Py_ssize_t;
+#define PY_SSIZE_T_MAX LONG_MAX
+#define PY_SSIZE_T_MIN LONG_MIN
+Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)
+{
+ return PyLong_AsLong(o);
+}
+int PyIndex_Check(PyObject *o)
+{
+ return 0;
+}
+#define PY_SSIZE_T_FMT "l"
+#else
+/* Python 2.5 and up uses 'n' as the format char for Py_ssize_t */
+#define PY_SSIZE_T_FMT "n"
+#endif
+
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 6
/* backward compatibility with Python 2.5 */
#define Py_TYPE(ob) (((PyObject *) (ob))->ob_type)
@@ -1024,7 +1043,7 @@ bitarray_search(bitarrayobject *self, PyObject *args)
bitarrayobject *xa;
idx_t p;
- if (!PyArg_ParseTuple(args, "O|n:_search", &x, &limit))
+ if (!PyArg_ParseTuple(args, "O|" PY_SSIZE_T_FMT ":_search", &x, &limit))
return NULL;
if (!bitarray_Check(x)) {
@@ -1412,7 +1431,7 @@ bitarray_fromfile(bitarrayobject *self, PyObject *args)
idx_t t, p;
long cur;
- if (!PyArg_ParseTuple(args, "O|n:fromfile", &f, &nbytes))
+ if (!PyArg_ParseTuple(args, "O|" PY_SSIZE_T_FMT ":fromfile", &f, &nbytes))
return NULL;
fp = PyFile_AsFile(f);
View
41 bitarray/test_bitarray.py
@@ -41,7 +41,7 @@ class Util(object):
def randombitarrays(self):
for n in list(range(25)) + [randint(1000, 2000)]:
- a = bitarray(endian='big' if randint(0, 1) else 'little')
+ a = bitarray(endian=['little', 'big'][randint(0, 1)])
a.frombytes(os.urandom(bits2bytes(n)))
del a[n:]
yield a
@@ -51,7 +51,10 @@ def randomlists(self):
yield [bool(randint(0, 1)) for d in range(n)]
def rndsliceidx(self, length):
- return randint(-2*length, 2*length-1) if randint(0, 1) == 1 else None
+ if randint(0, 1):
+ return None
+ else:
+ return randint(-2 * length, 2 * length - 1)
def slicelen(self, r, length):
return getIndicesEx(r, length)[-1]
@@ -86,15 +89,19 @@ def getIndicesEx(r, length):
if step == 0:
raise ValueError("slice step cannot be zero")
- defstart = length-1 if step < 0 else 0
- defstop = -1 if step < 0 else length
+ if step < 0:
+ defstart = length - 1
+ defstop = -1
+ else:
+ defstart = 0
+ defstop = length
if r.start is None:
start = defstart
else:
if start < 0: start += length
- if start < 0: start = -1 if step < 0 else 0
- if start >= length: start = length-1 if step < 0 else length
+ if start < 0: start = [0, -1][step < 0]
+ if start >= length: start = [length, length - 1][step < 0]
if r.stop is None:
stop = defstop
@@ -106,9 +113,9 @@ def getIndicesEx(r, length):
if (step < 0 and stop >= length) or (step > 0 and start >= stop):
slicelength = 0
elif step < 0:
- slicelength = (stop-start+1) / step + 1
+ slicelength = (stop - start + 1) / step + 1
else:
- slicelength = (stop-start-1) / step + 1
+ slicelength = (stop - start - 1) / step + 1
if slicelength < 0:
slicelength = 0
@@ -149,9 +156,9 @@ def test_bits2bytes(self):
self.assertRaises(ValueError, bits2bytes, -1)
self.assertRaises(ValueError, bits2bytes, -924)
- for n in range(1000):
- self.assertEqual(bits2bytes(n),
- 0 if n==0 else ((n - 1) // 8 + 1));
+ self.assertEqual(bits2bytes(0), 0)
+ for n in range(1, 1000):
+ self.assertEqual(bits2bytes(n), (n - 1) // 8 + 1)
for n, m in [(0, 0), (1, 1), (2, 1), (7, 1), (8, 1), (9, 2),
(10, 2), (15, 2), (16, 2), (64, 8), (65, 9),
@@ -265,7 +272,7 @@ def test_01(self):
for n in range(50):
lst = [bool(randint(0, 1)) for d in range(n)]
- s = ''.join('1' if x else '0' for x in lst)
+ s = ''.join([['0', '1'][x] for x in lst])
a = bitarray(s)
self.assertEqual(a.tolist(), lst)
self.check_obj(a)
@@ -724,6 +731,9 @@ def test_all(self):
a = bitarray()
self.assertTrue(a.all())
+ if sys.version_info[:2] < (2, 5):
+ return
+
for a in self.randombitarrays():
self.assertEqual(all(a), a.all())
self.assertEqual(all(a.tolist()), a.all())
@@ -733,6 +743,9 @@ def test_any(self):
a = bitarray()
self.assertFalse(a.any())
+ if sys.version_info[:2] < (2, 5):
+ return
+
for a in self.randombitarrays():
self.assertEqual(any(a), a.any())
self.assertEqual(any(a.tolist()), a.any())
@@ -1164,7 +1177,7 @@ def test_string01(self):
for b in self.randomlists():
c = bitarray(a)
idc = id(c)
- c.extend(''.join(('1' if x else '0') for x in b))
+ c.extend(''.join(['0', '1'][x] for x in b))
self.assertEqual(id(c), idc)
self.assertEqual(c.tolist(), a + b)
self.check_obj(c)
@@ -1690,6 +1703,8 @@ def test_cPickle(self):
self.assertEQUAL(a, b)
def test_shelve(self):
+ if sys.version_info[:2] < (2, 5):
+ return
import shelve, hashlib
d = shelve.open(self.tmpfname)
View
6 setup.py
@@ -1,6 +1,9 @@
import re
from os.path import join
-from distutils.core import setup, Extension
+try:
+ from setuptools import setup, Extension
+except ImportError:
+ from distutils.core import setup, Extension
kwds = {}
@@ -25,6 +28,7 @@
"Operating System :: OS Independent",
"Programming Language :: C",
"Programming Language :: Python :: 2",
+ "Programming Language :: Python :: 2.4",
"Programming Language :: Python :: 2.5",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
View
2  setupegg.py
@@ -1,2 +0,0 @@
-from setuptools import setup, Extension
-execfile('setup.py')

No commit comments for this range

Something went wrong with that request. Please try again.