Skip to content

Commit f45ccd8

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

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

Lib/test/test_venv.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,28 @@ 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+
755+
with tempfile.TemporaryDirectory() as python_symlink_dir:
756+
executable_symlink = os.path.join(
757+
python_symlink_dir, os.path.basename(sys.executable))
758+
os.symlink(os.path.abspath(sys.executable), executable_symlink)
759+
cmd = [executable_symlink, "-m", "venv", "--without-pip",
760+
self.env_dir]
761+
subprocess.check_call(cmd)
762+
data = self.get_text_file_contents('pyvenv.cfg')
763+
executable = sys._base_executable
764+
path = os.path.dirname(executable)
765+
self.assertIn('home = %s' % path, data)
766+
self.assertIn('executable = %s' %
767+
os.path.realpath(sys.executable), data)
768+
747769
@requireVenvCreate
748770
class EnsurePipTest(BaseTest):
749771
"""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)