如何腌制继承的异常?

How to pickle inherited exceptions?

这个代码

import pickle
class Error(Exception):
    def __init__(self):
        super().__init__("Some error message")   

c = Error()    
a = pickle.dumps(c)
b = pickle.loads(a)

引发错误:

...
    b = pickle.loads(a)
TypeError: __init__() takes 1 positional argument but 2 were given

但是这段代码:

import pickle
class MyBaseException:
    def __init__(self, msg):
        self.msg = msg

class Error(MyBaseException):
    def __init__(self):
        super().__init__("Some error message")

c = Error()
a = pickle.dumps(c)
b = pickle.loads(a)

工作正常。

问题:如何 pickle 继承自 Python base Exception 对象的对象?

Python版本:3.6.3

BaseException 实现了一个 __reduce__ method,return 是一个类型和值为 self.args 的元组(如果除 [ 之外还有更多属性,则包括第三个值) =15=]):

>>> Error().__reduce__()
(<class '__main__.Error'>, ('Some error message',))

这个元组被 pickle 并且当 unpickling 时,用于 re-create 实例。 class 还实现了 __setstate__,如果 __reduce__ return 元组中有第三个元素,则会调用它,但这与此处无关。

您必须提供 __reduce__ 的不同实现:

class Error(Exception):
    def __init__(self):
        super().__init__("Some error message")
    def __reduce__(self):
        return type(self), ()