There was an error while loading. Please reload this page.
2 parents 66a78ce + f84b197 commit 7c376cbCopy full SHA for 7c376cb
2 files changed
pre_commit/languages/python.py
@@ -43,14 +43,13 @@ def _find_by_py_launcher(version): # pragma: no cover (windows only)
43
pass
44
45
46
-def _get_default_version(): # pragma: no cover (platform dependent)
+def _find_by_sys_executable():
47
def _norm(path):
48
_, exe = os.path.split(path.lower())
49
exe, _, _ = exe.partition('.exe')
50
if find_executable(exe) and exe not in {'python', 'pythonw'}:
51
return exe
52
53
- # First attempt from `sys.executable` (or the realpath)
54
# On linux, I see these common sys.executables:
55
#
56
# system `python`: /usr/bin/python -> python2.7
@@ -59,10 +58,18 @@ def _norm(path):
59
58
# virtualenv v -ppython2: v/bin/python -> python2
60
# virtualenv v -ppython2.7: v/bin/python -> python2.7
61
# virtualenv v -ppypy: v/bin/python -> v/bin/pypy
62
- for path in {sys.executable, os.path.realpath(sys.executable)}:
+ for path in (sys.executable, os.path.realpath(sys.executable)):
63
exe = _norm(path)
64
if exe:
65
+ return None
66
+
67
68
+def _get_default_version(): # pragma: no cover (platform dependent)
69
+ # First attempt from `sys.executable` (or the realpath)
70
+ exe = _find_by_sys_executable()
71
+ if exe:
72
+ return exe
73
74
# Next try the `pythonX.X` executable
75
exe = 'python{}.{}'.format(*sys.version_info)
tests/languages/python_test.py
@@ -32,3 +32,19 @@ def test_sys_executable_matches(v):
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)
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'),
+ ('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.object(os.path, 'realpath', return_value=realpath):
+ with mock.patch.object(python, 'find_executable', lambda x: x):
+ assert python._find_by_sys_executable() == expected
0 commit comments