无法解开以kwargs为参数的自定义异常

时间:2018-02-22 15:52:37

标签: python python-2.7

import pickle


class CustomEx(Exception):
    def __init__(self, *args, **kwargs):
        self.a = kwargs.get('a')
        assert self.a, "a must present"


s = pickle.dumps(CustomEx(a='a'))
pickle.loads(str(s)) # this fails

我使用的是python版本2.7.13。请告知我该如何使它工作。它失败并跟随堆栈跟踪:

Traceback (most recent call last):
  File "/Users/app/tmp/pickle_user_purchase_required.py", line 10, in <module>
    pickle.loads(str(s))
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1388, in loads
    return Unpickler(file).load()
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 864, in load
    dispatch[key](self)
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1139, in load_reduce
    value = func(*args)
  File "/Users/app/tmp/pickle_user_purchase_required.py", line 6, in __init__
    assert self.a, "a must present"
AssertionError: a must present

0 个答案:

没有答案