Skip to content
This repository

Python 2.4 Compatibility #4

Closed
itkach opened this Issue · 4 comments

3 participants

itkach Miguel Hernandez Martos Tony Garnock-Jones
itkach

simplebuffer.py uses Python 2.5 syntax, this can be "fixed" to work on 2.4 with the following patch:

diff --git a/pika/simplebuffer.py b/pika/simplebuffer.py
index 08abf8c..956d8d9 100644
--- a/pika/simplebuffer.py
+++ b/pika/simplebuffer.py
@@ -12,6 +12,7 @@ try:
 except ImportError:
     import StringIO

+SEEK_END = 2

 class SimpleBuffer:
     """
@@ -37,7 +38,7 @@ class SimpleBuffer:
         self.buf = StringIO.StringIO()
         if data is not None:
             self.write(data)
-        self.buf.seek(0, os.SEEK_END)
+        self.buf.seek(0, SEEK_END)

     def write(self, *data_strings):
         ''' Append given strings to the buffer. '''
@@ -59,7 +60,7 @@ class SimpleBuffer:
         else:
             data = self.buf.read(size)

-        self.buf.seek(0, os.SEEK_END)
+        self.buf.seek(0, SEEK_END)
         return data

     def consume(self, size):
@@ -84,7 +85,7 @@ class SimpleBuffer:
         ''' Faster way of sending buffer data to socket 'sd'. '''
         self.buf.seek(self.offset)
         r = sd.send( self.buf.read() )
-        self.buf.seek(0, os.SEEK_END)
+        self.buf.seek(0, SEEK_END)
         self.offset += r
         self.size -= r
         if self.offset > 524288 and self.size == 0:
@@ -97,7 +98,7 @@ class SimpleBuffer:

     def __nonzero__(self):
         ''' Are we empty? '''
-        return True if self.size else False
+        return bool(self.size)

     def __len__(self):
         return self.size
@@ -106,5 +107,9 @@ class SimpleBuffer:
         return self.__repr__()

     def __repr__(self):
+        if self.size > 16:
+            s = '...'
+        else:
+            s = ''
         return '<SimpleBuffer of %i bytes, %i total size, %r%s>' % \
-                    (self.size, self.size + self.offset, self.read(16), '...' if self.size > 16 else '')
+                    (self.size, self.size + self.offset, self.read(16), s)
itkach

Hm... I spoke too soon - there's more to it than just simplebuffer.py. After applying the patch I'm getting another error:

error: uncaptured python exception, closing channel (exceptions.AttributeError:'module' object has no attribute 'unpack_from' [/usr/lib/python2.4/asyncore.py|read|69] [/usr/lib/python2.4/asyncore.py|handle_read_event|391] [/usr/lib/python2.4/site-packages/pika/asyncore_adapter.py|handle_read|38] [/usr/lib/python2.4/site-packages/pika/connection.py|on_data_available|216] [/usr/lib/python2.4/site-packages/pika/codec.py|handle_input|112] [/usr/lib/python2.4/site-packages/pika/codec.py|_waiting_for_header|125])

Looks struct.unpack_from() which was added in Python 2.5 is used in many places...
Is there any chance the code can be changed to be Python 2.4 compatible? For those using CentOS there's no simple way to upgrade to a newer Python version.

Miguel Hernandez Martos

Based on your patch I did some more monkeypatching and I have been able to run under python 2.4 apparently without issues:

diff --git a/pika/__init__.py b/pika/__init__.py
index 0a21b6f..fa8c502 100644
--- a/pika/__init__.py
+++ b/pika/__init__.py
@@ -63,3 +63,13 @@ from pika.blocking_adapter import \

 def repl_channel(host = '127.0.0.1', *args):
     return BlockingConnection(ConnectionParameters(host, *args)).channel()
+
+try:
+    import struct
+    getattr(struct, "unpack_from")
+except AttributeError:
+    def _unpack_from(fmt, buf, offset=0):
+        slice = buffer(buf, offset, struct.calcsize(fmt))
+        return struct.unpack(fmt, slice)
+    struct.unpack_from = _unpack_from
+
diff --git a/pika/simplebuffer.py b/pika/simplebuffer.py
index 2db1d0b..5a9dcae 100644
--- a/pika/simplebuffer.py
+++ b/pika/simplebuffer.py
@@ -60,6 +60,11 @@ try:
 except ImportError:
     import StringIO

+try:
+    getattr(os, "SEEK_END")
+except AttributeError:
+    os.SEEK_SET, os.SEEK_CUR, os.SEEK_END = range(3)
+

 class SimpleBuffer:
     """
@@ -145,7 +150,7 @@ class SimpleBuffer:

     def __nonzero__(self):
         """ Are we empty? """
-        return True if self.size else False
+        return self.size > 0

     def __len__(self):
         return self.size
@@ -155,4 +160,4 @@ class SimpleBuffer:

     def __repr__(self):
         return '<SimpleBuffer of %i bytes, %i total size, %r%s>' % \
-                    (self.size, self.size + self.offset, self.read(16), '...' if self.size > 16 else '')
+                    (self.size, self.size + self.offset, self.read(16), (self.size > 16) and '...' or '')
Tony Garnock-Jones
Owner
tonyg commented

Python 2.4 support. closed by 2d25505

Tony Garnock-Jones
Owner
tonyg commented

Thanks for the patches! Gratefully applied.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.