Skip to content

Commit ad36335

Browse files
committed
gh-106045: Fix venv creation from a python executable symlink
1 parent 7e894c2 commit ad36335

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

Lib/test/test_venv.py

+23
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,29 @@ def test_venv_same_path(self):
775775
else:
776776
self.assertFalse(same_path(path1, path2))
777777

778+
@requires_subprocess()
779+
@unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
780+
@unittest.skipUnless(can_symlink(), 'Needs symlinks')
781+
def test_executable_symlink(self):
782+
"""
783+
Test creation using a symlink to python executable.
784+
"""
785+
rmtree(self.env_dir)
786+
with tempfile.TemporaryDirectory() as symlink_dir:
787+
executable_symlink = os.path.join(
788+
os.path.realpath(symlink_dir),
789+
os.path.basename(sys.executable))
790+
os.symlink(os.path.abspath(sys.executable), executable_symlink)
791+
cmd = [executable_symlink, "-m", "venv", "--without-pip",
792+
self.env_dir]
793+
subprocess.check_call(cmd)
794+
data = self.get_text_file_contents('pyvenv.cfg')
795+
executable = sys._base_executable
796+
path = os.path.dirname(executable)
797+
self.assertIn('home = %s' % path, data)
798+
self.assertIn('executable = %s' %
799+
os.path.realpath(sys.executable), data)
800+
778801
@requireVenvCreate
779802
class EnsurePipTest(BaseTest):
780803
"""Test venv module installation of pip."""

Lib/venv/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def create_if_needed(d):
164164
'Python interpreter. Provide an explicit path or '
165165
'check that your PATH environment variable is '
166166
'correctly set.')
167-
dirname, exename = os.path.split(os.path.abspath(executable))
167+
dirname, exename = os.path.split(os.path.realpath(executable))
168168
if sys.platform == 'win32':
169169
# Always create the simplest name in the venv. It will either be a
170170
# link back to executable, or a copy of the appropriate launcher

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ Eric Daniel
416416
Scott David Daniels
417417
Derzsi Dániel
418418
Lawrence D'Anna
419+
Matthieu Darbois
419420
Ben Darnell
420421
Kushal Das
421422
Jonathan Dasteel
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)