summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2021-10-07 22:55:15 (GMT)
committerGitHub <noreply@github.com>2021-10-07 22:55:15 (GMT)
commit06935bd68e3d89a4cc3f08c1d15eaa651b79a523 (patch)
treea5f247d98dc3702c4ba07d54e94655acd3f55aef
parent4346b81cc60ee23e0ae175300822615bd7d16431 (diff)
downloadcpython-06935bd68e3d89a4cc3f08c1d15eaa651b79a523.zip
cpython-06935bd68e3d89a4cc3f08c1d15eaa651b79a523.tar.gz
cpython-06935bd68e3d89a4cc3f08c1d15eaa651b79a523.tar.bz2
bpo-45337: Use the realpath of the new executable when creating a venv on Windows (GH-28663)
(cherry picked from commit 6811fdaec825bd6ab64e358a4b480108f5634d2d) Co-authored-by: Steve Dower <steve.dower@python.org>
-rw-r--r--Lib/test/test_venv.py10
-rw-r--r--Lib/venv/__init__.py24
-rw-r--r--Misc/NEWS.d/next/Windows/2021-09-30-23-17-27.bpo-45337.qg7U_h.rst4
3 files changed, 29 insertions, 9 deletions
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index ca003d5..480cb29 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -151,14 +151,20 @@ class BasicTest(BaseTest):
def test_upgrade_dependencies(self):
builder = venv.EnvBuilder()
bin_path = 'Scripts' if sys.platform == 'win32' else 'bin'
- python_exe = 'python.exe' if sys.platform == 'win32' else 'python'
+ python_exe = os.path.split(sys.executable)[1]
with tempfile.TemporaryDirectory() as fake_env_dir:
+ expect_exe = os.path.normcase(
+ os.path.join(fake_env_dir, bin_path, python_exe)
+ )
+ if sys.platform == 'win32':
+ expect_exe = os.path.normcase(os.path.realpath(expect_exe))
def pip_cmd_checker(cmd):
+ cmd[0] = os.path.normcase(cmd[0])
self.assertEqual(
cmd,
[
- os.path.join(fake_env_dir, bin_path, python_exe),
+ expect_exe,
'-m',
'pip',
'install',
diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py
index 8009deb..ce1f5d7 100644
--- a/Lib/venv/__init__.py
+++ b/Lib/venv/__init__.py
@@ -142,6 +142,20 @@ class EnvBuilder:
context.bin_name = binname
context.env_exe = os.path.join(binpath, exename)
create_if_needed(binpath)
+ # Assign and update the command to use when launching the newly created
+ # environment, in case it isn't simply the executable script (e.g. bpo-45337)
+ context.env_exec_cmd = context.env_exe
+ if sys.platform == 'win32':
+ # bpo-45337: Fix up env_exec_cmd to account for file system redirections.
+ # Some redirects only apply to CreateFile and not CreateProcess
+ real_env_exe = os.path.realpath(context.env_exe)
+ if os.path.normcase(real_env_exe) != os.path.normcase(context.env_exe):
+ logger.warning('Actual environment location may have moved due to '
+ 'redirects, links or junctions.\n'
+ ' Requested location: "%s"\n'
+ ' Actual location: "%s"',
+ context.env_exe, real_env_exe)
+ context.env_exec_cmd = real_env_exe
return context
def create_configuration(self, context):
@@ -293,8 +307,8 @@ class EnvBuilder:
# We run ensurepip in isolated mode to avoid side effects from
# environment vars, the current directory and anything else
# intended for the global Python environment
- cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
- '--default-pip']
+ cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade',
+ '--default-pip']
subprocess.check_output(cmd, stderr=subprocess.STDOUT)
def setup_scripts(self, context):
@@ -394,11 +408,7 @@ class EnvBuilder:
logger.debug(
f'Upgrading {CORE_VENV_DEPS} packages in {context.bin_path}'
)
- if sys.platform == 'win32':
- python_exe = os.path.join(context.bin_path, 'python.exe')
- else:
- python_exe = os.path.join(context.bin_path, 'python')
- cmd = [python_exe, '-m', 'pip', 'install', '--upgrade']
+ cmd = [context.env_exec_cmd, '-m', 'pip', 'install', '--upgrade']
cmd.extend(CORE_VENV_DEPS)
subprocess.check_call(cmd)
diff --git a/Misc/NEWS.d/next/Windows/2021-09-30-23-17-27.bpo-45337.qg7U_h.rst b/Misc/NEWS.d/next/Windows/2021-09-30-23-17-27.bpo-45337.qg7U_h.rst
new file mode 100644
index 0000000..007ee87
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2021-09-30-23-17-27.bpo-45337.qg7U_h.rst
@@ -0,0 +1,4 @@
+venv now warns when the created environment may need to be accessed at a
+different path, due to redirections, links or junctions. It also now
+correctly installs or upgrades components when the alternate path is
+required.