Is there a way to make python pickle ignore the error "this is not the same object"?
I am writing a test using Mock to have fine control over the results obtained by datetime.utcnow (). The code I use is time sensitive, so the layout of the patch makes it easy to test.
The same tests should sort the objects and send the results to the remote server. For testing purposes, if the standard time and time was pickled and received by the remote server, everything will be fine.
Unfortunately, the brine module has the following error:
Unable to sort <type 'datetime.datetime'>: this is not the same object as datetime.datetime
Here is a minimal example to reproduce the error.
from mock import patch from datetime import datetime import pickle class MockDatetime(datetime): frozendt = datetime(2011,05,31) @classmethod def advance(cls, **kw): cls.frozendt = cls.frozendt + timedelta(**kw) @classmethod def utcnow(cls): return cls.frozendt @patch('datetime.datetime', MockDatetime) def test(): pickle.dumps(datetime.utcnow()) if __name__ == '__main__': test()
Are there any combos of the __reduce__ and __getstate__ methods that could trick the pickle mechanism into thinking MockDatetime - is this the time I'm pickled?
source share