Skip to content

Commit fa2e154

Browse files
committed
Stabilize python default version lookup
For example, for sys.executable: /usr/bin/python3 -> python3.7 ...the default lookup may return either python3 or python3.7. Make the order deterministic by iterating over tuple, not set, of candidates.
1 parent 7c69730 commit fa2e154

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

pre_commit/languages/python.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ def _find_by_py_launcher(version): # pragma: no cover (windows only)
4343
pass
4444

4545

46-
def _get_default_version(): # pragma: no cover (platform dependent)
46+
def _find_by_sys_executable():
4747
def _norm(path):
4848
_, exe = os.path.split(path.lower())
4949
exe, _, _ = exe.partition('.exe')
5050
if find_executable(exe) and exe not in {'python', 'pythonw'}:
5151
return exe
5252

53-
# First attempt from `sys.executable` (or the realpath)
5453
# On linux, I see these common sys.executables:
5554
#
5655
# system `python`: /usr/bin/python -> python2.7
@@ -59,10 +58,17 @@ def _norm(path):
5958
# virtualenv v -ppython2: v/bin/python -> python2
6059
# virtualenv v -ppython2.7: v/bin/python -> python2.7
6160
# virtualenv v -ppypy: v/bin/python -> v/bin/pypy
62-
for path in {sys.executable, os.path.realpath(sys.executable)}:
61+
for path in (sys.executable, os.path.realpath(sys.executable)):
6362
exe = _norm(path)
6463
if exe:
6564
return exe
65+
return None
66+
67+
68+
def _get_default_version(): # pragma: no cover (platform dependent)
69+
70+
# First attempt from `sys.executable` (or the realpath)
71+
exe = _find_by_sys_executable()
6672

6773
# Next try the `pythonX.X` executable
6874
exe = 'python{}.{}'.format(*sys.version_info)

tests/languages/python_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ def test_sys_executable_matches(v):
3232
def test_sys_executable_matches_does_not_match(v):
3333
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
3434
assert not python._sys_executable_matches(v)
35+
36+
37+
@pytest.mark.parametrize(
38+
'exe,realpath,expected', (
39+
('/usr/bin/python3', '/usr/bin/python3.7', 'python3'),
40+
('/usr/bin/python', '/usr/bin/python3.7', 'python3.7'),
41+
('/usr/bin/python', '/usr/bin/python', None),
42+
('/usr/bin/python3.6m', '/usr/bin/python3.6m', 'python3.6m'),
43+
('v/bin/python', 'v/bin/pypy', 'pypy'),
44+
),
45+
)
46+
def test_find_by_sys_executable(exe, realpath, expected):
47+
with mock.patch.object(sys, 'executable', exe):
48+
with mock.patch('os.path.realpath', return_value=realpath):
49+
assert python._find_by_sys_executable() == expected

0 commit comments

Comments
 (0)