Skip to content
Merged
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
43 changes: 43 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def tearDownModule():
shutil.rmtree(TEMP_DIR)


class CustomError(Exception):
pass


class TzPathUserMixin:
"""
Adds a setUp() and tearDown() to make TZPATH manipulations thread-safe.
Expand Down Expand Up @@ -404,6 +408,25 @@ def test_time_fixed_offset(self):
self.assertEqual(t.utcoffset(), offset.utcoffset)
self.assertEqual(t.dst(), offset.dst)

def test_cache_exception(self):
class Incomparable(str):
eq_called = False
def __eq__(self, other):
self.eq_called = True
raise CustomError
__hash__ = str.__hash__

key = "America/Los_Angeles"
tz1 = self.klass(key)
key = Incomparable(key)
try:
tz2 = self.klass(key)
except CustomError:
self.assertTrue(key.eq_called)
else:
self.assertFalse(key.eq_called)
self.assertIs(tz2, tz1)


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

def test_clear_cache_refleak(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently I wrote this function 4 years ago, but coming back to it now it is opaque as heck, so we should probably describe what this test is about.

Suggested change
def test_clear_cache_refleak(self):
def test_clear_cache_refleak(self):
# bpo-43132: The portion of the C implementation of ZoneInfo that
# ejects specific keys from the strong cache (i.e. the LRU cache) was
# leaking references if comparing to the key object failed; because the
# strong cache is not currently implemented for subclasses, this test
# only really applies to the C implementation of the ZoneInfo class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may also want to skip this test if self.klass is not exactly the C implementation of ZoneInfo, since otherwise it's a no-op.

class Stringy(str):
allow_comparisons = True
def __eq__(self, other):
if not self.allow_comparisons:
raise CustomError
return super().__eq__(other)
__hash__ = str.__hash__

key = Stringy("America/Los_Angeles")
self.klass(key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To increase the chance that this actually works:

Suggested change
self.klass(key)
self.klass.clear_cache(only_keys="America/Los_Angeles")
self.klass(key)

key.allow_comparisons = False
try:
# Note: This is try/except rather than assertRaises because
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize that I wrote this function, but it kind of bothers me that this can silently become a no-op (and in fact this is silently a no-op for all subclasses ZoneInfo). I think that the only way around it is fairly annoying if we want to allow tests to run in parallel, which is that we would need to acquire a lock around anything that deals with the cache.

# 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 CustomError:
pass


class CZoneInfoCacheTest(ZoneInfoCacheTest):
module = c_zoneinfo
Expand Down
Loading