@@ -58,6 +58,10 @@ def tearDownModule():
5858 shutil .rmtree (TEMP_DIR )
5959
6060
61+ class CustomError (Exception ):
62+ pass
63+
64+
6165class TzPathUserMixin :
6266 """
6367 Adds a setUp() and tearDown() to make TZPATH manipulations thread-safe.
@@ -405,18 +409,23 @@ def test_time_fixed_offset(self):
405409 self .assertEqual (t .dst (), offset .dst )
406410
407411 def test_cache_exception (self ):
408- class ComparisonError (Exception ):
409- pass
410-
411412 class Incomparable (str ):
413+ eq_called = False
412414 def __eq__ (self , other ):
413- raise ComparisonError
414- def __hash__ ( self ):
415- return id ( self )
415+ self . eq_called = True
416+ raise CustomError
417+ __hash__ = str . __hash__
416418
417- key = Incomparable ("America/Los_Angeles" )
418- with self .assertRaises (ComparisonError ):
419- self .klass (key )
419+ key = "America/Los_Angeles"
420+ tz1 = self .klass (key )
421+ key = Incomparable (key )
422+ try :
423+ tz2 = self .klass (key )
424+ except CustomError :
425+ self .assertTrue (key .eq_called )
426+ else :
427+ self .assertFalse (key .eq_called )
428+ self .assertIs (tz2 , tz1 )
420429
421430
422431class CZoneInfoTest (ZoneInfoTest ):
@@ -1522,20 +1531,13 @@ def test_clear_cache_two_keys(self):
15221531 self .assertIs (tok0 , tok1 )
15231532
15241533 def test_clear_cache_refleak (self ):
1525- class ComparisonError (Exception ):
1526- pass
1527-
15281534 class Stringy (str ):
1529- def __new__ (cls , value ):
1530- rv = super ().__new__ (cls , value )
1531- rv .allow_comparisons = True
1532- return rv
1535+ allow_comparisons = True
15331536 def __eq__ (self , other ):
15341537 if not self .allow_comparisons :
1535- raise ComparisonError
1538+ raise CustomError
15361539 return super ().__eq__ (other )
1537- def __hash__ (self ):
1538- return hash (self [:])
1540+ __hash__ = str .__hash__
15391541
15401542 key = Stringy ("America/Los_Angeles" )
15411543 self .klass (key )
@@ -1545,7 +1547,7 @@ def __hash__(self):
15451547 # there is no guarantee that the key is even still in the cache,
15461548 # or that the key for the cache is the original `key` object.
15471549 self .klass .clear_cache (only_keys = "America/Los_Angeles" )
1548- except ComparisonError :
1550+ except CustomError :
15491551 pass
15501552
15511553
0 commit comments