mirror of https://github.com/python/cpython.git
bpo-45577: test all pickle protocols in `test_zoneinfo` (GH-29167)
This commit is contained in:
parent
233841ab78
commit
66e6b3dcd3
|
@ -1403,44 +1403,50 @@ def tzpath(self):
|
||||||
return [self.zoneinfo_data.tzpath]
|
return [self.zoneinfo_data.tzpath]
|
||||||
|
|
||||||
def test_cache_hit(self):
|
def test_cache_hit(self):
|
||||||
zi_in = self.klass("Europe/Dublin")
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
pkl = pickle.dumps(zi_in)
|
with self.subTest(proto=proto):
|
||||||
zi_rt = pickle.loads(pkl)
|
zi_in = self.klass("Europe/Dublin")
|
||||||
|
pkl = pickle.dumps(zi_in, protocol=proto)
|
||||||
|
zi_rt = pickle.loads(pkl)
|
||||||
|
|
||||||
with self.subTest(test="Is non-pickled ZoneInfo"):
|
with self.subTest(test="Is non-pickled ZoneInfo"):
|
||||||
self.assertIs(zi_in, zi_rt)
|
self.assertIs(zi_in, zi_rt)
|
||||||
|
|
||||||
zi_rt2 = pickle.loads(pkl)
|
zi_rt2 = pickle.loads(pkl)
|
||||||
with self.subTest(test="Is unpickled ZoneInfo"):
|
with self.subTest(test="Is unpickled ZoneInfo"):
|
||||||
self.assertIs(zi_rt, zi_rt2)
|
self.assertIs(zi_rt, zi_rt2)
|
||||||
|
|
||||||
def test_cache_miss(self):
|
def test_cache_miss(self):
|
||||||
zi_in = self.klass("Europe/Dublin")
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
pkl = pickle.dumps(zi_in)
|
with self.subTest(proto=proto):
|
||||||
|
zi_in = self.klass("Europe/Dublin")
|
||||||
|
pkl = pickle.dumps(zi_in, protocol=proto)
|
||||||
|
|
||||||
del zi_in
|
del zi_in
|
||||||
self.klass.clear_cache() # Induce a cache miss
|
self.klass.clear_cache() # Induce a cache miss
|
||||||
zi_rt = pickle.loads(pkl)
|
zi_rt = pickle.loads(pkl)
|
||||||
zi_rt2 = pickle.loads(pkl)
|
zi_rt2 = pickle.loads(pkl)
|
||||||
|
|
||||||
self.assertIs(zi_rt, zi_rt2)
|
self.assertIs(zi_rt, zi_rt2)
|
||||||
|
|
||||||
def test_no_cache(self):
|
def test_no_cache(self):
|
||||||
zi_no_cache = self.klass.no_cache("Europe/Dublin")
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
with self.subTest(proto=proto):
|
||||||
|
zi_no_cache = self.klass.no_cache("Europe/Dublin")
|
||||||
|
|
||||||
pkl = pickle.dumps(zi_no_cache)
|
pkl = pickle.dumps(zi_no_cache, protocol=proto)
|
||||||
zi_rt = pickle.loads(pkl)
|
zi_rt = pickle.loads(pkl)
|
||||||
|
|
||||||
with self.subTest(test="Not the pickled object"):
|
with self.subTest(test="Not the pickled object"):
|
||||||
self.assertIsNot(zi_rt, zi_no_cache)
|
self.assertIsNot(zi_rt, zi_no_cache)
|
||||||
|
|
||||||
zi_rt2 = pickle.loads(pkl)
|
zi_rt2 = pickle.loads(pkl)
|
||||||
with self.subTest(test="Not a second unpickled object"):
|
with self.subTest(test="Not a second unpickled object"):
|
||||||
self.assertIsNot(zi_rt, zi_rt2)
|
self.assertIsNot(zi_rt, zi_rt2)
|
||||||
|
|
||||||
zi_cache = self.klass("Europe/Dublin")
|
zi_cache = self.klass("Europe/Dublin")
|
||||||
with self.subTest(test="Not a cached object"):
|
with self.subTest(test="Not a cached object"):
|
||||||
self.assertIsNot(zi_rt, zi_cache)
|
self.assertIsNot(zi_rt, zi_cache)
|
||||||
|
|
||||||
def test_from_file(self):
|
def test_from_file(self):
|
||||||
key = "Europe/Dublin"
|
key = "Europe/Dublin"
|
||||||
|
@ -1456,35 +1462,38 @@ def test_from_file(self):
|
||||||
]
|
]
|
||||||
|
|
||||||
for zi, test_name in test_cases:
|
for zi, test_name in test_cases:
|
||||||
with self.subTest(test_name=test_name):
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
with self.assertRaises(pickle.PicklingError):
|
with self.subTest(test_name=test_name, proto=proto):
|
||||||
pickle.dumps(zi)
|
with self.assertRaises(pickle.PicklingError):
|
||||||
|
pickle.dumps(zi, protocol=proto)
|
||||||
|
|
||||||
def test_pickle_after_from_file(self):
|
def test_pickle_after_from_file(self):
|
||||||
# This may be a bit of paranoia, but this test is to ensure that no
|
# This may be a bit of paranoia, but this test is to ensure that no
|
||||||
# global state is maintained in order to handle the pickle cache and
|
# global state is maintained in order to handle the pickle cache and
|
||||||
# from_file behavior, and that it is possible to interweave the
|
# from_file behavior, and that it is possible to interweave the
|
||||||
# constructors of each of these and pickling/unpickling without issues.
|
# constructors of each of these and pickling/unpickling without issues.
|
||||||
key = "Europe/Dublin"
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
zi = self.klass(key)
|
with self.subTest(proto=proto):
|
||||||
|
key = "Europe/Dublin"
|
||||||
|
zi = self.klass(key)
|
||||||
|
|
||||||
pkl_0 = pickle.dumps(zi)
|
pkl_0 = pickle.dumps(zi, protocol=proto)
|
||||||
zi_rt_0 = pickle.loads(pkl_0)
|
zi_rt_0 = pickle.loads(pkl_0)
|
||||||
self.assertIs(zi, zi_rt_0)
|
self.assertIs(zi, zi_rt_0)
|
||||||
|
|
||||||
with open(self.zoneinfo_data.path_from_key(key), "rb") as f:
|
with open(self.zoneinfo_data.path_from_key(key), "rb") as f:
|
||||||
zi_ff = self.klass.from_file(f, key=key)
|
zi_ff = self.klass.from_file(f, key=key)
|
||||||
|
|
||||||
pkl_1 = pickle.dumps(zi)
|
pkl_1 = pickle.dumps(zi, protocol=proto)
|
||||||
zi_rt_1 = pickle.loads(pkl_1)
|
zi_rt_1 = pickle.loads(pkl_1)
|
||||||
self.assertIs(zi, zi_rt_1)
|
self.assertIs(zi, zi_rt_1)
|
||||||
|
|
||||||
with self.assertRaises(pickle.PicklingError):
|
with self.assertRaises(pickle.PicklingError):
|
||||||
pickle.dumps(zi_ff)
|
pickle.dumps(zi_ff, protocol=proto)
|
||||||
|
|
||||||
pkl_2 = pickle.dumps(zi)
|
pkl_2 = pickle.dumps(zi, protocol=proto)
|
||||||
zi_rt_2 = pickle.loads(pkl_2)
|
zi_rt_2 = pickle.loads(pkl_2)
|
||||||
self.assertIs(zi, zi_rt_2)
|
self.assertIs(zi, zi_rt_2)
|
||||||
|
|
||||||
|
|
||||||
class CZoneInfoPickleTest(ZoneInfoPickleTest):
|
class CZoneInfoPickleTest(ZoneInfoPickleTest):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Add subtests for all ``pickle`` protocols in ``test_zoneinfo``.
|
Loading…
Reference in New Issue