Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize python default version lookup #1117

Merged
merged 5 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 9 additions & 3 deletions pre_commit/languages/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ def _find_by_py_launcher(version): # pragma: no cover (windows only)
pass


def _get_default_version(): # pragma: no cover (platform dependent)
def _find_by_sys_executable():
def _norm(path):
_, exe = os.path.split(path.lower())
exe, _, _ = exe.partition('.exe')
if find_executable(exe) and exe not in {'python', 'pythonw'}:
return exe

# First attempt from `sys.executable` (or the realpath)
# On linux, I see these common sys.executables:
#
# system `python`: /usr/bin/python -> python2.7
Expand All @@ -59,10 +58,17 @@ def _norm(path):
# virtualenv v -ppython2: v/bin/python -> python2
# virtualenv v -ppython2.7: v/bin/python -> python2.7
# virtualenv v -ppypy: v/bin/python -> v/bin/pypy
for path in {sys.executable, os.path.realpath(sys.executable)}:
for path in (sys.executable, os.path.realpath(sys.executable)):
exe = _norm(path)
if exe:
return exe
return None


def _get_default_version(): # pragma: no cover (platform dependent)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

# First attempt from `sys.executable` (or the realpath)
exe = _find_by_sys_executable()
asottile marked this conversation as resolved.
Show resolved Hide resolved

# Next try the `pythonX.X` executable
exe = 'python{}.{}'.format(*sys.version_info)
Expand Down
21 changes: 21 additions & 0 deletions tests/languages/python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,24 @@ def test_sys_executable_matches(v):
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)


@pytest.mark.parametrize(
'exe,realpath,expected', (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer a tuple of strings here instead of stringly typed parametrize as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'exe,realpath,expected', (
('exe', 'realpath', 'expected'), (

('/usr/bin/python3', '/usr/bin/python3.7', 'python3'),
('/usr/bin/python', '/usr/bin/python3.7', 'python3.7'),
('/usr/bin/python', '/usr/bin/python', None),
('/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):
def mocked_find_executable(exe):
return exe.rpartition('/')[2]
with mock.patch.object(sys, 'executable', exe):
with mock.patch('os.path.realpath', return_value=realpath):
with mock.patch(
'pre_commit.parse_shebang.find_executable',
side_effect=mocked_find_executable,
):
assert python._find_by_sys_executable() == expected