Accept byte strings as the first argument of Worker() in Python 2

This make it easy to write Python 2/3 compatible code.
e.g.

    Worker(sys.argv[1:])

    # Without from __future__ import unicode_literals
    Worker(['high', 'normal', 'low'])
main
orangain 9 years ago
parent a14041ae1a
commit e8165fdddf

@ -128,7 +128,7 @@ class Worker(object):
connection = get_current_connection() connection = get_current_connection()
self.connection = connection self.connection = connection
queues = [self.queue_class(name=q) if isinstance(q, text_type) else q queues = [self.queue_class(name=q) if isinstance(q, string_types) else q
for q in ensure_list(queues)] for q in ensure_list(queues)]
self._name = name self._name = name
self.queues = queues self.queues = queues
@ -176,7 +176,7 @@ class Worker(object):
"""Sanity check for the given queues.""" """Sanity check for the given queues."""
for queue in self.queues: for queue in self.queues:
if not isinstance(queue, self.queue_class): if not isinstance(queue, self.queue_class):
raise TypeError('{0} is not of type {1} or text type'.format(queue, self.queue_class)) raise TypeError('{0} is not of type {1} or string types'.format(queue, self.queue_class))
def queue_names(self): def queue_names(self):
"""Returns the queue names of this worker's queues.""" """Returns the queue names of this worker's queues."""

@ -15,7 +15,7 @@ from tests.fixtures import (create_file, create_file_after_timeout,
from tests.helpers import strip_microseconds from tests.helpers import strip_microseconds
from rq import get_failed_queue, Queue, SimpleWorker, Worker from rq import get_failed_queue, Queue, SimpleWorker, Worker
from rq.compat import as_text from rq.compat import as_text, PY2
from rq.job import Job, JobStatus from rq.job import Job, JobStatus
from rq.registry import StartedJobRegistry from rq.registry import StartedJobRegistry
from rq.suspension import resume, suspend from rq.suspension import resume, suspend
@ -44,6 +44,22 @@ class TestWorker(RQTestCase):
self.assertEqual(w.queues[0].name, 'foo') self.assertEqual(w.queues[0].name, 'foo')
self.assertEqual(w.queues[1].name, 'bar') self.assertEqual(w.queues[1].name, 'bar')
# Also accept byte strings in Python 2
if PY2:
# With single byte string argument
w = Worker(b'foo')
self.assertEqual(w.queues[0].name, 'foo')
# With list of byte strings
w = Worker([b'foo', b'bar'])
self.assertEqual(w.queues[0].name, 'foo')
self.assertEqual(w.queues[1].name, 'bar')
# With iterable of byte strings
w = Worker(iter([b'foo', b'bar']))
self.assertEqual(w.queues[0].name, 'foo')
self.assertEqual(w.queues[1].name, 'bar')
# With single Queue # With single Queue
w = Worker(Queue('foo')) w = Worker(Queue('foo'))
self.assertEqual(w.queues[0].name, 'foo') self.assertEqual(w.queues[0].name, 'foo')

Loading…
Cancel
Save