11from  __future__ import  print_function 
22import  os 
33import  sys 
4- import  imp 
4+ import  importlib . util 
55import  inspect 
66from  RLTest .utils  import  Colors 
77
@@ -17,8 +17,9 @@ def __init__(self, filename, symbol, modulename):
1717        self .name  =  '{}:{}' .format (self .modulename , symbol )
1818
1919    def  initialize (self ):
20-         module_file  =  open (self .filename )
21-         module  =  imp .load_module (self .modulename , module_file , self .filename , ('.py' , 'r' , imp .PY_SOURCE ))
20+         module_spec  =  importlib .util .spec_from_file_location (self .modulename , self .filename )
21+         module  =  importlib .util .module_from_spec (module_spec )
22+         module_spec .loader .exec_module (module )
2223        obj  =  getattr (module , self .symbol )
2324        self .target  =  obj 
2425
@@ -50,8 +51,9 @@ def __init__(self, filename, symbol, modulename, functions):
5051        self .name  =  '{}:{}' .format (self .modulename , symbol )
5152
5253    def  initialize (self ):
53-         module_file  =  open (self .filename )
54-         module  =  imp .load_module (self .modulename , module_file , self .filename , ('.py' , 'r' , imp .PY_SOURCE ))
54+         module_spec  =  importlib .util .spec_from_file_location (self .modulename , self .filename )
55+         module  =  importlib .util .module_from_spec (module_spec )
56+         module_spec .loader .exec_module (module )
5557        obj  =  getattr (module , self .symbol )
5658        self .clsname  =  obj .__name__ 
5759        self .cls  =  obj 
@@ -113,25 +115,24 @@ def load_spec(self, arg):
113115    def  load_files (self , module_dir , module_name , toplevel_filter = None , subfilter = None ):
114116        filename  =  '%s/%s.py'  %  (module_dir , module_name )
115117        try :
116-             with  open (filename , 'r' ) as  module_file :
117-                 try :
118-                     module  =  imp .load_module (module_name , module_file , filename ,
119-                                              ('.py' , 'r' , imp .PY_SOURCE ))
120-                     for  symbol  in  dir (module ):
121-                         if  not  self .filter_modulevar (symbol , toplevel_filter ):
122-                             continue 
123- 
124-                         obj  =  getattr (module , symbol )
125-                         if  inspect .isclass (obj ):
126-                             methnames  =  [mname  for  mname  in  dir (obj )
127-                                          if  self .filter_method (mname , subfilter )]
128-                             self .tests .append (TestClass (filename , symbol , module_name , methnames ))
129-                         elif  inspect .isfunction (obj ):
130-                             self .tests .append (TestFunction (filename , symbol , module_name ))
131-                 except  Exception  as  x :
132-                     print (Colors .Red ("Problems in file %s: %s"  %  (filename , x )))
133-         except :
118+             module_spec  =  importlib .util .spec_from_file_location (module_name , filename )
119+             module  =  importlib .util .module_from_spec (module_spec )
120+             module_spec .loader .exec_module (module )
121+             for  symbol  in  dir (module ):
122+                 if  not  self .filter_modulevar (symbol , toplevel_filter ):
123+                     continue 
124+ 
125+                 obj  =  getattr (module , symbol )
126+                 if  inspect .isclass (obj ):
127+                     methnames  =  [mname  for  mname  in  dir (obj )
128+                                     if  self .filter_method (mname , subfilter )]
129+                     self .tests .append (TestClass (filename , symbol , module_name , methnames ))
130+                 elif  inspect .isfunction (obj ):
131+                     self .tests .append (TestFunction (filename , symbol , module_name ))
132+         except  FileNotFoundError :
134133            print (Colors .Red ("File %s not found: skipping"  %  filename ))
134+         except  Exception  as  x :
135+             print (Colors .Red ("Problems in file %s: %s"  %  (filename , x )))
135136
136137    def  scan_dir (self , testdir ):
137138        for  filename  in  os .listdir (testdir ):
0 commit comments