Skip to content

Commit 1f7a1fa

Browse files
bpo-43132: Add tests for find_in_strong_cache() bug in _zoneinfo
Co-Authored-By: Paul Ganssle <[email protected]>
1 parent b4fc44b commit 1f7a1fa

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,20 @@ def test_time_fixed_offset(self):
400400
self.assertEqual(t.utcoffset(), offset.utcoffset)
401401
self.assertEqual(t.dst(), offset.dst)
402402

403+
def test_cache_exception(self):
404+
class ComparisonError(Exception):
405+
pass
406+
407+
class Incomparable(str):
408+
def __eq__(self, other):
409+
raise ComparisonError
410+
def __hash__(self):
411+
return id(self)
412+
413+
key = Incomparable("America/Los_Angeles")
414+
with self.assertRaises(ComparisonError):
415+
self.klass(key)
416+
403417

404418
class CZoneInfoTest(ZoneInfoTest):
405419
module = c_zoneinfo
@@ -1377,6 +1391,33 @@ def test_clear_cache_two_keys(self):
13771391
self.assertIsNot(dub0, dub1)
13781392
self.assertIs(tok0, tok1)
13791393

1394+
def test_clear_cache_refleak(self):
1395+
class ComparisonError(Exception):
1396+
pass
1397+
1398+
class Stringy(str):
1399+
def __new__(cls, value):
1400+
rv = super().__new__(cls, value)
1401+
rv.allow_comparisons = True
1402+
return rv
1403+
def __eq__(self, other):
1404+
if not self.allow_comparisons:
1405+
raise ComparisonError
1406+
return super().__eq__(other)
1407+
def __hash__(self):
1408+
return hash(self[:])
1409+
1410+
key = Stringy("America/Los_Angeles")
1411+
self.klass(key)
1412+
key.allow_comparisons = False
1413+
try:
1414+
# Note: This is try/except rather than assertRaises because
1415+
# there is no guarantee that the key is even still in the cache,
1416+
# or that the key for the cache is the original `key` object.
1417+
self.klass.clear_cache(only_keys="America/Los_Angeles")
1418+
except ComparisonError:
1419+
pass
1420+
13801421

13811422
class CZoneInfoCacheTest(ZoneInfoCacheTest):
13821423
module = c_zoneinfo

0 commit comments

Comments
 (0)