Skip to content

Commit 32d6523

Browse files
committed
Use sys.executable if it matches the requested version
1 parent 1cf4b54 commit 32d6523

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

pre_commit/languages/python.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,26 @@ def get_default_version():
8989
return get_default_version()
9090

9191

92+
def _sys_executable_matches(version):
93+
if version == 'python':
94+
return True
95+
elif not version.startswith('python'):
96+
return False
97+
98+
try:
99+
info = tuple(int(p) for p in version[len('python'):].split('.'))
100+
except ValueError:
101+
return False
102+
103+
return sys.version_info[:len(info)] == info
104+
105+
92106
def norm_version(version):
93107
if os.name == 'nt': # pragma: no cover (windows)
108+
# first see if our current executable is appropriate
109+
if _sys_executable_matches(version):
110+
return sys.executable
111+
94112
# Try looking up by name
95113
version_exec = find_executable(version)
96114
if version_exec and version_exec != version:

tests/languages/python_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from __future__ import unicode_literals
33

44
import os.path
5+
import sys
6+
7+
import mock
8+
import pytest
59

610
from pre_commit.languages import python
711

@@ -16,3 +20,15 @@ def test_norm_version_expanduser():
1620
expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
1721
result = python.norm_version(path)
1822
assert result == expected_path
23+
24+
25+
@pytest.mark.parametrize('v', ('python3.6', 'python3', 'python'))
26+
def test_sys_executable_matches(v):
27+
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
28+
assert python._sys_executable_matches(v)
29+
30+
31+
@pytest.mark.parametrize('v', ('notpython', 'python3.x'))
32+
def test_sys_executable_matches_does_not_match(v):
33+
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
34+
assert not python._sys_executable_matches(v)

0 commit comments

Comments
 (0)