bpo-45577: test all pickle protocols in `test_zoneinfo` (GH-29167)

This commit is contained in:
Nikita Sobolev 2021-10-28 23:22:24 +03:00 committed by GitHub
parent 233841ab78
commit 66e6b3dcd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 44 deletions

View File

@ -1403,8 +1403,10 @@ def tzpath(self):
return [self.zoneinfo_data.tzpath] return [self.zoneinfo_data.tzpath]
def test_cache_hit(self): def test_cache_hit(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
zi_in = self.klass("Europe/Dublin") zi_in = self.klass("Europe/Dublin")
pkl = pickle.dumps(zi_in) pkl = pickle.dumps(zi_in, protocol=proto)
zi_rt = pickle.loads(pkl) zi_rt = pickle.loads(pkl)
with self.subTest(test="Is non-pickled ZoneInfo"): with self.subTest(test="Is non-pickled ZoneInfo"):
@ -1415,8 +1417,10 @@ def test_cache_hit(self):
self.assertIs(zi_rt, zi_rt2) self.assertIs(zi_rt, zi_rt2)
def test_cache_miss(self): def test_cache_miss(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
zi_in = self.klass("Europe/Dublin") zi_in = self.klass("Europe/Dublin")
pkl = pickle.dumps(zi_in) 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
@ -1426,9 +1430,11 @@ def test_cache_miss(self):
self.assertIs(zi_rt, zi_rt2) self.assertIs(zi_rt, zi_rt2)
def test_no_cache(self): def test_no_cache(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
zi_no_cache = self.klass.no_cache("Europe/Dublin") 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"):
@ -1456,33 +1462,36 @@ 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.subTest(test_name=test_name, proto=proto):
with self.assertRaises(pickle.PicklingError): with self.assertRaises(pickle.PicklingError):
pickle.dumps(zi) 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.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.subTest(proto=proto):
key = "Europe/Dublin" key = "Europe/Dublin"
zi = self.klass(key) 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)

View File

@ -0,0 +1 @@
Add subtests for all ``pickle`` protocols in ``test_zoneinfo``.