8000 bpo-41011: venv -- add more variables to pyvenv.cfg (GH-30382) by akulakov · Pull Request #30382 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-41011: venv -- add more variables to pyvenv.cfg (GH-30382) 8000 #30382

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 17 commits into from
Jan 7, 2022
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
Prev Previous commit
Next Next commit
unskip tests
  • Loading branch information
akulakov committed Jan 5, 2022
commit abef4f4a112267b74b4e51ed7118f63e3d68f631
13 changes: 7 additions & 6 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from test.support.os_helper import (can_symlink, EnvironmentVarGuard, rmtree)
import unittest
import venv
from unittest.mock import patch
from unittest.mock import patch, Mock

try:
import ctypes
Expand Down Expand Up @@ -93,7 +93,6 @@ def isdir(self, *args):
fn = self.get_env_file(*args)
self.assertTrue(os.path.isdir(fn))

@unittest.skip
def test_defaults(self):
"""
Test the create function with default arguments.
Expand Down Expand Up @@ -127,15 +126,18 @@ def test_defaults(self):

def test_config_file_command_key(self):
attrs = [
('symlinks', '--use-symlinks'),
('with_pip', '--with-pip'),
('symlinks', '--symlinks'),
('with_pip', '--without-pip'),
('system_site_packages', '--system-site-packages'),
('clear', '--clear'),
('upgrade', '--upgrade'),
('upgrade_deps', '--upgrade-deps'),
]
for attr, opt in attrs:
rmtree(self.env_dir)
self.run_with_capture(venv.create, self.env_dir, **{attr:True})
b = venv.EnvBuilder(**{attr: False if attr=='with_pip' else True})
b.upgrade_dependencies = Mock() # avoid pip command to upgrade deps
self.run_with_capture(b.create, self.env_dir)
data = self.get_text_file_contents('pyvenv.cfg')
self.assertRegex(data, rf'command = .* {opt}')

Expand Down Expand Up @@ -430,7 +432,6 @@ def test_macos_env(self):
'import os; print("__PYVENV_LAUNCHER__" in os.environ)'])
self.assertEqual(out.strip(), 'False'.encode())

@unittest.skip
@requireVenvCreate
class EnsurePipTest(BaseTest):
"""Test venv module installation of pip."""
Expand Down
16 changes: 8 additions & 8 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,20 @@ def create_configuration(self, context):
f.write('executable = %s\n' % os.path.realpath(sys.executable))
Copy link
Contributor

Choose a reason for hiding this comment

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

(I appreciate that this PR is long since merged, but just following-up on a surprising pyvenv.cfg file that I saw recently, and git-blamed it back to here)

I'm not at all sure why realpath is important here - the sys.executable should be good enough when being called directly. When called through the EnvBuilder, I don't see a reason for this either. 🤔

Copy link
Member

Choose a reason for hiding this comment

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

You're right, it is a difference when you're not on Windows. But thinking about it, you could view the config as recording the actual interpreter used instead of the path that could change on you in the future (e.g., some symlink later gets changed to point to another interpreter).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for this perspective (I hadn't considered it)! I'm not sure that using a config file to log information about how a venv was created is a good idea, but can appreciate the idea. Since this is a machine readable file, I would be concerned that it eventually gets used for non-log/audit purposes.

I've been digging into symlink issues with venv a lot recently, and think calling realpath is quite problematic in other context (i.e. when determining home). More details of that in #106045.

No follow-up needed here - just flagging it, and depending on the outcome of the linked issue, perhaps in the future I may request that this information become non realpath, or under a "history of how this venv was created" section of the config (perhaps in the form of a comment).

Copy link
Contributor

Choose a reason for hiding this comment

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

args = []
if self.symlinks:
args.append('--use-symlinks')
if self.with_pip:
args.append('--with-pip')
args.append('--symlinks')
if not self.with_pip:
args.append('--without-pip')
if self.system_site_packages:
args.append('--system-site-packages')
if self.clear:
args.append('--clear')
if self.upgrade:
args.append('--upgrade')
if self.upgrade_deps:
args.append('--upgrade-deps')
args.append(context.env_dir)
args = ' '.join(args)
f.write(f'command = {sys.executable} -m {args}\n')
f.write(f'command = {sys.executable} -m venv {args}\n')

if os.name != 'nt':
def symlink_or_copy(self, src, dst, relative_symlinks_ok=False):
Expand Down Expand Up @@ -430,13 +432,11 @@ def upgrade_dependencies(self, context):


def create(env_dir, system_site_packages=False, clear=False,
symlinks=False, with_pip=False, prompt=None, upgrade_deps=False,
upgrade=False):
symlinks=False, with_pip=False, prompt=None, upgrade_deps=False):
"""Create a virtual environment in a directory."""
builder = EnvBuilder(system_site_packages=system_site_packages,
clear=clear, symlinks=symlinks, with_pip=with_pip,
prompt=prompt, upgrade_deps=upgrade_deps,
upgrade=upgrade)
prompt=prompt, upgrade_deps=upgrade_deps)
builder.create(env_dir)

def main(args=None):
Expand Down
0