Skip to content

bpo-41718: Reduce libregrtest runtest imports #24980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
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
97 changes: 1 addition & 96 deletions Lib/test/libregrtest/refleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from inspect import isabstract
from test import support
from test.support import os_helper
from test.libregrtest.utils import clear_caches

try:
from _abc import _get_dump
Expand Down Expand Up @@ -181,102 +182,6 @@ def dash_R_cleanup(fs, ps, pic, zdc, abcs):
clear_caches()


def clear_caches():
# Clear the warnings registry, so they can be displayed again
for mod in sys.modules.values():
if hasattr(mod, '__warningregistry__'):
del mod.__warningregistry__

# Flush standard output, so that buffered data is sent to the OS and
# associated Python objects are reclaimed.
for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
if stream is not None:
stream.flush()

# Clear assorted module caches.
# Don't worry about resetting the cache if the module is not loaded
try:
distutils_dir_util = sys.modules['distutils.dir_util']
except KeyError:
pass
else:
distutils_dir_util._path_created.clear()
re.purge()

try:
_strptime = sys.modules['_strptime']
except KeyError:
pass
else:
_strptime._regex_cache.clear()

try:
urllib_parse = sys.modules['urllib.parse']
except KeyError:
pass
else:
urllib_parse.clear_cache()

try:
urllib_request = sys.modules['urllib.request']
except KeyError:
pass
else:
urllib_request.urlcleanup()

try:
linecache = sys.modules['linecache']
except KeyError:
pass
else:
linecache.clearcache()

try:
mimetypes = sys.modules['mimetypes']
except KeyError:
pass
else:
mimetypes._default_mime_types()

try:
filecmp = sys.modules['filecmp']
except KeyError:
pass
else:
filecmp._cache.clear()

try:
struct = sys.modules['struct']
except KeyError:
pass
else:
struct._clearcache()

try:
doctest = sys.modules['doctest']
except KeyError:
pass
else:
doctest.master = None

try:
ctypes = sys.modules['ctypes']
except KeyError:
pass
else:
ctypes._reset_cache()

try:
typing = sys.modules['typing']
except KeyError:
pass
else:
for f in typing._cleanups:
f()

support.gc_collect()


def warm_caches():
# char cache
s = bytes(range(256))
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/libregrtest/runtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from test import support
from test.support import import_helper
from test.support import os_helper
from test.libregrtest.refleak import dash_R, clear_caches
from test.libregrtest.utils import clear_caches
from test.libregrtest.save_env import saved_test_environment
from test.libregrtest.utils import format_duration, print_warning

Expand Down Expand Up @@ -226,6 +226,9 @@ def _runtest_inner2(ns, test_name):

the_module = importlib.import_module(abstest)

if ns.huntrleaks:
from test.libregrtest.refleak import dash_R

# If the test has a test_main, that will run the appropriate
# tests. If not, use normal unittest test loading.
test_runner = getattr(the_module, "test_main", None)
Expand Down
102 changes: 102 additions & 0 deletions Lib/test/libregrtest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,105 @@ def setup_unraisable_hook():
global orig_unraisablehook
orig_unraisablehook = sys.unraisablehook
sys.unraisablehook = regrtest_unraisable_hook


def clear_caches():
# Clear the warnings registry, so they can be displayed again
for mod in sys.modules.values():
if hasattr(mod, '__warningregistry__'):
del mod.__warningregistry__

# Flush standard output, so that buffered data is sent to the OS and
# associated Python objects are reclaimed.
for stream in (sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__):
if stream is not None:
stream.flush()

# Clear assorted module caches.
# Don't worry about resetting the cache if the module is not loaded
try:
distutils_dir_util = sys.modules['distutils.dir_util']
except KeyError:
pass
else:
distutils_dir_util._path_created.clear()

try:
re = sys.modules['re']
except KeyError:
pass
else:
re.purge()

try:
_strptime = sys.modules['_strptime']
except KeyError:
pass
else:
_strptime._regex_cache.clear()

try:
urllib_parse = sys.modules['urllib.parse']
except KeyError:
pass
else:
urllib_parse.clear_cache()

try:
urllib_request = sys.modules['urllib.request']
except KeyError:
pass
else:
urllib_request.urlcleanup()

try:
linecache = sys.modules['linecache']
except KeyError:
pass
else:
linecache.clearcache()

try:
mimetypes = sys.modules['mimetypes']
except KeyError:
pass
else:
mimetypes._default_mime_types()

try:
filecmp = sys.modules['filecmp']
except KeyError:
pass
else:
filecmp._cache.clear()

try:
struct = sys.modules['struct']
except KeyError:
pass
else:
struct._clearcache()

try:
doctest = sys.modules['doctest']
except KeyError:
pass
else:
doctest.master = None

try:
ctypes = sys.modules['ctypes']
except KeyError:
pass
else:
ctypes._reset_cache()

try:
typing = sys.modules['typing']
except KeyError:
pass
else:
for f in typing._cleanups:
f()

support.gc_collect()