Skip to content

Commit 10417dd

Browse files
authored
bpo-41718: Reduce libregrtest runtest imports (GH-24980)
Move clear_caches() from libregrtest.refleak to libregrtest.utils to avoid importing libregrtest.refleak when it's not needed. clear_caches() now only calls re.purge() if 're' is in sys.modules.
1 parent 532e063 commit 10417dd

3 files changed

Lines changed: 107 additions & 97 deletions

File tree

Lib/test/libregrtest/refleak.py

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from inspect import isabstract
66
from test import support
77
from test.support import os_helper
8+
from test.libregrtest.utils import clear_caches
89

910
try:
1011
from _abc import _get_dump
@@ -181,102 +182,6 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
181182
clear_caches()
182183

183184

184-
def clear_caches():
185-
# Clear the warnings registry, so they can be displayed again
186-
for mod in sys.modules.values():
187-
if hasattr(mod, '__warningregistry__'):
188-
del mod.__warningregistry__
189-
190-
# Flush standard output, so that buffered data is sent to the OS and
191-
# associated Python objects are reclaimed.
192-
for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
193-
if stream is not None:
194-
stream.flush()
195-
196-
# Clear assorted module caches.
197-
# Don't worry about resetting the cache if the module is not loaded
198-
try:
199-
distutils_dir_util = sys.modules['distutils.dir_util']
200-
except KeyError:
201-
pass
202-
else:
203-
distutils_dir_util._path_created.clear()
204-
re.purge()
205-
206-
try:
207-
_strptime = sys.modules['_strptime']
208-
except KeyError:
209-
pass
210-
else:
211-
_strptime._regex_cache.clear()
212-
213-
try:
214-
urllib_parse = sys.modules['urllib.parse']
215-
except KeyError:
216-
pass
217-
else:
218-
urllib_parse.clear_cache()
219-
220-
try:
221-
urllib_request = sys.modules['urllib.request']
222-
except KeyError:
223-
pass
224-
else:
225-
urllib_request.urlcleanup()
226-
227-
try:
228-
linecache = sys.modules['linecache']
229-
except KeyError:
230-
pass
231-
else:
232-
linecache.clearcache()
233-
234-
try:
235-
mimetypes = sys.modules['mimetypes']
236-
except KeyError:
237-
pass
238-
else:
239-
mimetypes._default_mime_types()
240-
241-
try:
242-
filecmp = sys.modules['filecmp']
243-
except KeyError:
244-
pass
245-
else:
246-
filecmp._cache.clear()
247-
248-
try:
249-
struct = sys.modules['struct']
250-
except KeyError:
251-
pass
252-
else:
253-
struct._clearcache()
254-
255-
try:
256-
doctest = sys.modules['doctest']
257-
except KeyError:
258-
pass
259-
else:
260-
doctest.master = None
261-
262-
try:
263-
ctypes = sys.modules['ctypes']
264-
except KeyError:
265-
pass
266-
else:
267-
ctypes._reset_cache()
268-
269-
try:
270-
typing = sys.modules['typing']
271-
except KeyError:
272-
pass
273-
else:
274-
for f in typing._cleanups:
275-
f()
276-
277-
support.gc_collect()
278-
279-
280185
def warm_caches():
281186
# char cache
282187
s = bytes(range(256))

Lib/test/libregrtest/runtest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from test import support
1414
from test.support import import_helper
1515
from test.support import os_helper
16-
from test.libregrtest.refleak import dash_R, clear_caches
16+
from test.libregrtest.utils import clear_caches
1717
from test.libregrtest.save_env import saved_test_environment
1818
from test.libregrtest.utils import format_duration, print_warning
1919

@@ -226,6 +226,9 @@ def _runtest_inner2(ns, test_name):
226226

227227
the_module = importlib.import_module(abstest)
228228

229+
if ns.huntrleaks:
230+
from test.libregrtest.refleak import dash_R
231+
229232
# If the test has a test_main, that will run the appropriate
230233
# tests. If not, use normal unittest test loading.
231234
test_runner = getattr(the_module, "test_main", None)

Lib/test/libregrtest/utils.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,105 @@ def setup_unraisable_hook():
8484
global orig_unraisablehook
8585
orig_unraisablehook = sys.unraisablehook
8686
sys.unraisablehook = regrtest_unraisable_hook
87+
88+
89+
def clear_caches():
90+
# Clear the warnings registry, so they can be displayed again
91+
for mod in sys.modules.values():
92+
if hasattr(mod, '__warningregistry__'):
93+
del mod.__warningregistry__
94+
95+
# Flush standard output, so that buffered data is sent to the OS and
96+
# associated Python objects are reclaimed.
97+
for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
98+
if stream is not None:
99+
stream.flush()
100+
101+
# Clear assorted module caches.
102+
# Don't worry about resetting the cache if the module is not loaded
103+
try:
104+
distutils_dir_util = sys.modules['distutils.dir_util']
105+
except KeyError:
106+
pass
107+
else:
108+
distutils_dir_util._path_created.clear()
109+
110+
try:
111+
re = sys.modules['re']
112+
except KeyError:
113+
pass
114+
else:
115+
re.purge()
116+
117+
try:
118+
_strptime = sys.modules['_strptime']
119+
except KeyError:
120+
pass
121+
else:
122+
_strptime._regex_cache.clear()
123+
124+
try:
125+
urllib_parse = sys.modules['urllib.parse']
126+
except KeyError:
127+
pass
128+
else:
129+
urllib_parse.clear_cache()
130+
131+
try:
132+
urllib_request = sys.modules['urllib.request']
133+
except KeyError:
134+
pass
135+
else:
136+
urllib_request.urlcleanup()
137+
138+
try:
139+
linecache = sys.modules['linecache']
140+
except KeyError:
141+
pass
142+
else:
143+
linecache.clearcache()
144+
145+
try:
146+
mimetypes = sys.modules['mimetypes']
147+
except KeyError:
148+
pass
149+
else:
150+
mimetypes._default_mime_types()
151+
152+
try:
153+
filecmp = sys.modules['filecmp']
154+
except KeyError:
155+
pass
156+
else:
157+
filecmp._cache.clear()
158+
159+
try:
160+
struct = sys.modules['struct']
161+
except KeyError:
162+
pass
163+
else:
164+
struct._clearcache()
165+
166+
try:
167+
doctest = sys.modules['doctest']
168+
except KeyError:
169+
pass
170+
else:
171+
doctest.master = None
172+
173+
try:
174+
ctypes = sys.modules['ctypes']
175+
except KeyError:
176+
pass
177+
else:
178+
ctypes._reset_cache()
179+
180+
try:
181+
typing = sys.modules['typing']
182+
except KeyError:
183+
pass
184+
else:
185+
for f in typing._cleanups:
186+
f()
187+
188+
support.gc_collect()

0 commit comments

Comments
 (0)