Solve the UnicodeDecodeError while decode literal things. (#817)

* Solve the UnicodeDecodeError while decode literal things.

* Add test case for when worker result is a unicode or str object that other than
pure ascii content.
main
Peng Liu 8 years ago committed by Selwin Ong
parent cab89254b5
commit b7d4b4ec1b

@ -65,13 +65,23 @@ if not PY2:
return dict((as_text(k), h[k]) for k in h) return dict((as_text(k), h[k]) for k in h)
else: else:
# Python 2.x # 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) string_types = (str, unicode)
def as_text(v): def as_text(v):
if v is None: if v is None:
return 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): def decode_redis_hash(h):
return h return h

@ -27,6 +27,11 @@ def say_hello(name=None):
return 'Hi there, %s!' % (name,) 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(): def do_nothing():
"""The best job in the world.""" """The best job in the world."""
pass pass

@ -452,6 +452,20 @@ class TestWorker(RQTestCase):
self.assertEqual(job.result, 'Hi there, Adam!') self.assertEqual(job.result, 'Hi there, Adam!')
self.assertEqual(job.description, '你好 世界!') 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): def test_suspend_worker_execution(self):
"""Test Pause Worker Execution""" """Test Pause Worker Execution"""

Loading…
Cancel
Save