summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsk Solem <ask@celeryproject.org>2012-11-13 15:39:04 +0000
committerAsk Solem <ask@celeryproject.org>2012-11-13 16:28:19 +0000
commit831fd07382da2398fcdd7ab3b76469902755d785 (patch)
treef66bbe663abfeb3934db32fa47d1c37130e7f3e9
parent38146f5df875ab0f0a618606cd79fe81e33fbe28 (diff)
downloadpy-amqp-831fd07382da2398fcdd7ab3b76469902755d785.tar.gz
Use more str.format
-rw-r--r--amqp/abstract_channel.py2
-rw-r--r--amqp/connection.py4
-rw-r--r--amqp/exceptions.py8
-rw-r--r--amqp/method_framing.py4
-rw-r--r--amqp/serialization.py23
-rw-r--r--amqp/transport.py8
-rwxr-xr-xfuntests/test_channel.py57
-rw-r--r--setup.py5
8 files changed, 61 insertions, 50 deletions
diff --git a/amqp/abstract_channel.py b/amqp/abstract_channel.py
index f13c6a2..07f18db 100644
--- a/amqp/abstract_channel.py
+++ b/amqp/abstract_channel.py
@@ -82,7 +82,7 @@ class AbstractChannel(object):
amqp_method = self._METHOD_MAP[method_sig]
except KeyError:
raise AMQPNotImplementedError(
- 'Unknown AMQP method %r' % method_sig, method_sig)
+ 'Unknown AMQP method {0!r}'.format((method_sig, method_sig))
if content is None:
return amqp_method(self, args)
diff --git a/amqp/connection.py b/amqp/connection.py
index 3348626..3494812 100644
--- a/amqp/connection.py
+++ b/amqp/connection.py
@@ -185,7 +185,7 @@ class Connection(AbstractChannel):
return self._avail_channel_ids.pop()
except IndexError:
raise ResourceError(
- 'No free channel ids, current=%d, channel_max=%d' % (
+ 'No free channel ids, current={0}, channel_max={1}'.format(
len(self.channels), self.channel_max), (20, 10))
def _wait_method(self, channel_id, allowed_methods):
@@ -287,7 +287,7 @@ class Connection(AbstractChannel):
if amqp_method is None:
raise AMQPNotImplementedError(
- 'Unknown AMQP method %r' % method_sig, method_sig)
+ 'Unknown AMQP method {0!r}'.format((method_sig, method_sig))
if content is None:
return amqp_method(channel, args)
diff --git a/amqp/exceptions.py b/amqp/exceptions.py
index 52af080..2ea1496 100644
--- a/amqp/exceptions.py
+++ b/amqp/exceptions.py
@@ -47,9 +47,11 @@ class AMQPError(Exception):
reply_text, method_sig, self.method_name)
def __str__(self):
- return '%s: (%s) %s' % (
- self.method_name or self.method_sig,
- self.reply_code, self.reply_text)
+ return '{0.method}: ({0.reply_code}) {0.reply_text}'.format(self)
+
+ @property
+ def method(self):
+ return self.method_name or self.method_sig
class ConnectionError(AMQPError):
diff --git a/amqp/method_framing.py b/amqp/method_framing.py
index dd80039..7f06b30 100644
--- a/amqp/method_framing.py
+++ b/amqp/method_framing.py
@@ -115,8 +115,8 @@ class MethodReader(object):
if frame_type not in (self.expected_types[channel], 8):
self.queue.put((
channel,
- UnexpectedFrame(505,
- 'Received frame type %s while expecting type: %s' % (
+ UnexpectedFrame(
+ 'Received frame {0} while expecting type: {1}'.format(
frame_type, self.expected_types[channel]))
))
elif frame_type == 1:
diff --git a/amqp/serialization.py b/amqp/serialization.py
index 54f59bb..5d530c5 100644
--- a/amqp/serialization.py
+++ b/amqp/serialization.py
@@ -160,7 +160,9 @@ class AMQPReader(object):
elif ftype == 100:
val = table_data.read_float()
else:
- raise FrameSyntaxError('Unknown table item type: %r' % ftype)
+ raise FrameSyntaxError(
+ 'Unknown value in table: {0!r} ({1!r})'.format(
+ ftype, type(ftype)))
result[name] = val
return result
@@ -231,28 +233,32 @@ class AMQPWriter(object):
def write_octet(self, n):
"""Write an integer as an unsigned 8-bit value."""
if n < 0 or n > 255:
- raise FrameSyntaxError('Octet %r out of range 0..255' % (n, ))
+ raise FrameSyntaxError(
+ 'Octet {0!r} out of range 0..255'.format(n))
self._flushbits()
self.out.write(pack('B', n))
def write_short(self, n):
"""Write an integer as an unsigned 16-bit value."""
if n < 0 or n > 65535:
- raise FrameSyntaxError('Octet %r out of range 0..65535' % (n, ))
+ raise FrameSyntaxError(
+ 'Octet {0!r} out of range 0..65535'.format(n))
self._flushbits()
self.out.write(pack('>H', int(n)))
def write_long(self, n):
"""Write an integer as an unsigned2 32-bit value."""
if n < 0 or n >= 2 ** 32:
- raise FrameSyntaxError('Octet %r out of range 0..2**31-1' % (n, ))
+ raise FrameSyntaxError(
+ 'Octet {0!r} out of range 0..2**31-1'.format(n))
self._flushbits()
self.out.write(pack('>I', n))
def write_longlong(self, n):
"""Write an integer as an unsigned 64-bit value."""
if n < 0 or n >= 2 ** 64:
- raise FrameSyntaxError('Octet %r out of range 0..2**64-1' % (n, ))
+ raise FrameSyntaxError(
+ 'Octet {0!r} out of range 0..2**64-1'.format(n))
self._flushbits()
self.out.write(pack('>Q', n))
@@ -266,7 +272,8 @@ class AMQPWriter(object):
if isinstance(s, string):
s = s.encode('utf-8')
if len(s) > 255:
- raise FrameSyntaxError('String too long (%r)' % (len(s), ))
+ raise FrameSyntaxError(
+ 'Shortstring overflow ({0} > 255)'.format(len(s)))
self.write_octet(len(s))
self.out.write(s)
@@ -319,7 +326,9 @@ class AMQPWriter(object):
table_data.write(byte(70)) # 'F'
table_data.write_table(v)
else:
- raise FrameSyntaxError('%r not serializable in AMQP' % (v, ))
+ raise FrameSyntaxError(
+ 'Table type {0!r} not handled by amqp: {1!r}'.format(
+ type(v), v))
table_data = table_data.getvalue()
self.write_long(len(table_data))
self.out.write(table_data)
diff --git a/amqp/transport.py b/amqp/transport.py
index 3465494..2f46d0f 100644
--- a/amqp/transport.py
+++ b/amqp/transport.py
@@ -57,7 +57,7 @@ class _AbstractTransport(object):
"""Common superclass for TCP and SSL transports"""
def __init__(self, host, connect_timeout):
- msg = 'socket.getaddrinfo() for %s returned an empty list' % host
+ msg = None
port = AMQP_PORT
m = IPV6_LITERAL.match(host)
@@ -78,7 +78,8 @@ class _AbstractTransport(object):
self.sock = socket.socket(af, socktype, proto)
self.sock.settimeout(connect_timeout)
self.sock.connect(sa)
- except socket.error as msg:
+ except socket.error as exc:
+ msg = exc
self.sock.close()
self.sock = None
continue
@@ -86,6 +87,7 @@ class _AbstractTransport(object):
if not self.sock:
# Didn't connect, return the most recent error message
+ msg = msg or 'getaddrinfo() for {0} is empty list'.format(host)
raise socket.error(msg)
self.sock.settimeout(None)
@@ -140,7 +142,7 @@ class _AbstractTransport(object):
return frame_type, channel, payload
else:
raise UnexpectedFrame(
- 'Framing Error, received 0x%02x while expecting 0xce' % ch)
+ 'Received 0x{0:02x} while expecting 0xce'.format(ch))
def write_frame(self, frame_type, channel, payload):
"""Write out an AMQP frame."""
diff --git a/funtests/test_channel.py b/funtests/test_channel.py
index 1ceb1ae..68d37a7 100755
--- a/funtests/test_channel.py
+++ b/funtests/test_channel.py
@@ -72,7 +72,6 @@ class TestChannel(unittest.TestCase):
n = self.ch.queue_delete()
self.assertEqual(n, 0)
-
def test_encoding(self):
my_routing_key = 'unittest.test_queue'
@@ -123,7 +122,9 @@ class TestChannel(unittest.TestCase):
#
# Plain string (bytes in Python 3.x) with bogus encoding
#
- test_bytes = u'hello w\xd6rld'.encode('latin_1') # don't really care about latin_1, just want bytes
+
+ # don't really care about latin_1, just want bytes
+ test_bytes = u'hello w\xd6rld'.encode('latin_1')
msg = Message(test_bytes, content_encoding='I made this up')
self.ch.basic_publish(msg, 'amq.direct', routing_key=my_routing_key)
msg2 = self.ch.basic_get(qname, no_ack=True)
@@ -258,63 +259,63 @@ class TestChannel(unittest.TestCase):
# 3 of the 4 messages we sent should have been returned
#
self.assertEqual(self.ch.returned_messages.qsize(), 3)
-
+
def test_exchange_bind(self):
"""Test exchange binding.
Network configuration is as follows (-> is forwards to :
source_exchange -> dest_exchange -> queue
The test checks that once the message is publish to the
- destination exchange(unittest.topic_dest) it is delivered to the queue.
+ destination exchange(unittest.topic_dest) it is delivered to the queue.
"""
-
+
test_routing_key = 'unit_test__key'
dest_exchange = 'unittest.topic_dest_bind'
- source_exchange = 'unittest.topic_source_bind'
-
+ source_exchange = 'unittest.topic_source_bind'
+
self.ch.exchange_declare(dest_exchange, 'topic', auto_delete=True)
self.ch.exchange_declare(source_exchange, 'topic', auto_delete=True)
-
+
qname, _, _ = self.ch.queue_declare()
- self.ch.exchange_bind(destination = dest_exchange,
- source = source_exchange,
+ self.ch.exchange_bind(destination = dest_exchange,
+ source = source_exchange,
routing_key = test_routing_key)
-
- self.ch.queue_bind(qname, dest_exchange,
+
+ self.ch.queue_bind(qname, dest_exchange,
routing_key=test_routing_key)
-
+
msg = Message('unittest message',
content_type='text/plain',
application_headers={'foo': 7, 'bar': 'baz'})
-
-
- self.ch.basic_publish(msg, source_exchange,
+
+
+ self.ch.basic_publish(msg, source_exchange,
routing_key = test_routing_key)
-
+
msg2 = self.ch.basic_get(qname, no_ack=True)
self.assertEqual(msg, msg2)
-
+
def test_exchange_unbind(self):
dest_exchange = 'unittest.topic_dest_unbind'
source_exchange = 'unittest.topic_source_unbind'
test_routing_key = 'unit_test__key'
-
- self.ch.exchange_declare(dest_exchange,
+
+ self.ch.exchange_declare(dest_exchange,
'topic', auto_delete=True)
- self.ch.exchange_declare(source_exchange,
+ self.ch.exchange_declare(source_exchange,
'topic', auto_delete=True)
-
- self.ch.exchange_bind(destination = dest_exchange,
- source = source_exchange,
+
+ self.ch.exchange_bind(destination = dest_exchange,
+ source = source_exchange,
routing_key = test_routing_key)
-
- self.ch.exchange_unbind(destination = dest_exchange,
+
+ self.ch.exchange_unbind(destination = dest_exchange,
source = source_exchange,
routing_key = test_routing_key)
-
+
+
def main():
suite = unittest.TestLoader().loadTestsFromTestCase(TestChannel)
unittest.TextTestRunner(**settings.test_args).run(suite)
-
if __name__ == '__main__':
main()
diff --git a/setup.py b/setup.py
index d5ed284..4bbad58 100644
--- a/setup.py
+++ b/setup.py
@@ -68,8 +68,7 @@ pats = {re_meta: add_default,
re_vers: add_version,
re_doc: add_doc}
here = os.path.abspath(os.path.dirname(__file__))
-meta_fh = open(os.path.join(here, 'amqp/__init__.py'))
-try:
+with open(os.path.join(here, 'amqp/__init__.py')) as meta_fh:
meta = {}
for line in meta_fh:
if line.strip() == '# -eof meta-':
@@ -78,8 +77,6 @@ try:
m = pattern.match(line.strip())
if m:
meta.update(handler(m))
-finally:
- meta_fh.close()
# -*- Installation Requires -*-