From cd0230cae315d3dfc5ee3d8983f50388da323830 Mon Sep 17 00:00:00 2001 From: Adam Wen Date: Thu, 16 Oct 2014 18:00:48 +0800 Subject: [PATCH 1/3] make job description unicode friendly --- rq/job.py | 3 ++- tests/test_worker.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/rq/job.py b/rq/job.py index e8080f8..a2cf6a2 100644 --- a/rq/job.py +++ b/rq/job.py @@ -407,7 +407,8 @@ class Job(object): self.created_at = to_date(as_text(obj.get('created_at'))) self.origin = as_text(obj.get('origin')) - self.description = as_text(obj.get('description')) + self.description = (as_text(obj.get('description')).decode('unicode_escape') + if obj.get('description') else None) self.enqueued_at = to_date(as_text(obj.get('enqueued_at'))) self.ended_at = to_date(as_text(obj.get('ended_at'))) self._result = unpickle(obj.get('result')) if obj.get('result') else None # noqa diff --git a/tests/test_worker.py b/tests/test_worker.py index 764cf46..c6d85ff 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -307,3 +307,14 @@ class TestWorker(RQTestCase): # Updates worker statuses self.assertEqual(worker.state, 'busy') self.assertEqual(worker.get_current_job_id(), job.id) + + def test_work_unicode_friendly(self): + """Worker processes work with unicode description, then quits.""" + q = Queue('foo') + w = Worker([q]) + job = q.enqueue('tests.fixtures.say_hello', name='Adam', + description='你好 世界!') + self.assertEquals(w.work(burst=True), True, + 'Expected at least some work done.') + self.assertEquals(job.result, 'Hi there, Adam!') + self.assertEquals(job.description, '你好 世界!') From 1795e1ee531b95ba9235ae645703a954453db2bf Mon Sep 17 00:00:00 2001 From: Adam Wen Date: Thu, 16 Oct 2014 18:47:32 +0800 Subject: [PATCH 2/3] fix decode --- rq/job.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rq/job.py b/rq/job.py index a2cf6a2..6ba0807 100644 --- a/rq/job.py +++ b/rq/job.py @@ -407,7 +407,7 @@ class Job(object): self.created_at = to_date(as_text(obj.get('created_at'))) self.origin = as_text(obj.get('origin')) - self.description = (as_text(obj.get('description')).decode('unicode_escape') + self.description = (as_text(obj.get('description')).decode('utf-8') if obj.get('description') else None) self.enqueued_at = to_date(as_text(obj.get('enqueued_at'))) self.ended_at = to_date(as_text(obj.get('ended_at'))) From f5001a0e361357d53e1c2d5cff9c932afc465340 Mon Sep 17 00:00:00 2001 From: Adam Wen Date: Mon, 20 Oct 2014 10:39:27 +0800 Subject: [PATCH 3/3] fix decode error in py3. --- rq/compat/__init__.py | 4 +++- rq/job.py | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/rq/compat/__init__.py b/rq/compat/__init__.py index 5e660b5..3095983 100644 --- a/rq/compat/__init__.py +++ b/rq/compat/__init__.py @@ -69,7 +69,9 @@ else: string_types = (str, unicode) def as_text(v): - return v + if v is None: + return None + return v.decode('utf-8') def decode_redis_hash(h): return h diff --git a/rq/job.py b/rq/job.py index 6ba0807..e8080f8 100644 --- a/rq/job.py +++ b/rq/job.py @@ -407,8 +407,7 @@ class Job(object): self.created_at = to_date(as_text(obj.get('created_at'))) self.origin = as_text(obj.get('origin')) - self.description = (as_text(obj.get('description')).decode('utf-8') - if obj.get('description') else None) + self.description = as_text(obj.get('description')) self.enqueued_at = to_date(as_text(obj.get('enqueued_at'))) self.ended_at = to_date(as_text(obj.get('ended_at'))) self._result = unpickle(obj.get('result')) if obj.get('result') else None # noqa