镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import contextlib
import ensurepip
import json
import os
import os.path
import pathlib
Expand Down Expand Up @@ -846,6 +847,42 @@ def test_fish_activate_shadowed_builtins(self):
self.assertNotIn('LEAK', text)
self.assertIn('OLDSTATUS=7', text)

@requires_subprocess()
def test_powershell_activate_literal_paths(self):
shells = [path for name in ('pwsh', 'powershell.exe')
if (path := shutil.which(name)) is not None]
if not shells:
self.skipTest('PowerShell required for this test')

env_dir = self.get_env_file('venv[1]')
venv.EnvBuilder(prompt='from-config').create(env_dir)
activate = os.path.join(env_dir, self.bindir, 'Activate.ps1')
activate = activate.replace("'", "''")
test_script = self.get_env_file('test_literal_paths.ps1')
code = ('import json, os, sys; '
'print(json.dumps([sys.executable, '
"os.environ['VIRTUAL_ENV'], "
"os.environ['VIRTUAL_ENV_PROMPT']]))")
with open(test_script, 'w', encoding='utf-8-sig') as f:
f.write(
"$ErrorActionPreference = 'Stop'\n"
f". '{activate}'\n"
f'{self.exe} -c "{code}"\n'
'deactivate\n'
)
for shell in shells:
with self.subTest(shell=shell):
out, err = check_output(
[shell, '-NoProfile', '-NonInteractive',
'-ExecutionPolicy', 'Bypass', '-File', test_script],
)
self.assertFalse(err)
executable, active_dir, prompt = json.loads(out)
self.assertEqual(pathlib.Path(active_dir), pathlib.Path(env_dir))
self.assertEqual(prompt, 'from-config')
self.assertEqual(pathlib.Path(executable),
pathlib.Path(env_dir, self.bindir, self.exe))

# gh-124651: test quoted strings on Windows
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
def test_special_chars_windows(self):
Expand Down
6 changes: 3 additions & 3 deletions Lib/venv/scripts/common/Activate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ function Get-PyVenvConfig(
Write-Verbose "Given ConfigDir=$ConfigDir, obtain values in pyvenv.cfg"

# Ensure the file exists, and issue a warning if it doesn't (but still allow the function to continue).
$pyvenvConfigPath = Join-Path -Resolve -Path $ConfigDir -ChildPath 'pyvenv.cfg' -ErrorAction Continue
$pyvenvConfigPath = Resolve-Path -LiteralPath (Join-Path -Path $ConfigDir -ChildPath 'pyvenv.cfg') -ErrorAction Continue

# An empty map will be returned if no config file is found.
$pyvenvConfig = @{ }

if ($pyvenvConfigPath) {

Write-Verbose "File exists, parse `key = value` lines"
$pyvenvConfigContent = Get-Content -Path $pyvenvConfigPath
$pyvenvConfigContent = Get-Content -LiteralPath $pyvenvConfigPath.ProviderPath

$pyvenvConfigContent | ForEach-Object {
$keyval = $PSItem -split "\s*=\s*", 2
Expand All @@ -168,7 +168,7 @@ function Get-PyVenvConfig(

# Determine the containing directory of this script
$VenvExecPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$VenvExecDir = Get-Item -Path $VenvExecPath
$VenvExecDir = Get-Item -LiteralPath $VenvExecPath

Write-Verbose "Activation script is located in path: '$VenvExecPath'"
Write-Verbose "VenvExecDir Fullname: '$($VenvExecDir.FullName)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix PowerShell activation of :mod:`venv` environments with square brackets
in their paths, including reading the intended ``pyvenv.cfg`` file.
Loading