Skip to content

Commit c945dba

Browse files
committed
testing/python/collect: replace use of deprecated/removed imp module
1 parent 52fa735 commit c945dba

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

testing/python/collect.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -897,25 +897,29 @@ def pytest_pycollect_makeitem(collector, name, obj):
897897
def test_issue2369_collect_module_fileext(self, pytester: Pytester) -> None:
898898
"""Ensure we can collect files with weird file extensions as Python
899899
modules (#2369)"""
900-
# We'll implement a little finder and loader to import files containing
900+
# Implement a little meta path finder to import files containing
901901
# Python source code whose file extension is ".narf".
902902
pytester.makeconftest(
903903
"""
904-
import sys, os, imp
904+
import sys
905+
import os.path
906+
from importlib.util import spec_from_loader
907+
from importlib.machinery import SourceFileLoader
905908
from _pytest.python import Module
906909
907-
class Loader(object):
908-
def load_module(self, name):
909-
return imp.load_source(name, name + ".narf")
910-
class Finder(object):
911-
def find_module(self, name, path=None):
912-
if os.path.exists(name + ".narf"):
913-
return Loader()
914-
sys.meta_path.append(Finder())
910+
class MetaPathFinder:
911+
def find_spec(self, fullname, path, target=None):
912+
if os.path.exists(fullname + ".narf"):
913+
return spec_from_loader(
914+
fullname,
915+
SourceFileLoader(fullname, fullname + ".narf"),
916+
)
917+
sys.meta_path.append(MetaPathFinder())
915918
916919
def pytest_collect_file(file_path, parent):
917920
if file_path.suffix == ".narf":
918-
return Module.from_parent(path=file_path, parent=parent)"""
921+
return Module.from_parent(path=file_path, parent=parent)
922+
"""
919923
)
920924
pytester.makefile(
921925
".narf",

0 commit comments

Comments
 (0)