Is there a way to make python pickle ignore "it's not the same object " errors?
I'm writing a test using Mock to have fine grain control over the results that datetime.utcnow() produces. The code I'm using is time sensitive so mock's patch makes it easy to test.
The same tests need to pickle objects and send the results to a remote server. For the purpose of the test if a standard datetime was pickled and received by the remote server everything would be fine.
Unfortunately the pickle module is barfing with the following error:
Can't pickle <type 'datetime.datetime'>: it's not the same object as
from mock import patch
from datetime import datetime
frozendt = datetime(2011,05,31)
def advance(cls, **kw):
cls.frozendt = cls.frozendt + timedelta(**kw)
if __name__ == '__main__':
Looking at the where to patch section in the documentation I see this advice:
The basic principle is that you patch where an object is used, which is not necessarily the same place as where it is defined.
Following this recommendation, I've tried to replace:
and I didn't get any error from
pickle. Also, I added a
datetime was really being patched and I got the expected value.