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

Fix discovery when name is non-spec format on PATH #1748

Merged
merged 2 commits into from
Mar 24, 2020
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
1 change: 1 addition & 0 deletions docs/changelog/1746.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix discovery of interpreter by name from ``PATH`` that does not match a spec format - by :user:`gaborbernat`.
20 changes: 13 additions & 7 deletions src/virtualenv/discovery/py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,19 @@ def clear_cache(cls, app_data):

def satisfies(self, spec, impl_must_match):
"""check if a given specification can be satisfied by the this python interpreter instance"""
if spec.path and self.executable == os.path.abspath(spec.path):
return True # if the path is a our own executable path we're done

if spec.path is not None: # if path set, and is not our original executable name, this does not match
root, _ = os.path.splitext(os.path.basename(self.original_executable))
if root != spec.path:
return False
if spec.path:
if self.executable == os.path.abspath(spec.path):
return True # if the path is a our own executable path we're done
if not spec.is_abs:
# if path set, and is not our original executable name, this does not match
basename = os.path.basename(self.original_executable)
spec_path = spec.path
if sys.platform == "win32":
basename, suffix = os.path.splitext(basename)
if spec_path.endswith(suffix):
spec_path = spec_path[: -len(suffix)]
if basename != spec_path:
return False

if impl_must_match:
if spec.implementation is not None and spec.implementation.lower() != self.implementation.lower():
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/discovery/py_info/test_py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,23 @@ def test_py_info_ignores_distutils_config(monkeypatch, tmp_path):
distutils = py_info.distutils_install
for key, value in distutils.items():
assert not value.startswith(str(tmp_path)), "{}={}".format(key, value)


def test_discover_exe_on_path_non_spec_name_match(mocker):
suffixed_name = "python{}.{}m".format(CURRENT.version_info.major, CURRENT.version_info.minor)
if sys.platform == "win32":
suffixed_name += Path(CURRENT.original_executable).suffix
spec = PythonSpec.from_string_spec(suffixed_name)
mocker.patch.object(CURRENT, "original_executable", str(Path(CURRENT.executable).parent / suffixed_name))
assert CURRENT.satisfies(spec, impl_must_match=True) is True


def test_discover_exe_on_path_non_spec_name_not_match(mocker):
suffixed_name = "python{}.{}m".format(CURRENT.version_info.major, CURRENT.version_info.minor)
if sys.platform == "win32":
suffixed_name += Path(CURRENT.original_executable).suffix
spec = PythonSpec.from_string_spec(suffixed_name)
mocker.patch.object(
CURRENT, "original_executable", str(Path(CURRENT.executable).parent / "e{}".format(suffixed_name))
)
assert CURRENT.satisfies(spec, impl_must_match=True) is False