diff --git a/.travis.yml b/.travis.yml index d3ec4166..8f91d702 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,8 +16,6 @@ matrix: install: pip install coveralls tox script: tox before_install: - # work around https://github.com/travis-ci/travis-ci/issues/8363 - - which python3.5 || (pyenv install 3.5.4 && pyenv global system 3.5.4) - git --version - | if [ "$LATEST_GIT" = "1" ]; then diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7af11c42..da27dec6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,9 +5,8 @@ - The complete test suite depends on having at least the following installed (possibly not a complete list) - git (A sufficiently newer version is required to run pre-push tests) - - python - - python3.4 (Required by a test which checks different python versions) - - python3.5 (Required by a test which checks different python versions) + - python2 (Required by a test which checks different python versions) + - python3 (Required by a test which checks different python versions) - tox (or virtualenv) - ruby + gem - docker diff --git a/pre_commit/languages/python.py b/pre_commit/languages/python.py index 32852409..8d891aa3 100644 --- a/pre_commit/languages/python.py +++ b/pre_commit/languages/python.py @@ -9,6 +9,7 @@ from pre_commit.envcontext import UNSET from pre_commit.envcontext import Var from pre_commit.languages import helpers from pre_commit.parse_shebang import find_executable +from pre_commit.util import CalledProcessError from pre_commit.util import clean_path_on_failure from pre_commit.util import cmd_output from pre_commit.xargs import xargs @@ -40,6 +41,17 @@ def in_env(repo_cmd_runner, language_version): yield +def _find_by_py_launcher(version): # pragma: no cover (windows only) + if version.startswith('python'): + try: + return cmd_output( + 'py', '-{}'.format(version[len('python'):]), + '-c', 'import sys; print(sys.executable)', + )[1].strip() + except CalledProcessError: + pass + + def _get_default_version(): # pragma: no cover (platform dependent) def _norm(path): _, exe = os.path.split(path.lower()) @@ -66,6 +78,9 @@ def _get_default_version(): # pragma: no cover (platform dependent) if find_executable(exe): return exe + if _find_by_py_launcher(exe): + return exe + # Give a best-effort try for windows if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))): return exe @@ -99,6 +114,10 @@ def norm_version(version): if version_exec and version_exec != version: return version_exec + version_exec = _find_by_py_launcher(version) + if version_exec: + return version_exec + # If it is in the form pythonx.x search in the default # place on windows if version.startswith('python'): diff --git a/testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml b/testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml index 0320f025..2c237009 100644 --- a/testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml +++ b/testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml @@ -2,5 +2,5 @@ name: Python 3 Hook entry: python3-hook language: python - language_version: python3.5 + language_version: python3 files: \.py$ diff --git a/testing/resources/python3_hooks_repo/.pre-commit-hooks.yaml b/testing/resources/python3_hooks_repo/.pre-commit-hooks.yaml index 0320f025..2c237009 100644 --- a/testing/resources/python3_hooks_repo/.pre-commit-hooks.yaml +++ b/testing/resources/python3_hooks_repo/.pre-commit-hooks.yaml @@ -2,5 +2,5 @@ name: Python 3 Hook entry: python3-hook language: python - language_version: python3.5 + language_version: python3 files: \.py$ diff --git a/testing/resources/python3_hooks_repo/python3_hook/main.py b/testing/resources/python3_hooks_repo/python3_hook/main.py index 117c7969..04f974e6 100644 --- a/testing/resources/python3_hooks_repo/python3_hook/main.py +++ b/testing/resources/python3_hooks_repo/python3_hook/main.py @@ -4,7 +4,7 @@ import sys def func(): - print('{}.{}'.format(*sys.version_info[:2])) + print(sys.version_info[0]) print(repr(sys.argv[1:])) print('Hello World') return 0 diff --git a/tests/repository_test.py b/tests/repository_test.py index dd20dd0e..2b9ab6e5 100644 --- a/tests/repository_test.py +++ b/tests/repository_test.py @@ -130,8 +130,8 @@ def test_switch_language_versions_doesnt_clobber(tempdir_factory, store): assert ret[0] == 0 assert _norm_out(ret[1]) == expected_output - run_on_version('python3.4', b'3.4\n[]\nHello World\n') - run_on_version('python3.5', b'3.5\n[]\nHello World\n') + run_on_version('python2', b'2\n[]\nHello World\n') + run_on_version('python3', b'3\n[]\nHello World\n') @pytest.mark.integration @@ -140,7 +140,7 @@ def test_versioned_python_hook(tempdir_factory, store): tempdir_factory, store, 'python3_hooks_repo', 'python3-hook', [os.devnull], - b"3.5\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n", + b"3\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n", )