File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,7 +42,17 @@ def import_from_file_path(path):
4242 """Performs a module import given the filename."""
4343 module_name = os .path .basename (path )
4444
45- if sys .version_info .major == 3 :
45+ if sys .version_info .major == 3 and sys .version_info .minor < 5 :
46+ loader = importlib .machinery .SourceFileLoader (
47+ fullname = module_name ,
48+ path = path ,
49+ )
50+ spec = importlib .util .spec_from_loader (loader .name , loader , origin = path )
51+ module = importlib .util .module_from_spec (spec )
52+ sys .modules [loader .name ] = module
53+ loader .exec_module (module )
54+
55+ elif sys .version_info .major == 3 :
4656 from importlib import util # pylint: disable=g-import-not-at-top
4757 spec = util .spec_from_file_location (module_name , path )
4858
@@ -51,6 +61,7 @@ def import_from_file_path(path):
5161
5262 module = util .module_from_spec (spec )
5363 spec .loader .exec_module (module ) # pytype: disable=attribute-error
64+
5465 else :
5566 import imp # pylint: disable=g-import-not-at-top
5667 module = imp .load_source (module_name , path )
Original file line number Diff line number Diff line change @@ -65,27 +65,29 @@ def testFileNameFailure(self):
6565 def testFileNameModuleDuplication (self ):
6666 # Confirm that a file that masks a module still loads the module.
6767 with self .assertOutputMatches ('gettempdir' ):
68- file = self .create_tempfile ('tempfile' )
68+ dirname = os .path .dirname (self .file .name )
69+ with testutils .ChangeDirectory (dirname ):
70+ with open ('tempfile' , 'w' ):
71+ __main__ .main ([
72+ '__main__.py' ,
73+ 'tempfile' ,
74+ ])
6975
70- with testutils .ChangeDirectory (os .path .dirname (file .full_path )):
71- __main__ .main ([
72- '__main__.py' ,
73- 'tempfile' ,
74- ])
76+ os .remove ('tempfile' )
7577
7678 def testFileNameModuleFileFailure (self ):
7779 # Confirm that an invalid file that masks a non-existent module fails.
78- with self .assertRaisesWithLiteralMatch (
79- ValueError , 'Fire can only be called on .py files.' ):
80- file = self . create_tempfile ( 'foobar' )
81-
82- with testutils . ChangeDirectory ( os . path . dirname ( file . full_path ) ):
83- assert os . path . exists ( 'foobar' )
84-
85- __main__ . main ([
86- '__main__.py' ,
87- 'foobar' ,
88- ] )
80+ with self .assertRaisesRegex ( ValueError ,
81+ r 'Fire can only be called on \ .py files\ .' ):
82+ dirname = os . path . dirname ( self . file . name )
83+ with testutils . ChangeDirectory ( dirname ):
84+ with open ( 'foobar' , 'w' ):
85+ __main__ . main ([
86+ '__main__.py' ,
87+ 'foobar' ,
88+ ])
89+
90+ os . remove ( 'foobar' )
8991
9092
9193if __name__ == '__main__' :
You can’t perform that action at this time.
0 commit comments