accomodate py3 imports and builtins

main
Alex Morega 12 years ago
parent 2e517001a9
commit a3b5ce5e46

@ -39,3 +39,14 @@ else:
opfunc.__doc__ = getattr(int, opname).__doc__ opfunc.__doc__ = getattr(int, opname).__doc__
setattr(cls, opname, opfunc) setattr(cls, opname, opfunc)
return cls return cls
PY2 = sys.version_info[0] < 3
if PY2:
string_types = (str, unicode)
text_type = unicode
else:
string_types = (str,)
text_type = str

@ -21,6 +21,7 @@ import logging.handlers
import re import re
import sys import sys
import types import types
from rq.compat import string_types
IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I) IDENTIFIER = re.compile('^[a-z_][a-z0-9_]*$', re.I)
@ -230,7 +231,7 @@ class BaseConfigurator(object):
isinstance(value, tuple): isinstance(value, tuple):
value = ConvertingTuple(value) value = ConvertingTuple(value)
value.configurator = self value.configurator = self
elif isinstance(value, basestring): # str for py3k elif isinstance(value, string_types): # str for py3k
m = self.CONVERT_PATTERN.match(value) m = self.CONVERT_PATTERN.match(value)
if m: if m:
d = m.groupdict() d = m.groupdict()

@ -2,6 +2,7 @@ from functools import wraps
from .queue import Queue from .queue import Queue
from .connections import resolve_connection from .connections import resolve_connection
from .worker import DEFAULT_RESULT_TTL from .worker import DEFAULT_RESULT_TTL
from rq.compat import string_types
class job(object): class job(object):
@ -26,7 +27,7 @@ class job(object):
def __call__(self, f): def __call__(self, f):
@wraps(f) @wraps(f)
def delay(*args, **kwargs): def delay(*args, **kwargs):
if isinstance(self.queue, basestring): if isinstance(self.queue, string_types):
queue = Queue(name=self.queue, connection=self.connection) queue = Queue(name=self.queue, connection=self.connection)
else: else:
queue = self.queue queue = self.queue

@ -2,10 +2,14 @@ import importlib
import inspect import inspect
import times import times
from uuid import uuid4 from uuid import uuid4
try:
from cPickle import loads, dumps, UnpicklingError from cPickle import loads, dumps, UnpicklingError
except ImportError: # noqa
from pickle import loads, dumps, UnpicklingError # noqa
from .local import LocalStack from .local import LocalStack
from .connections import resolve_connection from .connections import resolve_connection
from .exceptions import UnpickleError, NoSuchJobError from .exceptions import UnpickleError, NoSuchJobError
from rq.compat import text_type
def enum(name, *sequential, **named): def enum(name, *sequential, **named):
@ -194,7 +198,7 @@ class Job(object):
first time the ID is requested. first time the ID is requested.
""" """
if self._id is None: if self._id is None:
self._id = unicode(uuid4()) self._id = text_type(uuid4())
return self._id return self._id
def set_id(self, value): def set_id(self, value):

@ -16,6 +16,9 @@ try:
except ImportError: # noqa except ImportError: # noqa
try: try:
from thread import get_ident # noqa from thread import get_ident # noqa
except ImportError: # noqa
try:
from _thread import get_ident # noqa
except ImportError: # noqa except ImportError: # noqa
from dummy_thread import get_ident # noqa from dummy_thread import get_ident # noqa

@ -3,7 +3,7 @@ from .connections import resolve_connection
from .job import Job, Status from .job import Job, Status
from .exceptions import (NoSuchJobError, UnpickleError, from .exceptions import (NoSuchJobError, UnpickleError,
InvalidJobOperationError, DequeueTimeout) InvalidJobOperationError, DequeueTimeout)
from .compat import total_ordering from .compat import total_ordering, string_types
def get_failed_queue(connection=None): def get_failed_queue(connection=None):
@ -154,7 +154,7 @@ class Queue(object):
* A string, representing the location of a function (must be * A string, representing the location of a function (must be
meaningful to the import context of the workers) 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( raise ValueError(
'Functions from the __main__ module cannot be processed ' 'Functions from the __main__ module cannot be processed '
'by workers.') 'by workers.')

@ -3,7 +3,10 @@ from datetime import datetime
from tests import RQTestCase from tests import RQTestCase
from tests.fixtures import Number, some_calculation, say_hello, access_self from tests.fixtures import Number, some_calculation, say_hello, access_self
from tests.helpers import strip_milliseconds from tests.helpers import strip_milliseconds
try:
from cPickle import loads from cPickle import loads
except ImportError:
from pickle import loads
from rq.job import Job, get_current_job from rq.job import Job, get_current_job
from rq.exceptions import NoSuchJobError, UnpickleError from rq.exceptions import NoSuchJobError, UnpickleError
from rq.queue import Queue from rq.queue import Queue

Loading…
Cancel
Save