diff --git a/rq/compat/__init__.py b/rq/compat/__init__.py index a10576f..e08e4c0 100644 --- a/rq/compat/__init__.py +++ b/rq/compat/__init__.py @@ -39,3 +39,14 @@ else: opfunc.__doc__ = getattr(int, opname).__doc__ setattr(cls, opname, opfunc) return cls + + +PY2 = sys.version_info[0] < 3 + +if PY2: + string_types = (str, unicode) + text_type = unicode + +else: + string_types = (str,) + text_type = str diff --git a/rq/compat/dictconfig.py b/rq/compat/dictconfig.py index 803b7cd..c314afb 100644 --- a/rq/compat/dictconfig.py +++ b/rq/compat/dictconfig.py @@ -21,6 +21,7 @@ import logging.handlers import re import sys import types +from rq.compat import string_types IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I) @@ -230,7 +231,7 @@ class BaseConfigurator(object): isinstance(value, tuple): value = ConvertingTuple(value) value.configurator = self - elif isinstance(value, basestring): # str for py3k + elif isinstance(value, string_types): # str for py3k m = self.CONVERT_PATTERN.match(value) if m: d = m.groupdict() diff --git a/rq/decorators.py b/rq/decorators.py index 4d57c90..d57b6cc 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -2,6 +2,7 @@ from functools import wraps from .queue import Queue from .connections import resolve_connection from .worker import DEFAULT_RESULT_TTL +from rq.compat import string_types class job(object): @@ -26,7 +27,7 @@ class job(object): def __call__(self, f): @wraps(f) def delay(*args, **kwargs): - if isinstance(self.queue, basestring): + if isinstance(self.queue, string_types): queue = Queue(name=self.queue, connection=self.connection) else: queue = self.queue diff --git a/rq/job.py b/rq/job.py index b3370b5..61dd247 100644 --- a/rq/job.py +++ b/rq/job.py @@ -2,10 +2,14 @@ import importlib import inspect import times from uuid import uuid4 -from cPickle import loads, dumps, UnpicklingError +try: + from cPickle import loads, dumps, UnpicklingError +except ImportError: # noqa + from pickle import loads, dumps, UnpicklingError # noqa from .local import LocalStack from .connections import resolve_connection from .exceptions import UnpickleError, NoSuchJobError +from rq.compat import text_type def enum(name, *sequential, **named): @@ -194,7 +198,7 @@ class Job(object): first time the ID is requested. """ if self._id is None: - self._id = unicode(uuid4()) + self._id = text_type(uuid4()) return self._id def set_id(self, value): diff --git a/rq/local.py b/rq/local.py index e789c81..555a6d1 100644 --- a/rq/local.py +++ b/rq/local.py @@ -17,7 +17,10 @@ except ImportError: # noqa try: from thread import get_ident # noqa except ImportError: # noqa - from dummy_thread import get_ident # noqa + try: + from _thread import get_ident # noqa + except ImportError: # noqa + from dummy_thread import get_ident # noqa def release_local(local): diff --git a/rq/queue.py b/rq/queue.py index d08311c..f3b5f92 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -3,7 +3,7 @@ from .connections import resolve_connection from .job import Job, Status from .exceptions import (NoSuchJobError, UnpickleError, InvalidJobOperationError, DequeueTimeout) -from .compat import total_ordering +from .compat import total_ordering, string_types def get_failed_queue(connection=None): @@ -154,7 +154,7 @@ class Queue(object): * A string, representing the location of a function (must be meaningful to the import context of the workers) """ - if not isinstance(f, basestring) and f.__module__ == '__main__': + if not isinstance(f, string_types) and f.__module__ == '__main__': raise ValueError( 'Functions from the __main__ module cannot be processed ' 'by workers.') diff --git a/tests/test_job.py b/tests/test_job.py index 8f59472..3135178 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -3,7 +3,10 @@ from datetime import datetime from tests import RQTestCase from tests.fixtures import Number, some_calculation, say_hello, access_self from tests.helpers import strip_milliseconds -from cPickle import loads +try: + from cPickle import loads +except ImportError: + from pickle import loads from rq.job import Job, get_current_job from rq.exceptions import NoSuchJobError, UnpickleError from rq.queue import Queue