@@ -897,25 +897,29 @@ def pytest_pycollect_makeitem(collector, name, obj):
897
897
def test_issue2369_collect_module_fileext (self , pytester : Pytester ) -> None :
898
898
"""Ensure we can collect files with weird file extensions as Python
899
899
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
901
901
# Python source code whose file extension is ".narf".
902
902
pytester .makeconftest (
903
903
"""
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
905
908
from _pytest.python import Module
906
909
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 ())
915
918
916
919
def pytest_collect_file(file_path, parent):
917
920
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
+ """
919
923
)
920
924
pytester .makefile (
921
925
".narf" ,
0 commit comments