@@ -1292,6 +1292,7 @@ def runtest_inner(test, verbose, quiet,
12921292 else :
12931293 # Always import it from the test package
12941294 abstest = 'test.' + test
1295+ clear_caches ()
12951296 with saved_test_environment (test , verbose , quiet , pgo = pgo ) as environment :
12961297 start_time = time .time ()
12971298 the_module = importlib .import_module (abstest )
@@ -1464,17 +1465,9 @@ def check_alloc_deltas(deltas):
14641465
14651466def dash_R_cleanup (fs , ps , pic , zdc , abcs ):
14661467 import gc , copyreg
1467- import _strptime , linecache
1468- import urllib .parse , urllib .request , mimetypes , doctest
1469- import struct , filecmp , collections .abc
1470- from distutils .dir_util import _path_created
1468+ import collections .abc
14711469 from weakref import WeakSet
14721470
1473- # Clear the warnings registry, so they can be displayed again
1474- for mod in sys .modules .values ():
1475- if hasattr (mod , '__warningregistry__' ):
1476- del mod .__warningregistry__
1477-
14781471 # Restore some original values.
14791472 warnings .filters [:] = fs
14801473 copyreg .dispatch_table .clear ()
@@ -1501,27 +1494,98 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
15011494 obj ._abc_cache .clear ()
15021495 obj ._abc_negative_cache .clear ()
15031496
1497+ clear_caches ()
1498+
1499+ # Collect cyclic trash and read memory statistics immediately after.
1500+ func1 = sys .getallocatedblocks
1501+ func2 = sys .gettotalrefcount
1502+ gc .collect ()
1503+ return func1 (), func2 ()
1504+
1505+ def clear_caches ():
1506+ import gc
1507+
1508+ # Clear the warnings registry, so they can be displayed again
1509+ for mod in sys .modules .values ():
1510+ if hasattr (mod , '__warningregistry__' ):
1511+ del mod .__warningregistry__
1512+
15041513 # Flush standard output, so that buffered data is sent to the OS and
15051514 # associated Python objects are reclaimed.
15061515 for stream in (sys .stdout , sys .stderr , sys .__stdout__ , sys .__stderr__ ):
15071516 if stream is not None :
15081517 stream .flush ()
15091518
15101519 # Clear assorted module caches.
1511- _path_created .clear ()
1520+ # Don't worry about resetting the cache if the module is not loaded
1521+ try :
1522+ distutils_dir_util = sys .modules ['distutils.dir_util' ]
1523+ except KeyError :
1524+ pass
1525+ else :
1526+ distutils_dir_util ._path_created .clear ()
1527+
15121528 re .purge ()
1513- _strptime ._regex_cache .clear ()
1514- urllib .parse .clear_cache ()
1515- urllib .request .urlcleanup ()
1516- linecache .clearcache ()
1517- mimetypes ._default_mime_types ()
1518- filecmp ._cache .clear ()
1519- struct ._clearcache ()
1520- doctest .master = None
1529+
15211530 try :
1522- import ctypes
1523- except ImportError :
1524- # Don't worry about resetting the cache if ctypes is not supported
1531+ _strptime = sys .modules ['_strptime' ]
1532+ except KeyError :
1533+ pass
1534+ else :
1535+ _strptime ._regex_cache .clear ()
1536+
1537+ try :
1538+ urllib_parse = sys .modules ['urllib.parse' ]
1539+ except KeyError :
1540+ pass
1541+ else :
1542+ urllib_parse .clear_cache ()
1543+
1544+ try :
1545+ urllib_request = sys .modules ['urllib.request' ]
1546+ except KeyError :
1547+ pass
1548+ else :
1549+ urllib_request .urlcleanup ()
1550+
1551+ try :
1552+ linecache = sys .modules ['linecache' ]
1553+ except KeyError :
1554+ pass
1555+ else :
1556+ linecache .clearcache ()
1557+
1558+ try :
1559+ mimetypes = sys .modules ['mimetypes' ]
1560+ except KeyError :
1561+ pass
1562+ else :
1563+ mimetypes ._default_mime_types ()
1564+
1565+ try :
1566+ filecmp = sys .modules ['filecmp' ]
1567+ except KeyError :
1568+ pass
1569+ else :
1570+ filecmp ._cache .clear ()
1571+
1572+ try :
1573+ struct = sys .modules ['struct' ]
1574+ except KeyError :
1575+ pass
1576+ else :
1577+ struct ._clearcache ()
1578+
1579+ try :
1580+ doctest = sys .modules ['doctest' ]
1581+ except KeyError :
1582+ pass
1583+ else :
1584+ doctest .master = None
1585+
1586+ try :
1587+ ctypes = sys .modules ['ctypes' ]
1588+ except KeyError :
15251589 pass
15261590 else :
15271591 ctypes ._reset_cache ()
@@ -1534,11 +1598,7 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
15341598 for f in typing ._cleanups :
15351599 f ()
15361600
1537- # Collect cyclic trash and read memory statistics immediately after.
1538- func1 = sys .getallocatedblocks
1539- func2 = sys .gettotalrefcount
15401601 gc .collect ()
1541- return func1 (), func2 ()
15421602
15431603def warm_caches ():
15441604 # char cache
0 commit comments