Skip to content

Commit bd347f7

Browse files
frostoovOmer Katz
authored andcommitted
Using Exception.args to serialize/deserialize exceptions instead of str(Exception) (celery#4085)
* Using Exception.args for serializetion * backwards compat * Fixed exception creating * Fixed typo * Test fix * backwards fixes * Formatting * Formatting and exception_to_python fix * Tests fix * Fixed exception deserialization * formatting
1 parent 87b263b commit bd347f7

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

celery/backends/base.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from celery.exceptions import (
3030
ChordError, TimeoutError, TaskRevokedError, ImproperlyConfigured,
3131
)
32-
from celery.five import items, string
32+
from celery.five import items
3333
from celery.result import (
3434
GroupResult, ResultBase, allow_join_result, result_from_tuple,
3535
)
@@ -237,14 +237,24 @@ def prepare_exception(self, exc, serializer=None):
237237
serializer = self.serializer if serializer is None else serializer
238238
if serializer in EXCEPTION_ABLE_CODECS:
239239
return get_pickleable_exception(exc)
240-
return {'exc_type': type(exc).__name__, 'exc_message': string(exc)}
240+
return {'exc_type': type(exc).__name__,
241+
'exc_message': exc.args,
242+
'exc_module': type(exc).__module__}
241243

242244
def exception_to_python(self, exc):
243245
"""Convert serialized exception to Python exception."""
244246
if exc:
245247
if not isinstance(exc, BaseException):
246-
exc = create_exception_cls(
247-
from_utf8(exc['exc_type']), __name__)(exc['exc_message'])
248+
exc_module = exc.get('exc_module')
249+
if exc_module is None:
250+
cls = create_exception_cls(
251+
from_utf8(exc['exc_type']), __name__)
252+
else:
253+
exc_module = from_utf8(exc_module)
254+
exc_type = from_utf8(exc['exc_type'])
255+
cls = getattr(sys.modules[exc_module], exc_type)
256+
exc_msg = exc['exc_message']
257+
exc = cls(*exc_msg if isinstance(exc_msg, tuple) else exc_msg)
248258
if self.serializer in EXCEPTION_ABLE_CODECS:
249259
exc = get_pickled_exception(exc)
250260
return exc

t/unit/backends/test_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ def test_regular(self):
142142
def test_unicode_message(self):
143143
message = u'\u03ac'
144144
x = self.b.prepare_exception(Exception(message))
145-
assert x == {'exc_message': message, 'exc_type': 'Exception'}
145+
assert x == {'exc_message': (message,),
146+
'exc_type': Exception.__name__,
147+
'exc_module': Exception.__module__}
146148

147149

148150
class KVBackend(KeyValueStoreBackend):

0 commit comments

Comments
 (0)