我正在尝试腌制pygame.Surface
对象,默认情况下不可腌制。我所做的是将经典的picklability 函数添加到类并覆盖它。这样它将与我的其余代码一起使用。
class TemporarySurface(pygame.Surface):
def __getstate__(self):
print '__getstate__ executed'
return (pygame.image.tostring(self,IMAGE_TO_STRING_FORMAT),self.get_size())
def __setstate__(self,state):
print '__setstate__ executed'
tempsurf = pygame.image.frombuffer(state[0],state[1],IMAGE_TO_STRING_FORMAT)
pygame.Surface.__init__(self,tempsurf)
pygame.Surface = TemporarySurface
这是我尝试腌制一些递归对象时的回溯示例:
Traceback (most recent call last):
File "dibujar.py", line 981, in save_project
pickler.dump((key,value))
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 562, in save_tuple
save(element)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 71, in _reduce_ex
state = base(self)
ValueError: size needs to be (int width, int height)
令我困惑的部分是打印语句没有被执行。__getstate__
甚至被调用?我在这里很困惑,我不确定要提供什么信息。让我知道是否有任何额外的帮助。