Explorar o código

Correct handling of exceptions with unicode messages in TaskWrapper.on_failure method.

Mikhail Korobov %!s(int64=15) %!d(string=hai) anos
pai
achega
0bf1404966
Modificáronse 2 ficheiros con 13 adicións e 4 borrados
  1. 11 2
      celery/tests/test_worker_job.py
  2. 2 2
      celery/worker/job.py

+ 11 - 2
celery/tests/test_worker_job.py

@@ -387,11 +387,11 @@ class TestTaskWrapper(unittest.TestCase):
                     "delivery_info": {},
                     "task_name": tw.task_name})
 
-    def test_on_failure(self):
+    def _test_on_failure(self, exception):
         tid = gen_unique_id()
         tw = TaskWrapper(mytask.name, tid, [4], {"f": "x"})
         try:
-            raise Exception("Inside unit tests")
+            raise exception
         except Exception:
             exc_info = ExceptionInfo(sys.exc_info())
 
@@ -409,3 +409,12 @@ class TestTaskWrapper(unittest.TestCase):
         self.assertIn("ERROR", logvalue)
 
         conf.CELERY_SEND_TASK_ERROR_EMAILS = False
+
+    def test_on_failure(self):
+        self._test_on_failure(Exception("Inside unit tests"))
+
+    def test_on_failure_unicode_exception(self):
+        self._test_on_failure(Exception(u"Бобры атакуют"))
+
+    def test_on_failure_utf8_exception(self):
+        self._test_on_failure(Exception(u"Бобры атакуют".encode('utf8')))

+ 2 - 2
celery/worker/job.py

@@ -345,8 +345,8 @@ class TaskWrapper(object):
             "hostname": socket.gethostname(),
             "id": self.task_id,
             "name": self.task_name,
-            "exc": exc_info.exception,
-            "traceback": exc_info.traceback,
+            "exc": repr(exc_info.exception),
+            "traceback": unicode(exc_info.traceback, 'utf-8'),
             "args": self.args,
             "kwargs": self.kwargs,
         }