Skip to content

Commit c1a0450

Browse files
jacobaustin123copybara-github
authored andcommitted
Fixed compatibility issues with unittest module and Python versions below 3.5.
PiperOrigin-RevId: 339713431 Change-Id: I80cb92f74a3958cde56ece04e393d1f2c079d86e
1 parent 5311d21 commit c1a0450

2 files changed

Lines changed: 31 additions & 18 deletions

File tree

fire/__main__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff 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)

fire/main_test.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff 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

9193
if __name__ == '__main__':

0 commit comments

Comments
 (0)