mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
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.
This commit is contained in:
parent
7c69730ad2
commit
fa2e154b41
2 changed files with 24 additions and 3 deletions
|
|
@ -43,14 +43,13 @@ def _find_by_py_launcher(version): # pragma: no cover (windows only)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _get_default_version(): # pragma: no cover (platform dependent)
|
def _find_by_sys_executable():
|
||||||
def _norm(path):
|
def _norm(path):
|
||||||
_, exe = os.path.split(path.lower())
|
_, exe = os.path.split(path.lower())
|
||||||
exe, _, _ = exe.partition('.exe')
|
exe, _, _ = exe.partition('.exe')
|
||||||
if find_executable(exe) and exe not in {'python', 'pythonw'}:
|
if find_executable(exe) and exe not in {'python', 'pythonw'}:
|
||||||
return exe
|
return exe
|
||||||
|
|
||||||
# First attempt from `sys.executable` (or the realpath)
|
|
||||||
# On linux, I see these common sys.executables:
|
# On linux, I see these common sys.executables:
|
||||||
#
|
#
|
||||||
# system `python`: /usr/bin/python -> python2.7
|
# system `python`: /usr/bin/python -> python2.7
|
||||||
|
|
@ -59,10 +58,17 @@ def _get_default_version(): # pragma: no cover (platform dependent)
|
||||||
# virtualenv v -ppython2: v/bin/python -> python2
|
# virtualenv v -ppython2: v/bin/python -> python2
|
||||||
# virtualenv v -ppython2.7: v/bin/python -> python2.7
|
# virtualenv v -ppython2.7: v/bin/python -> python2.7
|
||||||
# virtualenv v -ppypy: v/bin/python -> v/bin/pypy
|
# virtualenv v -ppypy: v/bin/python -> v/bin/pypy
|
||||||
for path in {sys.executable, os.path.realpath(sys.executable)}:
|
for path in (sys.executable, os.path.realpath(sys.executable)):
|
||||||
exe = _norm(path)
|
exe = _norm(path)
|
||||||
if exe:
|
if exe:
|
||||||
return exe
|
return exe
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_default_version(): # pragma: no cover (platform dependent)
|
||||||
|
|
||||||
|
# First attempt from `sys.executable` (or the realpath)
|
||||||
|
exe = _find_by_sys_executable()
|
||||||
|
|
||||||
# Next try the `pythonX.X` executable
|
# Next try the `pythonX.X` executable
|
||||||
exe = 'python{}.{}'.format(*sys.version_info)
|
exe = 'python{}.{}'.format(*sys.version_info)
|
||||||
|
|
|
||||||
|
|
@ -32,3 +32,18 @@ def test_sys_executable_matches(v):
|
||||||
def test_sys_executable_matches_does_not_match(v):
|
def test_sys_executable_matches_does_not_match(v):
|
||||||
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
|
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
|
||||||
assert not python._sys_executable_matches(v)
|
assert not python._sys_executable_matches(v)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'exe,realpath,expected', (
|
||||||
|
('/usr/bin/python3', '/usr/bin/python3.7', 'python3'),
|
||||||
|
('/usr/bin/python', '/usr/bin/python3.7', 'python3.7'),
|
||||||
|
('/usr/bin/python', '/usr/bin/python', None),
|
||||||
|
('/usr/bin/python3.6m', '/usr/bin/python3.6m', 'python3.6m'),
|
||||||
|
('v/bin/python', 'v/bin/pypy', 'pypy'),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_find_by_sys_executable(exe, realpath, expected):
|
||||||
|
with mock.patch.object(sys, 'executable', exe):
|
||||||
|
with mock.patch('os.path.realpath', return_value=realpath):
|
||||||
|
assert python._find_by_sys_executable() == expected
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue