Skip to content

Fix VIRTUALENV_PYTHON environment lookup #1998

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

Merged
merged 5 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion docs/changelog/1998.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Fix processing of the ``VIRTUALENV_PYTHON`` environment variable
Fix processing of the ``VIRTUALENV_PYTHON`` environment variable and make it
multi-value as well (separated by comma) - by :user:`pneff`.
6 changes: 6 additions & 0 deletions docs/cli_interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ variable ``VIRTUALENV_PYTHON`` like:

env VIRTUALENV_PYTHON=/opt/python-3.8/bin/python virtualenv

Multiple values can be provided separated with a comma:

.. code-block:: console

env VIRTUALENV_PYTHON=/opt/python-3.8/bin/python,python3.8 virtualenv

This also works for appending command line options, like :option:`extra-search-dir`, where a literal newline
is used to separate the values:

Expand Down
21 changes: 18 additions & 3 deletions src/virtualenv/config/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,31 @@ def _validate(self):
""""""

def convert(self, value, flatten=True):
if isinstance(value, (str, bytes)):
value = filter(None, [x.strip() for x in value.splitlines()])
values = list(value)
values = self.split_values(value)
result = []
for value in values:
sub_values = value.split(os.pathsep)
result.extend(sub_values)
converted = [self.as_type(i) for i in result]
return converted

def split_values(self, value):
"""Split the provided value into a list.

For strings this is a comma-separated. For more complex types (`Path`
for example) this is newline-separated.
"""
if isinstance(value, (str, bytes)):
if self.as_type is str:
# Simple split
value = value.split(",")
else:
value = value.splitlines()

value = filter(None, [x.strip() for x in value])

return list(value)


def convert(value, as_type, source):
"""Convert the value as a given type where the value comes from the given source"""
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/config/test_env_var.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def test_python_via_env_var(monkeypatch):
assert options.python == ["python3"]


def test_python_multi_value_via_env_var(monkeypatch):
options = VirtualEnvOptions()
monkeypatch.setenv(str("VIRTUALENV_PYTHON"), str("python3,python2"))
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, I did not realize that VIRTUALENV_EXTRA_SEARCH_DIR accepts newlines. Can we instead make this one use the same logic, and perhaps alter the logic for that to also accept the comma, not just newlines?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, what we have is correct, but instead of having different split character depending on the type, let's always accept both. I don't expect people to use comma in paths, so IMHO, this is alright.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am pushing a commit which implements this change. I opted for only accepting one split option at a time - so if the environment variable contains newlines, then we don't also accept commas. Do you agree with that decision?

I quickly went back to the legacy code as well, and it seems that newline support has only been added with the rewrite. The previous version had space separation for path names. Thus I would expect the backwards compatibility impact of this change to be minimal.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense. Can you add a test demonstrating/validating this? Also, the documentation should explicitly state that only of the two will be accepted, and not both at the same time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those tests have been added now.

session_via_cli(["venv"], options=options)
assert options.python == ["python3", "python2"]


def test_extra_search_dir_via_env_var(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
value = "a{}0{}b{}c".format(os.linesep, os.linesep, os.pathsep)
Expand Down