Skip to content

gh-87298: Add tests for find_in_strong_cache() bug in _zoneinfo #24829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ def test_time_fixed_offset(self):
self.assertEqual(t.utcoffset(), offset.utcoffset)
self.assertEqual(t.dst(), offset.dst)

def test_cache_exception(self):
class ComparisonError(Exception):
pass

class Incomparable(str):
def __eq__(self, other):
raise ComparisonError
def __hash__(self):
return id(self)

key = Incomparable("America/Los_Angeles")
with self.assertRaises(ComparisonError):
self.klass(key)


class CZoneInfoTest(ZoneInfoTest):
module = c_zoneinfo
Expand Down Expand Up @@ -1504,6 +1518,33 @@ def test_clear_cache_two_keys(self):
self.assertIsNot(dub0, dub1)
self.assertIs(tok0, tok1)

def test_clear_cache_refleak(self):
class ComparisonError(Exception):
pass

class Stringy(str):
def __new__(cls, value):
rv = super().__new__(cls, value)
rv.allow_comparisons = True
return rv
def __eq__(self, other):
if not self.allow_comparisons:
raise ComparisonError
return super().__eq__(other)
def __hash__(self):
return hash(self[:])

key = Stringy("America/Los_Angeles")
self.klass(key)
key.allow_comparisons = False
try:
# Note: This is try/except rather than assertRaises because
# there is no guarantee that the key is even still in the cache,
# or that the key for the cache is the original `key` object.
self.klass.clear_cache(only_keys="America/Los_Angeles")
except ComparisonError:
pass


class CZoneInfoCacheTest(ZoneInfoCacheTest):
module = c_zoneinfo
Expand Down