Skip to content

Commit d7a2010

Browse files
committed
pythongh-106045: Fix venv creation from a python executable symlink
1 parent e19103a commit d7a2010

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

Lib/test/test_venv.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,27 @@ def test_cli_without_scm_ignore_files(self):
744744
with self.assertRaises(FileNotFoundError):
745745
self.get_text_file_contents('.gitignore')
746746

747+
@requires_subprocess()
748+
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
749+
def test_executable_symlink(self):
750+
"""
751+
Test creation using a symlink to python executable.
752+
"""
753+
rmtree(self.env_dir)
754+
with tempfile.TemporaryDirectory() as symlink_dir:
755+
executable_symlink = os.path.join(
756+
symlink_dir, os.path.basename(sys.executable))
757+
os.symlink(os.path.abspath(sys.executable), executable_symlink)
758+
cmd = [executable_symlink, "-m", "venv", "--without-pip",
759+
self.env_dir]
760+
subprocess.check_call(cmd)
761+
data = self.get_text_file_contents('pyvenv.cfg')
762+
executable = sys._base_executable
763+
path = os.path.dirname(executable)
764+
self.assertIn('home = %s' % path, data)
765+
self.assertIn('executable = %s' %
766+
os.path.realpath(sys.executable), data)
767+
747768
@requireVenvCreate
748769
class EnsurePipTest(BaseTest):
749770
"""Test venv module installation of pip."""

Lib/venv/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def create_if_needed(d):
137137
'Python interpreter. Provide an explicit path or '
138138
'check that your PATH environment variable is '
139139
'correctly set.')
140-
dirname, exename = os.path.split(os.path.abspath(executable))
140+
dirname, exename = os.path.split(os.path.realpath(executable))
141141
if sys.platform == 'win32':
142142
# Always create the simplest name in the venv. It will either be a
143143
# link back to executable, or a copy of the appropriate launcher

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ Eric Daniel
415415
Scott David Daniels
416416
Derzsi Dániel
417417
Lawrence D'Anna
418+
Matthieu Darbois
418419
Ben Darnell
419420
Kushal Das
420421
Jonathan Dasteel
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``venv`` creation from a python executable symlink. Patch by Matthieu
2+
Darbois.

0 commit comments

Comments
 (0)