10000 bpo-45577: test all pickle protocols in `test_zoneinfo` (GH-29167) · python/cpython@66e6b3d · GitHub
[go: up one dir, main page]

Skip to content

Commit 66e6b3d

Browse files
authored
bpo-45577: test all pickle protocols in test_zoneinfo (GH-29167)
1 parent 233841a commit 66e6b3d

File tree

2 files changed

+54
-44
lines changed
  • Lib/test/test_zoneinfo
  • Misc/NEWS.d/next/Tests
  • 2 files changed

    +54
    -44
    lines changed

    Lib/test/test_zoneinfo/test_zoneinfo.py

    Lines changed: 53 additions & 44 deletions
    Original file line numberDiff line numberDiff line change
    @@ -1403,44 +1403,50 @@ def tzpath(self):
    14031403
    return [self.zoneinfo_data.tzpath]
    14041404

    14051405
    def test_cache_hit(self):
    1406-
    zi_in = self.klass("Europe/Dublin")
    1407-
    pkl = pickle.dumps(zi_in)
    1408-
    zi_rt = pickle.loads(pkl)
    1406+
    for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    1407+
    with self.subTest(proto=proto):
    1408+
    zi_in = self.klass("Europe/Dublin")
    1409+
    pkl = pickle.dumps(zi_in, protocol=proto)
    1410+
    zi_rt = pickle.loads(pkl)
    14091411

    1410-
    with self.subTest(test="Is non-pickled ZoneInfo"):
    1411-
    self.assertIs(zi_in, zi_rt)
    1412+
    with self.subTest(test="Is non-pickled ZoneInfo"):
    1413+
    self.assertIs(zi_in, zi_rt)
    14121414

    1413-
    zi_rt2 = pickle.loads(pkl)
    1414-
    with self.subTest(test="Is unpickled ZoneInfo"):
    1415-
    self.assertIs(zi_rt, zi_rt2)
    1415+
    zi_rt2 = pickle.loads(pkl)
    1416+
    with self.subTest(test="Is unpickled ZoneInfo"):
    1417+
    self.assertIs(zi_rt, zi_rt2)
    14161418

    14171419
    def test_cache_miss(self):
    1418-
    zi_in = self.klass("Europe/Dublin")
    1419-
    pkl = pickle.dumps(zi_in)
    1420+
    for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    1421+
    with self.subTest(proto=proto):
    1422+
    zi_in = self.klass("Europe/Dublin")
    1423+
    pkl = pickle.dumps(zi_in, protocol=proto)
    14201424

    1421-
    del zi_in
    1422-
    self.klass.clear_cache() # Induce a cache miss
    1423-
    zi_rt = pickle.loads(pkl)
    1424-
    zi_rt2 = pickle.loads(pkl)
    1425+
    del zi_in
    1426+
    self.klass.clear_cache() # Induce a cache miss
    1427+
    zi_rt = pickle.loads(pkl)
    1428+
    zi_rt2 = pickle.loads(pkl)
    14251429

    1426-
    self.assertIs(zi_rt, zi_rt2)
    1430+
    self.assertIs(zi_rt, zi_rt2)
    14271431

    14281432
    def test_no_cache(self):
    1429-
    zi_no_cache = self.klass.no_cache("Europe/Dublin")
    1433+
    for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    1434+
    with self.subTest(proto=proto):
    1435+
    zi_no_cache = self.klass.no_cache("Europe/Dublin")
    14301436

    1431-
    pkl = pickle.dumps(zi_no_cache)
    1432-
    zi_rt = pickle.loads(pkl)
    1437+
    pkl = pickle.dumps(zi_no_cache, protocol=proto)
    1438+
    zi_rt = pickle.loads(pkl)
    14331439

    1434-
    with self.subTest(test="Not the pickled object"):
    1435-
    self.assertIsNot(zi_rt, zi_no_cache)
    1440+
    with self.subTest(test="Not the pickled object"):
    1441+
    self.assertIsNot(zi_rt, zi_no_cache)
    14361442

    1437-
    zi_rt2 = pickle.loads(pkl)
    1438-
    with self.subTest(test="Not a second unpickled object"):
    1439-
    self.assertIsNot(zi_rt, zi_rt2)
    1443+
    zi_rt2 = pickle.loads(pkl)
    1444+
    with self.subTest(test="Not a second unpickled object"):
    1445+
    self.assertIsNot(zi_rt, zi_rt2)
    14401446

    1441-
    zi_cache = self.klass("Europe/Dublin")
    1442-
    with self.subTest(test="Not a cached object"):
    1443-
    self.assertIsNot(zi_rt, zi_cache)
    1447+
    zi_cache = self.klass("Europe/Dublin")
    1448+
    with self.subTest(test="Not a cached object"):
    1449+
    self.assertIsNot(zi_rt, zi_cache)
    14441450

    14451451
    def test_from_file(self):
    14461452
    key = "Europe/Dublin"
    @@ -1456,35 +1462,38 @@ def test_from_file(self):
    14561462
    ]
    14571463

    14581464
    for zi, test_name in test_cases:
    1459-
    with self.subTest(test_name=test_name):
    1460-
    with self.assertRaises(pickle.PicklingError):
    1461-
    pickle.dumps(zi)
    1465+
    for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    1466+
    with self.subTest(test_name=test_name, proto=proto):
    1467+
    with self.assertRaises(pickle.PicklingError):
    1468+
    pickle.dumps(zi, protocol=proto)
    14621469

    14631470
    def test_pickle_after_from_file(self):
    14641471
    # This may be a bit of paranoia, but this test is to ensure that no
    14651472
    # global state is maintained in order to handle the pickle cache and
    14661473
    # from_file behavior, and that it is possible to interweave the
    14671474
    # constructors of each of these and pickling/unpickling without issues.
    1468-
    key = "Europe/Dublin"
    1469-
    zi = self.klass(key)
    1475+
    for proto in range(pickle.HIGHEST_PROTOCOL + 1):
    1476+
    with self.subTest(proto=proto):
    1477+
    key = "Europe/Dublin"
    1478+
    zi = self.klass(key)
    14701479

    1471-
    pkl_0 = pickle.dumps(zi)
    1472-
    zi_rt_0 = pickle.loads(pkl_0)
    1473-
    self.assertIs(zi, zi_rt_0)
    1480+
    pkl_0 = pickle.dumps(zi, protocol=proto)
    1481+
    zi_rt_0 = pickle.loads(pkl_0)
    1482+
    self.assertIs(zi, zi_rt_0)
    14741483

    1475-
    with open(self.zoneinfo_data.path_from_key(key), "rb") as f:
    1476-
    zi_ff = self.klass.from_file(f, key=key)
    1484+
    with open(self.zoneinfo_data.path_from_key(key), "rb") as f:
    1485+
    zi_ff = self.klass.from_file(f, key=key)
    14771486

    1478-
    pkl_1 = pickle.dumps(zi)
    1479-
    zi_rt_1 = pickle.loads(pkl_1)
    1480-
    self.assertIs(zi, zi_rt_1)
    1487+
    pkl_1 = pickle.dumps(zi, protocol=proto)
    1488+
    zi_rt_1 = pickle.loads(pkl_1)
    1489+
    self.assertIs(zi, zi_rt_1)
    14811490

    1482-
    with self.assertRaises(pickle.PicklingError):
    1483-
    pickle.dumps(zi_ff)
    1491+
    with self.assertRaises(pickle.PicklingError):
    1492+
    pickle.dumps(zi_ff, protocol=proto)
    14841493

    1485-
    pkl_2 = pickle.dumps(zi)
    1486-
    zi_rt_2 = pickle.loads(pkl_2)
    1487-
    self.assertIs(zi, zi_rt_2)
    1494+
    pkl_2 = pickle.dumps(zi, protocol=proto)
    1495+
    zi_rt_2 = pickle.loads(pkl_2)
    1496+
    self.assertIs(zi, zi_rt_2)
    14881497

    14891498

    14901499
    class CZoneInfoPickleTest(ZoneInfoPickleTest):
    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1 @@
    1+
    Add subtests for all ``pickle`` protocols in ``test_zoneinfo``.

    0 commit comments

    Comments
     (0)
    0