def testDatabaseFixes(self): # Hack the pickle to make it refer to a timezone abbreviation # that does not match anything. The unpickler should be able # to repair this case tz = pytz.timezone('Australia/Melbourne') p = pickle.dumps(tz) tzname = tz._tzname hacked_p = p.replace(_byte_string(tzname), _byte_string('???')) self.assertNotEqual(p, hacked_p) unpickled_tz = pickle.loads(hacked_p) self.assertTrue(tz is unpickled_tz) # Simulate a database correction. In this case, the incorrect # data will continue to be used. p = pickle.dumps(tz) new_utcoffset = tz._utcoffset.seconds + 42 # Python 3 introduced a new pickle protocol where numbers are stored in # hexadecimal representation. Here we extract the pickle # representation of the number for the current Python version. old_pickle_pattern = pickle.dumps(tz._utcoffset.seconds)[3:-1] new_pickle_pattern = pickle.dumps(new_utcoffset)[3:-1] hacked_p = p.replace(old_pickle_pattern, new_pickle_pattern) self.assertNotEqual(p, hacked_p) unpickled_tz = pickle.loads(hacked_p) self.assertEqual(unpickled_tz._utcoffset.seconds, new_utcoffset) self.assertTrue(tz is not unpickled_tz)
def testOldPickles(self): # Ensure that applications serializing pytz instances as pickles # have no troubles upgrading to a new pytz release. These pickles # where created with pytz2006j east1 = pickle.loads(_byte_string( "cpytz\n_p\np1\n(S'US/Eastern'\np2\nI-18000\n" "I0\nS'EST'\np3\ntRp4\n." )) east2 = pytz.timezone('US/Eastern').localize( datetime(2006, 1, 1)).tzinfo self.assertTrue(east1 is east2) # Confirm changes in name munging between 2006j and 2007c cause # no problems. pap1 = pickle.loads(_byte_string( "cpytz\n_p\np1\n(S'America/Port_minus_au_minus_Prince'" "\np2\nI-17340\nI0\nS'PPMT'\np3\ntRp4\n.")) pap2 = pytz.timezone('America/Port-au-Prince').localize( datetime(1910, 1, 1)).tzinfo self.assertTrue(pap1 is pap2) gmt1 = pickle.loads(_byte_string( "cpytz\n_p\np1\n(S'Etc/GMT_plus_10'\np2\ntRp3\n.")) gmt2 = pytz.timezone('Etc/GMT+10') self.assertTrue(gmt1 is gmt2)
def testDatabaseFixes(self): # Hack the pickle to make it refer to a timezone abbreviation # that does not match anything. The unpickler should be able # to repair this case tz = pytz.timezone('Australia/Melbourne') p = pickle.dumps(tz) tzname = tz._tzname hacked_p = p.replace(_byte_string(tzname), _byte_string('?'*len(tzname))) self.assertNotEqual(p, hacked_p) unpickled_tz = pickle.loads(hacked_p) self.assertTrue(tz is unpickled_tz) # Simulate a database correction. In this case, the incorrect # data will continue to be used. p = pickle.dumps(tz) new_utcoffset = tz._utcoffset.seconds + 42 # Python 3 introduced a new pickle protocol where numbers are stored in # hexadecimal representation. Here we extract the pickle # representation of the number for the current Python version. old_pickle_pattern = pickle.dumps(tz._utcoffset.seconds)[3:-1] new_pickle_pattern = pickle.dumps(new_utcoffset)[3:-1] hacked_p = p.replace(old_pickle_pattern, new_pickle_pattern) self.assertNotEqual(p, hacked_p) unpickled_tz = pickle.loads(hacked_p) self.assertEqual(unpickled_tz._utcoffset.seconds, new_utcoffset) self.assertTrue(tz is not unpickled_tz)