Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pre_commit/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,11 @@ def _sys_executable_matches(version: str) -> bool:
return sys.version_info[:len(info)] == info


def norm_version(version: str) -> str:
if version == C.DEFAULT:
return os.path.realpath(sys.executable)

# first see if our current executable is appropriate
if _sys_executable_matches(version):
return sys.executable
def norm_version(version: str) -> Optional[str]:
if version == C.DEFAULT: # use virtualenv's default
return None
elif _sys_executable_matches(version): # virtualenv defaults to our exe
return None

if os.name == 'nt': # pragma: no cover (windows)
version_exec = _find_by_py_launcher(version)
Expand Down Expand Up @@ -194,8 +192,10 @@ def install_environment(
additional_dependencies: Sequence[str],
) -> None:
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
venv_cmd = [sys.executable, '-mvirtualenv', envdir]
python = norm_version(version)
venv_cmd = (sys.executable, '-mvirtualenv', envdir, '-p', python)
if python is not None:
venv_cmd.extend(('-p', python))
install_cmd = ('python', '-mpip', 'install', '.', *additional_dependencies)

with clean_path_on_failure(envdir):
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ def test_norm_version_expanduser():


def test_norm_version_of_default_is_sys_executable():
assert python.norm_version('default') == os.path.realpath(sys.executable)
assert python.norm_version('default') is None


@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)
assert python.norm_version(v) is None


@pytest.mark.parametrize('v', ('notpython', 'python3.x'))
Expand Down