@@ -400,6 +400,20 @@ def test_time_fixed_offset(self):
400
400
self .assertEqual (t .utcoffset (), offset .utcoffset )
401
401
self .assertEqual (t .dst (), offset .dst )
402
402
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
+
403
417
404
418
class CZoneInfoTest (ZoneInfoTest ):
405
419
module = c_zoneinfo
@@ -1377,6 +1391,33 @@ def test_clear_cache_two_keys(self):
1377
1391
self .assertIsNot (dub0 , dub1 )
1378
1392
self .assertIs (tok0 , tok1 )
1379
1393
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
+
1380
1421
1381
1422
class CZoneInfoCacheTest (ZoneInfoCacheTest ):
1382
1423
module = c_zoneinfo
0 commit comments