Skip to content

Commit

Permalink
Python 2.4 support. closes pika#4
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyg committed May 17, 2010
1 parent 1c45111 commit 2d25505
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions THANKS
@@ -0,0 +1 @@
Github's enlavin (Miguel Hernandez Martos) and itkach for patches for python 2.4 support.
4 changes: 2 additions & 2 deletions examples/demo_receive.py
Expand Up @@ -56,12 +56,12 @@
import asyncore

conn = pika.AsyncoreConnection(pika.ConnectionParameters(
sys.argv[1] if len(sys.argv) > 1 else '127.0.0.1',
(len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1',
credentials = pika.PlainCredentials('guest', 'guest')))

print 'Connected to %r' % (conn.server_properties,)

qname = sys.argv[2] if len(sys.argv) > 2 else 'test'
qname = (len(sys.argv) > 2) and sys.argv[2] or 'test'

ch = conn.channel()
ch.queue_declare(queue=qname, durable=True, exclusive=False, auto_delete=False)
Expand Down
2 changes: 1 addition & 1 deletion examples/demo_send.py
Expand Up @@ -56,7 +56,7 @@
import asyncore

conn = pika.AsyncoreConnection(pika.ConnectionParameters(
sys.argv[1] if len(sys.argv) > 1 else '127.0.0.1',
(len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1',
credentials=pika.PlainCredentials('guest', 'guest')))

ch = conn.channel()
Expand Down
10 changes: 10 additions & 0 deletions pika/__init__.py
Expand Up @@ -63,3 +63,13 @@

def repl_channel(host = '127.0.0.1', *args):
return BlockingConnection(ConnectionParameters(host, *args)).channel()

# Python 2.4 support: add struct.unpack_from if it's missing.
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
9 changes: 7 additions & 2 deletions pika/simplebuffer.py
Expand Up @@ -60,6 +60,11 @@
except ImportError:
import StringIO

# Python 2.4 support: os lacks SEEK_END and friends
try:
getattr(os, "SEEK_END")
except AttributeError:
os.SEEK_SET, os.SEEK_CUR, os.SEEK_END = range(3)

class SimpleBuffer:
"""
Expand Down Expand Up @@ -145,7 +150,7 @@ def flush(self):

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

def __len__(self):
return self.size
Expand All @@ -155,4 +160,4 @@ def __str__(self):

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 '')

0 comments on commit 2d25505

Please sign in to comment.