On Fri, Mar 30, 2012 at 04:56:38pm +0200, Iustin Pop wrote:
> > Would it make sense for the IP pool functionality to only be available
> > on Python 2.5? In this manner, IP pool could still be included as an
> > option, due to its usefulness and applicability to a wide range of
> > scenarios.
> 
> Yes, if someone implements handling code for that :)
> 
> The problem is that while the hconfd is a standalone binary that can
> easily be enabled/disabled by itself, the address pool patches afects
> masterd itself. So we or you would have to write some very careful code
> that enables disables this based on configure-time parameters (or python
> version).
> 

Dear Iustin,

As an alternative, I experimented a bit with porting python-bitarray 0.3.5-1
from Debian Sid (http://packages.debian.org/sid/python-bitarray) to Python 2.4.

The main problem is the use of type Py_Ssize_t in bitarray's C module,
as well as a few inline "if" statements.

Python 2.4:
$ virtualenv --no-site-packages --python=python2.4 ~/venv
$ . ~/venv/bin/activate
$ python2.4 ~/venv/lib/python2.4/site-packages/bitarray/test_bitarray.py
...........................................................................................
----------------------------------------------------------------------
Ran 91 tests in 2.647s

OK

Python 2.6:

$ virtualenv --no-site-packages --python=python2.6 ~/venv2
$ . ~/venv2/bin/activate
$ python2.6 ~/venv2/lib/python2.6/site-packages/bitarray/test_bitarray.py
...........................................................................................
----------------------------------------------------------------------
Ran 91 tests in 2.255s

OK

It's still a work in progress, there are a few rough edges, including my not
knowing what to do with PyIndex_Check(), and only having tested on an
i386 sid system.

How would you propose to proceed?
Would it make sense to submit this to the author of bitarray? 
Any comments are more than welcome.

Vangelis.

diff -ur bitarray-original/python-bitarray-0.3.5/bitarray/_bitarray.c 
bitarray/python-bitarray-0.3.5/bitarray/_bitarray.c
--- bitarray-original/python-bitarray-0.3.5/bitarray/_bitarray.c        
2009-04-06 06:37:36.000000000 +0300
+++ bitarray/python-bitarray-0.3.5/bitarray/_bitarray.c 2012-03-30 
20:20:46.000000000 +0300
@@ -10,6 +10,31 @@
 #define PY_SSIZE_T_CLEAN
 #include "Python.h"
 
+#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
+/*
+ * Python 2.4 does not support Py_Ssize_t,
+ * 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)
+{
+       /* FIXME please */
+       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)
@@ -868,7 +893,7 @@
     bitarrayobject *xa;
     idx_t p, n;
 
-    if (!PyArg_ParseTuple(args, "On:_search", &x, &limit))
+    if (!PyArg_ParseTuple(args, "O" PY_SSIZE_T_FMT ":_search", &x, &limit))
         return NULL;
 
     assert(bitarray_Check(x));
@@ -1167,7 +1192,7 @@
     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);
diff -ur bitarray-original/python-bitarray-0.3.5/bitarray/test_bitarray.py 
bitarray/python-bitarray-0.3.5/bitarray/test_bitarray.py
--- bitarray-original/python-bitarray-0.3.5/bitarray/test_bitarray.py   
2009-04-01 23:00:24.000000000 +0300
+++ bitarray/python-bitarray-0.3.5/bitarray/test_bitarray.py    2012-03-30 
19:51:48.000000000 +0300
@@ -11,6 +11,19 @@
 from random import randint
 from cStringIO import StringIO
 
+def any(iterable):
+    for element in iterable:
+        if element:
+            return True
+    return False
+
+
+def all(iterable):
+    for element in iterable:
+        if not element:
+            return False
+    return True
+
 
 if __name__ == '__main__':
     from __init__ import bitarray, bits2bytes
@@ -28,14 +41,17 @@
     def randombitarrays(self):
         for n in range(25) + [randint(1000, 2000)]:
             yield bitarray([randint(0, 1) for d in xrange(n)],
-                           endian='big' if randint(0, 1) else 'little')
+                           endian=['little', 'big'][randint(0,1)])
 
     def randomlists(self):
         for n in range(25) + [randint(1000, 2000)]:
             yield [bool(randint(0, 1)) for d in xrange(n)]
 
     def rndsliceidx(self, length):
-        return randint(-2*length, 2*length-1) if randint(0, 1) == 1 else None
+        if randint(0,1) == 1:
+            return randint(-2*length, 2*length-1)
+        else:
+            return None
 
     def slicelen(self, r, length):
         return getIndicesEx(r, length)[-1]
@@ -65,15 +81,29 @@
         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
+    else:
+        defstart = 0
+    if step < 0:
+        defstop = -1
+    else:
+        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:
+            if step < 0:
+                start = -1
+            else:
+                start = 0
+        if start >= length:
+            if step < 0:
+                start = length-1
+            else:
+                start = length
 
     if r.stop is None:
         stop = defstop
@@ -109,8 +139,11 @@
         self.assertRaises(ValueError, bits2bytes, -924L)
 
         for n in xrange(1000):
-            self.assertEqual(bits2bytes(n),
-                             0 if n==0 else ((n - 1) / 8 + 1));
+            if n == 0:
+                assertval = 0
+            else:
+                assertval = ((n - 1) / 8 + 1)
+            self.assertEqual(bits2bytes(n), assertval)
 
         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),
@@ -221,7 +254,7 @@
 
         for n in xrange(50):
             lst = [bool(randint(0, 1)) for d in xrange(n)]
-            s = ''.join('1' if x else '0' for x in lst)
+            s = ''.join(map(lambda x: ['0', '1'][x == True], lst))
             a = bitarray(s)
             self.assertEqual(a.tolist(), lst)
             self.check_obj(a)
@@ -1010,7 +1043,8 @@
             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(map(lambda x: ['0', '1'][x == True], b)))
                 self.assertEqual(id(c), idc)
                 self.assertEqual(c.tolist(), a + b)
                 self.check_obj(c)
@@ -1822,7 +1856,10 @@
 
 
 if __name__ == '__main__':
-    verbosity = 2 if 'v' in sys.argv else 1
+    if 'v' in sys.argv:
+        verbosity = 2
+    else:
+        verbosity = 1
     if 'm' in sys.argv:
         check_memory_leaks(verbosity)
     else:
diff -ur bitarray-original/python-bitarray-0.3.5/setup.py 
bitarray/python-bitarray-0.3.5/setup.py
--- bitarray-original/python-bitarray-0.3.5/setup.py    2009-01-15 
19:21:37.000000000 +0200
+++ bitarray/python-bitarray-0.3.5/setup.py     2012-03-30 20:16:01.000000000 
+0300
@@ -3,8 +3,8 @@
 from os.path import abspath, join
 from distutils.core import setup, Extension
 
-if sys.version_info[:2] < (2, 5):
-    raise Exception('bitarray requires Python 2.5 or greater.')
+if sys.version_info[:2] < (2, 4):
+    raise Exception('bitarray requires Python 2.4 or greater.')
 
 kwds = {}
 

Attachment: signature.asc
Description: Digital signature

Reply via email to