diff --git a/rq/compat/__init__.py b/rq/compat/__init__.py index 3095983..7565835 100644 --- a/rq/compat/__init__.py +++ b/rq/compat/__init__.py @@ -65,13 +65,23 @@ if not PY2: return dict((as_text(k), h[k]) for k in h) else: # Python 2.x - text_type = unicode + def text_type(v): + try: + return unicode(v) + except Exception: + return unicode(v, "utf-8", errors="ignore") + string_types = (str, unicode) def as_text(v): if v is None: return None - return v.decode('utf-8') + elif isinstance(v, str): + return v.decode('utf-8') + elif isinstance(v, unicode): + return v + else: + raise Exception("Input cannot be decoded into literal thing.") def decode_redis_hash(h): return h diff --git a/tests/fixtures.py b/tests/fixtures.py index 031a0e2..9173989 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -27,6 +27,11 @@ def say_hello(name=None): return 'Hi there, %s!' % (name,) +def say_hello_unicode(name=None): + """A job with a single argument and a return value.""" + return unicode(say_hello(name)) + + def do_nothing(): """The best job in the world.""" pass diff --git a/tests/test_worker.py b/tests/test_worker.py index f337400..02c6aa6 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -452,6 +452,20 @@ class TestWorker(RQTestCase): self.assertEqual(job.result, 'Hi there, Adam!') self.assertEqual(job.description, '你好 世界!') + def test_work_log_unicode_friendly(self): + """Worker process work with unicode or str other than pure ascii content, + logging work properly""" + q = Queue("foo") + w = Worker([q]) + job = q.enqueue('tests.fixtures.say_hello', name='阿达姆', + description='你好 世界!') + self.assertEqual(w.work(burst=True), True, + 'Expected at least some work done.') + job = q.enqueue('tests.fixtures.say_hello_unicode', name='阿达姆', + description='你好 世界!') + self.assertEqual(w.work(burst=True), True, + 'Expected at least some work done.') + def test_suspend_worker_execution(self): """Test Pause Worker Execution"""