| 1 |
from celery.utils.serialization import pickle |
| 2 |
|
| 3 |
|
| 4 |
class RegularException(Exception): |
| 5 |
pass |
| 6 |
|
| 7 |
|
| 8 |
class ArgOverrideException(Exception): |
| 9 |
|
| 10 |
def __init__(self, message, status_code=10): |
| 11 |
self.status_code = status_code |
| 12 |
super().__init__(message, status_code) |
| 13 |
|
| 14 |
|
| 15 |
class test_Pickle: |
| 16 |
|
| 17 |
def test_pickle_regular_exception(self): |
| 18 |
exc = None |
| 19 |
try: |
| 20 |
raise RegularException('RegularException raised') |
| 21 |
except RegularException as exc_: |
| 22 |
exc = exc_ |
| 23 |
|
| 24 |
pickled = pickle.dumps({'exception': exc}) |
| 25 |
unpickled = pickle.loads(pickled) |
| 26 |
exception = unpickled.get('exception') |
| 27 |
assert exception |
| 28 |
assert isinstance(exception, RegularException) |
| 29 |
assert exception.args == ('RegularException raised',) |
| 30 |
|
| 31 |
def test_pickle_arg_override_exception(self): |
| 32 |
|
| 33 |
exc = None |
| 34 |
try: |
| 35 |
raise ArgOverrideException( |
| 36 |
'ArgOverrideException raised', status_code=100, |
| 37 |
) |
| 38 |
except ArgOverrideException as exc_: |
| 39 |
exc = exc_ |
| 40 |
|
| 41 |
pickled = pickle.dumps({'exception': exc}) |
| 42 |
unpickled = pickle.loads(pickled) |
| 43 |
exception = unpickled.get('exception') |
| 44 |
assert exception |
| 45 |
assert isinstance(exception, ArgOverrideException) |
| 46 |
assert exception.args == ('ArgOverrideException raised', 100) |
| 47 |
assert exception.status_code == 100 |