I am attempting to pickle the pygame.Surface
object, which is not pickleable by default. What I've done is to add the classic picklability functions to the class and overwrite it. This way it will work with the rest of my code.
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
Here is an example of my traceback when I try to pickle a few recursive objects:
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)
The part that puzzles me is that the print statement is not being executed. Is __getstate__
even being called? I'm confused here, and I'm not exactly sure what information to put up. Let me know if anything additional would help.