Skip to content

Commit

Permalink
Make sure __repr__ and __str__ returns bytes on Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ask committed Apr 11, 2016
1 parent 4bf2256 commit 82e05c0
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 74 deletions.
7 changes: 6 additions & 1 deletion kombu/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .connection import maybe_channel
from .exceptions import NotBoundError
from .five import python_2_unicode_compatible
from .utils import ChannelPromise

__all__ = ['Object', 'MaybeChannelBound']
Expand Down Expand Up @@ -56,6 +57,7 @@ def __copy__(self):
return self.__class__(**self.as_dict())


@python_2_unicode_compatible
class MaybeChannelBound(Object):
"""Mixin for classes that can be bound to an AMQP channel."""
_channel = None
Expand Down Expand Up @@ -94,7 +96,10 @@ def when_bound(self):
"""Callback called when the class is bound."""
pass

def __repr__(self, item=''):
def __repr__(self):
return self._repr_entity(type(self).__name__)

def _repr_entity(self, item=''):
item = item or type(self).__name__
if self.is_bound:
return '<{0} bound to chan:{1}>'.format(
Expand Down
40 changes: 22 additions & 18 deletions kombu/async/aws/connection.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

try: # pragma: no cover
from email import message_from_file
from email.mime.message import MIMEMessage
except ImportError: # pragma: no cover
from mimetools import Message as MIMEMessage # noqa
from io import BytesIO

def message_from_file(m): # noqa
return m
from vine import promise, transform

from io import BytesIO
from kombu.async.http import Headers, Request, get_client
from kombu.five import items, python_2_unicode_compatible

from .ext import (
boto, AWSAuthConnection, AWSQueryConnection, XmlHandler, ResultSet,
)

try:
from urllib.parse import urlunsplit
except ImportError:
from urlparse import urlunsplit # noqa
from xml.sax import parseString as sax_parse

from vine import promise, transform

from kombu.async.http import Headers, Request, get_client
from kombu.five import items
try: # pragma: no cover
from email import message_from_file
from email.mime.message import MIMEMessage
except ImportError: # pragma: no cover
from mimetools import Message as MIMEMessage # noqa

from .ext import (
boto, AWSAuthConnection, AWSQueryConnection, XmlHandler, ResultSet,
)
def message_from_file(m): # noqa
return m

__all__ = ['AsyncHTTPConnection', 'AsyncHTTPSConnection',
'AsyncHTTPResponse', 'AsyncConnection',
'AsyncAWSAuthConnection', 'AsyncAWSQueryConnection']
__all__ = [
'AsyncHTTPConnection', 'AsyncHTTPSConnection',
'AsyncHTTPResponse', 'AsyncConnection',
'AsyncAWSAuthConnection', 'AsyncAWSQueryConnection',
]


@python_2_unicode_compatible
class AsyncHTTPResponse(object):

def __init__(self, response):
Expand Down Expand Up @@ -72,6 +75,7 @@ def __repr__(self):
return repr(self.response)


@python_2_unicode_compatible
class AsyncHTTPConnection(object):
Request = Request
Response = AsyncHTTPResponse
Expand Down
3 changes: 2 additions & 1 deletion kombu/async/http/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from vine import Thenable, promise, maybe_promise

from kombu.exceptions import HttpError
from kombu.five import items
from kombu.five import items, python_2_unicode_compatible
from kombu.utils import coro
from kombu.utils.encoding import bytes_to_str
from kombu.utils.functional import maybe_list, memoize
Expand Down Expand Up @@ -36,6 +36,7 @@ class Headers(dict):
_prev_key = None


@python_2_unicode_compatible
class Request(object):
"""A HTTP Request.
Expand Down
3 changes: 2 additions & 1 deletion kombu/async/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from vine import Thenable, promise

from kombu.five import Empty, range
from kombu.five import Empty, python_2_unicode_compatible, range
from kombu.log import get_logger
from kombu.utils import cached_property, fileno
from kombu.utils.eventio import READ, WRITE, ERR, poll
Expand Down Expand Up @@ -56,6 +56,7 @@ def set_event_loop(loop):
return loop


@python_2_unicode_compatible
class Hub(object):
"""Event loop object.
Expand Down
5 changes: 5 additions & 0 deletions kombu/async/semaphore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

from collections import deque

from kombu.five import python_2_unicode_compatible

__all__ = ['DummyLock', 'LaxBoundedSemaphore']


@python_2_unicode_compatible
class LaxBoundedSemaphore(object):
"""Asynchronous Bounded Semaphore.
Expand All @@ -21,6 +24,8 @@ class LaxBoundedSemaphore(object):
Example:
.. code-block:: pycon
>>> from future import print_statement as printf
# ^ ignore: just fooling stupid pyflakes
Expand Down
11 changes: 6 additions & 5 deletions kombu/async/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@

from vine.utils import wraps

from kombu.five import monotonic
from kombu.five import monotonic, python_2_unicode_compatible
from kombu.log import get_logger

try:
from pytz import utc
except ImportError: # pragma: no cover
utc = None

DEFAULT_MAX_INTERVAL = 2
EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=utc)
IS_PYPY = hasattr(sys, 'pypy_version_info')
__all__ = ['Entry', 'Timer', 'to_timestamp']

logger = get_logger(__name__)

__all__ = ['Entry', 'Timer', 'to_timestamp']
DEFAULT_MAX_INTERVAL = 2
EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=utc)
IS_PYPY = hasattr(sys, 'pypy_version_info')

scheduled = namedtuple('scheduled', ('eta', 'priority', 'entry'))

Expand All @@ -47,6 +47,7 @@ def to_timestamp(d, default_timezone=utc):


@total_ordering
@python_2_unicode_compatible
class Entry(object):
if not IS_PYPY: # pragma: no cover
__slots__ = (
Expand Down
4 changes: 3 additions & 1 deletion kombu/clocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
from itertools import islice
from operator import itemgetter

from .five import zip
from .five import python_2_unicode_compatible, zip

__all__ = ['LamportClock', 'timetuple']

R_CLOCK = '_lamport(clock={0}, timestamp={1}, id={2} {3!r})'


@python_2_unicode_compatible
class timetuple(tuple):
"""Tuple of event clock information.
Expand Down Expand Up @@ -68,6 +69,7 @@ def __ge__(self, other):
obj = property(itemgetter(3))


@python_2_unicode_compatible
class LamportClock(object):
"""Lamport's logical clock.
Expand Down
30 changes: 19 additions & 11 deletions kombu/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# jython breaks on relative import for .exceptions for some reason
# (Issue #112)
from kombu import exceptions
from .five import bytes_if_py2, string_t, text_t
from .five import bytes_if_py2, python_2_unicode_compatible, string_t, text_t
from .log import get_logger
from .resource import Resource
from .transport import get_transport_cls, supports_librabbitmq
Expand All @@ -28,21 +28,25 @@

__all__ = ['Connection', 'ConnectionPool', 'ChannelPool']

RESOLVE_ALIASES = {'pyamqp': 'amqp',
'librabbitmq': 'amqp'}

_LOG_CONNECTION = os.environ.get('KOMBU_LOG_CONNECTION', False)
_LOG_CHANNEL = os.environ.get('KOMBU_LOG_CHANNEL', False)

logger = get_logger(__name__)

roundrobin_failover = cycle

resolve_aliases = {
'pyamqp': 'amqp',
'librabbitmq': 'amqp',
}

failover_strategies = {
'round-robin': roundrobin_failover,
'shuffle': shufflecycle,
}

_log_connection = os.environ.get('KOMBU_LOG_CONNECTION', False)
_log_channel = os.environ.get('KOMBU_LOG_CHANNEL', False)


@python_2_unicode_compatible
class Connection(object):
"""A connection to the broker.
Expand Down Expand Up @@ -131,6 +135,9 @@ class Connection(object):
#: Heartbeat value, currently only supported by the py-amqp transport.
heartbeat = None

resolve_aliases = resolve_aliases
failover_strategies = failover_strategies

hostname = userid = password = ssl = login_method = None

def __init__(self, hostname='localhost', userid=None,
Expand Down Expand Up @@ -177,7 +184,7 @@ def __init__(self, hostname='localhost', userid=None,

# fallback hosts
self.alt = alt
self.failover_strategy = failover_strategies.get(
self.failover_strategy = self.failover_strategies.get(
failover_strategy or 'round-robin') or failover_strategy
if self.alt:
self.cycle = self.failover_strategy(self.alt)
Expand All @@ -187,7 +194,7 @@ def __init__(self, hostname='localhost', userid=None,
transport_options = {}
self.transport_options = transport_options

if _LOG_CONNECTION: # pragma: no cover
if _log_connection: # pragma: no cover
self._logger = True

if uri_prefix:
Expand Down Expand Up @@ -245,7 +252,7 @@ def channel(self):
"""Create and return a new channel."""
self._debug('create channel')
chan = self.transport.create_channel(self.connection)
if _LOG_CHANNEL: # pragma: no cover
if _log_channel: # pragma: no cover
from .utils.debug import Logwrapped
return Logwrapped(chan, 'kombu.channel',
'[Kombu channel:{0.channel_id}] ')
Expand Down Expand Up @@ -539,7 +546,8 @@ def get_heartbeat_interval(self):
def _info(self, resolve=True):
transport_cls = self.transport_cls
if resolve:
transport_cls = RESOLVE_ALIASES.get(transport_cls, transport_cls)
transport_cls = self.resolve_aliases.get(
transport_cls, transport_cls)
D = self.transport.default_connection_params

hostname = self.hostname or D.get('hostname')
Expand Down
22 changes: 13 additions & 9 deletions kombu/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from .abstract import MaybeChannelBound, Object
from .exceptions import ContentDisallowed
from .five import string_t
from .five import python_2_unicode_compatible, string_t
from .serialization import prepare_accept_content

TRANSIENT_DELIVERY_MODE = 1
Expand All @@ -32,7 +32,7 @@ def _reprstr(s):


def pretty_bindings(bindings):
return '[%s]' % (', '.join(map(str, bindings)))
return '[{0}]'.format(', '.join(map(str, bindings)))


def maybe_delivery_mode(
Expand All @@ -42,6 +42,7 @@ def maybe_delivery_mode(
return default


@python_2_unicode_compatible
class Exchange(MaybeChannelBound):
"""An Exchange declaration.
Expand Down Expand Up @@ -311,16 +312,19 @@ def __ne__(self, other):
return not self.__eq__(other)

def __repr__(self):
return super(Exchange, self).__repr__(str(self))
return self._repr_entity(self)

def __str__(self):
return 'Exchange %s(%s)' % (_reprstr(self.name) or repr(''), self.type)
return 'Exchange {0}({1})'.format(
_reprstr(self.name) or repr(''), self.type,
)

@property
def can_cache_declaration(self):
return not self.auto_delete


@python_2_unicode_compatible
class binding(Object):
"""Represents a queue or exchange binding.
Expand Down Expand Up @@ -366,14 +370,15 @@ def unbind(self, entity, nowait=False):
nowait=nowait)

def __repr__(self):
return '<binding: %s>' % (self,)
return '<binding: {0}>'.format(self)

def __str__(self):
return '%s->%s' % (
return '{0}->{1}'.format(
_reprstr(self.exchange.name), _reprstr(self.routing_key),
)


@python_2_unicode_compatible
class Queue(MaybeChannelBound):
"""A Queue declaration.
Expand Down Expand Up @@ -728,13 +733,12 @@ def __ne__(self, other):
return not self.__eq__(other)

def __repr__(self):
s = super(Queue, self).__repr__
if self.bindings:
return s('Queue {name} -> {bindings}'.format(
return self._repr_entity('Queue {name} -> {bindings}'.format(
name=_reprstr(self.name),
bindings=pretty_bindings(self.bindings),
))
return s(
return self._repr_entity(
'Queue {name} -> {0.exchange!r} -> {routing_key}'.format(
self, name=_reprstr(self.name),
routing_key=_reprstr(self.routing_key),
Expand Down

0 comments on commit 82e05c0

Please sign in to comment.