Skip to content

Commit 4748470

Browse files
committed
Use the modern way to load modules by file name.
Python 3.10 finally got super-noisy about load_module, which has been deprecated since 3.4! https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module
1 parent f08d8a8 commit 4748470

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

coverage/backward.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,17 @@ def import_local_file(modname, modfile=None):
245245
246246
"""
247247
try:
248-
from importlib.machinery import SourceFileLoader
248+
import importlib.util as importlib_util
249249
except ImportError:
250-
SourceFileLoader = None
250+
importlib_util = None
251251

252252
if modfile is None:
253253
modfile = modname + '.py'
254-
if SourceFileLoader:
255-
# pylint: disable=no-value-for-parameter, deprecated-method
256-
mod = SourceFileLoader(modname, modfile).load_module()
254+
if importlib_util:
255+
spec = importlib_util.spec_from_file_location(modname, modfile)
256+
mod = importlib_util.module_from_spec(spec)
257+
sys.modules[modname] = mod
258+
spec.loader.exec_module(mod)
257259
else:
258260
for suff in imp.get_suffixes(): # pragma: part covered
259261
if suff[0] == '.py':

setup.cfg

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
addopts = -q -n3 --strict --force-flaky --no-flaky-report -rfe --failed-first
33
markers =
44
expensive: too slow to run during "make smoke"
5-
# How come this warning is suppressed successfully here, but not in conftest.py??
5+
6+
# How come these warnings are suppressed successfully here, but not in conftest.py??
67
filterwarnings =
78
ignore:dns.hash module will be removed:DeprecationWarning
89
ignore:Using or importing the ABCs:DeprecationWarning
10+
911
# xfail tests that pass should fail the test suite
1012
xfail_strict=true
1113

tests/conftest.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,32 @@ def set_warnings():
2525
warnings.simplefilter("default")
2626
warnings.simplefilter("once", DeprecationWarning)
2727

28-
# A warning to suppress:
28+
# Warnings to suppress:
29+
# How come these warnings are successfully suppressed here, but not in setup.cfg??
30+
2931
# setuptools/py33compat.py:54: DeprecationWarning: The value of convert_charrefs will become
3032
# True in 3.5. You are encouraged to set the value explicitly.
3133
# unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape)
32-
# How come this warning is successfully suppressed here, but not in setup.cfg??
3334
warnings.filterwarnings(
3435
"ignore",
3536
category=DeprecationWarning,
36-
message="The value of convert_charrefs will become True in 3.5.",
37+
message=r"The value of convert_charrefs will become True in 3.5.",
3738
)
39+
3840
warnings.filterwarnings(
3941
"ignore",
4042
category=DeprecationWarning,
41-
message=".* instead of inspect.getfullargspec",
43+
message=r".* instead of inspect.getfullargspec",
4244
)
45+
46+
# <frozen importlib._bootstrap>:681:
47+
# ImportWarning: VendorImporter.exec_module() not found; falling back to load_module()
48+
warnings.filterwarnings(
49+
"ignore",
50+
category=ImportWarning,
51+
message=r".*exec_module\(\) not found; falling back to load_module\(\)",
52+
)
53+
4354
if env.PYPY3:
4455
# pypy3 warns about unclosed files a lot.
4556
warnings.filterwarnings("ignore", r".*unclosed file", category=ResourceWarning)

0 commit comments

Comments
 (0)