0

我正在使用搁置模块为我的程序添加一些持久性。起初我在搁置中使用默认的 bsddb,但我不得不将其更改为 gdbm,然后发生异常:

    Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in <type       'exceptions.AttributeError'> ignored

它似乎被一些 whichdb 调用在 anydbm 模块中引发(并捕获)。

它没有什么不好的,因为代码完成没有问题,但它看起来有点糟糕。据我所知,我可以使用警告模块来禁止打印,但是我想完全删除异常。

我正在查看其他地方的类似问题(它们出现在 pylint、django 和其他一些模块/包中),并且在任何地方似乎都被标记为“错误”。有没有人想出任何dbm / shelve模块中这种行为的解决方案?

编辑1。我发现导致此错误的原因是: def getattr (self, attr): return getattr(self.config, attr)

我正在尝试将包装另一个类的类保存到搁置中 - 这就是getattr函数被覆盖的原因。有没有办法写它而不是无限循环?

4

1 回答 1

1

好吧,我想我设法解决了这个问题。问题是包装类没有setstategetstate函数。到目前为止,它似乎工作。包装类不需要setstategetstate

我为包装类使用了简单的 getstate 和 setstate 函数:

    def __getstate__(self):
        '''
        Getstate for pickle (used by shelve module)
        '''
        return self.__dict__

    def __setstate__(self, dictionary):
        '''
        Setstate for pickle (used by shelve module)
        '''
        self.__dict__ = dictionary
于 2012-09-11T13:30:56.603 回答