mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Merge pull request #914 from pre-commit/self_exe_python
Use sys.executable if it matches the requested version
This commit is contained in:
commit
f1f299c4b8
2 changed files with 34 additions and 0 deletions
|
|
@ -89,8 +89,26 @@ def get_default_version():
|
|||
return get_default_version()
|
||||
|
||||
|
||||
def _sys_executable_matches(version):
|
||||
if version == 'python':
|
||||
return True
|
||||
elif not version.startswith('python'):
|
||||
return False
|
||||
|
||||
try:
|
||||
info = tuple(int(p) for p in version[len('python'):].split('.'))
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
return sys.version_info[:len(info)] == info
|
||||
|
||||
|
||||
def norm_version(version):
|
||||
if os.name == 'nt': # pragma: no cover (windows)
|
||||
# first see if our current executable is appropriate
|
||||
if _sys_executable_matches(version):
|
||||
return sys.executable
|
||||
|
||||
# Try looking up by name
|
||||
version_exec = find_executable(version)
|
||||
if version_exec and version_exec != version:
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@ from __future__ import absolute_import
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from pre_commit.languages import python
|
||||
|
||||
|
|
@ -16,3 +20,15 @@ def test_norm_version_expanduser():
|
|||
expected_path = home + '/.pyenv/versions/3.4.3/bin/python'
|
||||
result = python.norm_version(path)
|
||||
assert result == expected_path
|
||||
|
||||
|
||||
@pytest.mark.parametrize('v', ('python3.6', 'python3', 'python'))
|
||||
def test_sys_executable_matches(v):
|
||||
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
|
||||
assert python._sys_executable_matches(v)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('v', ('notpython', 'python3.x'))
|
||||
def test_sys_executable_matches_does_not_match(v):
|
||||
with mock.patch.object(sys, 'version_info', (3, 6, 7)):
|
||||
assert not python._sys_executable_matches(v)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue