parent
103d8ab374
commit
aad73ea0b9
@ -0,0 +1,445 @@
|
|||||||
|
From 82dbaffa36fe317e331b2d22c5efbb6a0b6c7b19 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Lumir Balhar <lbalhar@redhat.com>
|
||||||
|
Date: Tue, 8 Oct 2024 09:15:09 +0200
|
||||||
|
Subject: [PATCH] Prevent command injection
|
||||||
|
|
||||||
|
---
|
||||||
|
docs/changelog/2768.bugfix.rst | 2 ++
|
||||||
|
src/virtualenv/activation/bash/activate.sh | 9 +++++----
|
||||||
|
src/virtualenv/activation/batch/__init__.py | 4 ++++
|
||||||
|
src/virtualenv/activation/cshell/activate.csh | 10 +++++-----
|
||||||
|
src/virtualenv/activation/fish/activate.fish | 8 ++++----
|
||||||
|
src/virtualenv/activation/nushell/__init__.py | 19 ++++++++++++++++++
|
||||||
|
src/virtualenv/activation/nushell/activate.nu | 8 ++++----
|
||||||
|
.../activation/powershell/__init__.py | 12 +++++++++++
|
||||||
|
.../activation/powershell/activate.ps1 | 6 +++---
|
||||||
|
src/virtualenv/activation/python/__init__.py | 6 +++++-
|
||||||
|
.../activation/python/activate_this.py | 6 +++---
|
||||||
|
src/virtualenv/activation/via_template.py | 15 ++++++++++++--
|
||||||
|
tests/conftest.py | 6 +++++-
|
||||||
|
tests/unit/activation/conftest.py | 3 +--
|
||||||
|
tests/unit/activation/test_batch.py | 10 +++++-----
|
||||||
|
tests/unit/activation/test_powershell.py | 20 ++++++++++++++-----
|
||||||
|
16 files changed, 105 insertions(+), 39 deletions(-)
|
||||||
|
create mode 100644 docs/changelog/2768.bugfix.rst
|
||||||
|
|
||||||
|
diff --git a/docs/changelog/2768.bugfix.rst b/docs/changelog/2768.bugfix.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..7651eb6
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/docs/changelog/2768.bugfix.rst
|
||||||
|
@@ -0,0 +1,2 @@
|
||||||
|
+Properly quote string placeholders in activation script templates to mitigate
|
||||||
|
+potential command injection - by :user:`y5c4l3`.
|
||||||
|
diff --git a/src/virtualenv/activation/bash/activate.sh b/src/virtualenv/activation/bash/activate.sh
|
||||||
|
index fb40db6..d047ea4 100644
|
||||||
|
--- a/src/virtualenv/activation/bash/activate.sh
|
||||||
|
+++ b/src/virtualenv/activation/bash/activate.sh
|
||||||
|
@@ -44,14 +44,14 @@ deactivate () {
|
||||||
|
# unset irrelevant variables
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
-VIRTUAL_ENV='__VIRTUAL_ENV__'
|
||||||
|
+VIRTUAL_ENV=__VIRTUAL_ENV__
|
||||||
|
if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then
|
||||||
|
VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV")
|
||||||
|
fi
|
||||||
|
export VIRTUAL_ENV
|
||||||
|
|
||||||
|
_OLD_VIRTUAL_PATH="$PATH"
|
||||||
|
-PATH="$VIRTUAL_ENV/__BIN_NAME__:$PATH"
|
||||||
|
+PATH="$VIRTUAL_ENV/"__BIN_NAME__":$PATH"
|
||||||
|
export PATH
|
||||||
|
|
||||||
|
# unset PYTHONHOME if set
|
||||||
|
@@ -62,8 +62,9 @@ fi
|
||||||
|
|
||||||
|
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
|
||||||
|
_OLD_VIRTUAL_PS1="${PS1-}"
|
||||||
|
- if [ "x__VIRTUAL_PROMPT__" != x ] ; then
|
||||||
|
- PS1="(__VIRTUAL_PROMPT__) ${PS1-}"
|
||||||
|
+ if [ "x"__VIRTUAL_PROMPT__ != x ] ; then
|
||||||
|
+ PROMPT=__VIRTUAL_PROMPT__
|
||||||
|
+ PS1="(${PROMPT}) ${PS1-}"
|
||||||
|
else
|
||||||
|
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
|
||||||
|
fi
|
||||||
|
diff --git a/src/virtualenv/activation/batch/__init__.py b/src/virtualenv/activation/batch/__init__.py
|
||||||
|
index 13ba097..b3e8540 100644
|
||||||
|
--- a/src/virtualenv/activation/batch/__init__.py
|
||||||
|
+++ b/src/virtualenv/activation/batch/__init__.py
|
||||||
|
@@ -13,6 +13,10 @@ class BatchActivator(ViaTemplateActivator):
|
||||||
|
yield "deactivate.bat"
|
||||||
|
yield "pydoc.bat"
|
||||||
|
|
||||||
|
+ @staticmethod
|
||||||
|
+ def quote(string):
|
||||||
|
+ return string
|
||||||
|
+
|
||||||
|
def instantiate_template(self, replacements, template, creator):
|
||||||
|
# ensure the text has all newlines as \r\n - required by batch
|
||||||
|
base = super().instantiate_template(replacements, template, creator)
|
||||||
|
diff --git a/src/virtualenv/activation/cshell/activate.csh b/src/virtualenv/activation/cshell/activate.csh
|
||||||
|
index 837dcda..fcec426 100644
|
||||||
|
--- a/src/virtualenv/activation/cshell/activate.csh
|
||||||
|
+++ b/src/virtualenv/activation/cshell/activate.csh
|
||||||
|
@@ -10,15 +10,15 @@ alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PA
|
||||||
|
# Unset irrelevant variables.
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
-setenv VIRTUAL_ENV '__VIRTUAL_ENV__'
|
||||||
|
+setenv VIRTUAL_ENV __VIRTUAL_ENV__
|
||||||
|
|
||||||
|
set _OLD_VIRTUAL_PATH="$PATH:q"
|
||||||
|
-setenv PATH "$VIRTUAL_ENV:q/__BIN_NAME__:$PATH:q"
|
||||||
|
+setenv PATH "$VIRTUAL_ENV:q/"__BIN_NAME__":$PATH:q"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-if ('__VIRTUAL_PROMPT__' != "") then
|
||||||
|
- set env_name = '(__VIRTUAL_PROMPT__) '
|
||||||
|
+if (__VIRTUAL_PROMPT__ != "") then
|
||||||
|
+ set env_name = __VIRTUAL_PROMPT__
|
||||||
|
else
|
||||||
|
set env_name = '('"$VIRTUAL_ENV:t:q"') '
|
||||||
|
endif
|
||||||
|
@@ -42,7 +42,7 @@ if ( $do_prompt == "1" ) then
|
||||||
|
if ( "$prompt:q" =~ *"$newline:q"* ) then
|
||||||
|
:
|
||||||
|
else
|
||||||
|
- set prompt = "$env_name:q$prompt:q"
|
||||||
|
+ set prompt = '('"$env_name:q"') '"$prompt:q"
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
diff --git a/src/virtualenv/activation/fish/activate.fish b/src/virtualenv/activation/fish/activate.fish
|
||||||
|
index 62f631e..3637d55 100644
|
||||||
|
--- a/src/virtualenv/activation/fish/activate.fish
|
||||||
|
+++ b/src/virtualenv/activation/fish/activate.fish
|
||||||
|
@@ -57,7 +57,7 @@ end
|
||||||
|
# Unset irrelevant variables.
|
||||||
|
deactivate nondestructive
|
||||||
|
|
||||||
|
-set -gx VIRTUAL_ENV '__VIRTUAL_ENV__'
|
||||||
|
+set -gx VIRTUAL_ENV __VIRTUAL_ENV__
|
||||||
|
|
||||||
|
# https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
|
||||||
|
if test (echo $FISH_VERSION | head -c 1) -lt 3
|
||||||
|
@@ -65,7 +65,7 @@ if test (echo $FISH_VERSION | head -c 1) -lt 3
|
||||||
|
else
|
||||||
|
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||||
|
end
|
||||||
|
-set -gx PATH "$VIRTUAL_ENV"'/__BIN_NAME__' $PATH
|
||||||
|
+set -gx PATH "$VIRTUAL_ENV"'/'__BIN_NAME__ $PATH
|
||||||
|
|
||||||
|
# Unset `$PYTHONHOME` if set.
|
||||||
|
if set -q PYTHONHOME
|
||||||
|
@@ -87,8 +87,8 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||||
|
|
||||||
|
# Prompt override provided?
|
||||||
|
# If not, just prepend the environment name.
|
||||||
|
- if test -n '__VIRTUAL_PROMPT__'
|
||||||
|
- printf '(%s) ' '__VIRTUAL_PROMPT__'
|
||||||
|
+ if test -n __VIRTUAL_PROMPT__
|
||||||
|
+ printf '(%s) ' __VIRTUAL_PROMPT__
|
||||||
|
else
|
||||||
|
printf '(%s) ' (basename "$VIRTUAL_ENV")
|
||||||
|
end
|
||||||
|
diff --git a/src/virtualenv/activation/nushell/__init__.py b/src/virtualenv/activation/nushell/__init__.py
|
||||||
|
index 4e2ea77..c96af22 100644
|
||||||
|
--- a/src/virtualenv/activation/nushell/__init__.py
|
||||||
|
+++ b/src/virtualenv/activation/nushell/__init__.py
|
||||||
|
@@ -5,6 +5,25 @@ class NushellActivator(ViaTemplateActivator):
|
||||||
|
def templates(self):
|
||||||
|
yield "activate.nu"
|
||||||
|
|
||||||
|
+ @staticmethod
|
||||||
|
+ def quote(string):
|
||||||
|
+ """
|
||||||
|
+ Nushell supports raw strings like: r###'this is a string'###.
|
||||||
|
+
|
||||||
|
+ This method finds the maximum continuous sharps in the string and then
|
||||||
|
+ quote it with an extra sharp.
|
||||||
|
+ """
|
||||||
|
+ max_sharps = 0
|
||||||
|
+ current_sharps = 0
|
||||||
|
+ for char in string:
|
||||||
|
+ if char == "#":
|
||||||
|
+ current_sharps += 1
|
||||||
|
+ max_sharps = max(current_sharps, max_sharps)
|
||||||
|
+ else:
|
||||||
|
+ current_sharps = 0
|
||||||
|
+ wrapping = "#" * (max_sharps + 1)
|
||||||
|
+ return f"r{wrapping}'{string}'{wrapping}"
|
||||||
|
+
|
||||||
|
def replacements(self, creator, dest_folder): # noqa: U100
|
||||||
|
return {
|
||||||
|
"__VIRTUAL_PROMPT__": "" if self.flag_prompt is None else self.flag_prompt,
|
||||||
|
diff --git a/src/virtualenv/activation/nushell/activate.nu b/src/virtualenv/activation/nushell/activate.nu
|
||||||
|
index 3da1519..569a8a1 100644
|
||||||
|
--- a/src/virtualenv/activation/nushell/activate.nu
|
||||||
|
+++ b/src/virtualenv/activation/nushell/activate.nu
|
||||||
|
@@ -31,8 +31,8 @@ export-env {
|
||||||
|
}
|
||||||
|
|
||||||
|
let is_windows = ($nu.os-info.name | str downcase) == 'windows'
|
||||||
|
- let virtual_env = '__VIRTUAL_ENV__'
|
||||||
|
- let bin = '__BIN_NAME__'
|
||||||
|
+ let virtual_env = __VIRTUAL_ENV__
|
||||||
|
+ let bin = __BIN_NAME__
|
||||||
|
let path_sep = (char esep)
|
||||||
|
let path_name = (if $is_windows {
|
||||||
|
if (has-env 'Path') {
|
||||||
|
@@ -73,10 +73,10 @@ export-env {
|
||||||
|
$new_env
|
||||||
|
} else {
|
||||||
|
# Creating the new prompt for the session
|
||||||
|
- let virtual_prompt = (if ('__VIRTUAL_PROMPT__' == '') {
|
||||||
|
+ let virtual_prompt = (if (__VIRTUAL_PROMPT__ == '') {
|
||||||
|
$'(char lparen)($virtual_env | path basename)(char rparen) '
|
||||||
|
} else {
|
||||||
|
- '(__VIRTUAL_PROMPT__) '
|
||||||
|
+ (__VIRTUAL_PROMPT__)
|
||||||
|
})
|
||||||
|
|
||||||
|
# Back up the old prompt builder
|
||||||
|
diff --git a/src/virtualenv/activation/powershell/__init__.py b/src/virtualenv/activation/powershell/__init__.py
|
||||||
|
index 4e74ecb..773d690 100644
|
||||||
|
--- a/src/virtualenv/activation/powershell/__init__.py
|
||||||
|
+++ b/src/virtualenv/activation/powershell/__init__.py
|
||||||
|
@@ -5,6 +5,18 @@ class PowerShellActivator(ViaTemplateActivator):
|
||||||
|
def templates(self):
|
||||||
|
yield "activate.ps1"
|
||||||
|
|
||||||
|
+ @staticmethod
|
||||||
|
+ def quote(string):
|
||||||
|
+ """
|
||||||
|
+ This should satisfy PowerShell quoting rules [1], unless the quoted
|
||||||
|
+ string is passed directly to Windows native commands [2].
|
||||||
|
+
|
||||||
|
+ [1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules
|
||||||
|
+ [2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing#passing-arguments-that-contain-quote-characters
|
||||||
|
+ """ # noqa: D205
|
||||||
|
+ string = string.replace("'", "''")
|
||||||
|
+ return f"'{string}'"
|
||||||
|
+
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"PowerShellActivator",
|
||||||
|
diff --git a/src/virtualenv/activation/powershell/activate.ps1 b/src/virtualenv/activation/powershell/activate.ps1
|
||||||
|
index d524347..346980d 100644
|
||||||
|
--- a/src/virtualenv/activation/powershell/activate.ps1
|
||||||
|
+++ b/src/virtualenv/activation/powershell/activate.ps1
|
||||||
|
@@ -35,18 +35,18 @@ $env:VIRTUAL_ENV = $VIRTUAL_ENV
|
||||||
|
|
||||||
|
New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH
|
||||||
|
|
||||||
|
-$env:PATH = "$env:VIRTUAL_ENV/__BIN_NAME____PATH_SEP__" + $env:PATH
|
||||||
|
+$env:PATH = "$env:VIRTUAL_ENV/" + __BIN_NAME__ + __PATH_SEP__ + $env:PATH
|
||||||
|
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||||
|
function global:_old_virtual_prompt {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
$function:_old_virtual_prompt = $function:prompt
|
||||||
|
|
||||||
|
- if ("__VIRTUAL_PROMPT__" -ne "") {
|
||||||
|
+ if (__VIRTUAL_PROMPT__ -ne "") {
|
||||||
|
function global:prompt {
|
||||||
|
# Add the custom prefix to the existing prompt
|
||||||
|
$previous_prompt_value = & $function:_old_virtual_prompt
|
||||||
|
- ("(__VIRTUAL_PROMPT__) " + $previous_prompt_value)
|
||||||
|
+ ((__VIRTUAL_PROMPT__) + $previous_prompt_value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
diff --git a/src/virtualenv/activation/python/__init__.py b/src/virtualenv/activation/python/__init__.py
|
||||||
|
index a49444b..8f0971e 100644
|
||||||
|
--- a/src/virtualenv/activation/python/__init__.py
|
||||||
|
+++ b/src/virtualenv/activation/python/__init__.py
|
||||||
|
@@ -9,10 +9,14 @@ class PythonActivator(ViaTemplateActivator):
|
||||||
|
def templates(self):
|
||||||
|
yield "activate_this.py"
|
||||||
|
|
||||||
|
+ @staticmethod
|
||||||
|
+ def quote(string):
|
||||||
|
+ return repr(string)
|
||||||
|
+
|
||||||
|
def replacements(self, creator, dest_folder):
|
||||||
|
replacements = super().replacements(creator, dest_folder)
|
||||||
|
lib_folders = OrderedDict((os.path.relpath(str(i), str(dest_folder)), None) for i in creator.libs)
|
||||||
|
- lib_folders = os.pathsep.join(lib_folders.keys()).replace("\\", "\\\\") # escape Windows path characters
|
||||||
|
+ lib_folders = os.pathsep.join(lib_folders.keys())
|
||||||
|
win_py2 = creator.interpreter.platform == "win32" and creator.interpreter.version_info.major == 2
|
||||||
|
replacements.update(
|
||||||
|
{
|
||||||
|
diff --git a/src/virtualenv/activation/python/activate_this.py b/src/virtualenv/activation/python/activate_this.py
|
||||||
|
index e8eeb84..976952e 100644
|
||||||
|
--- a/src/virtualenv/activation/python/activate_this.py
|
||||||
|
+++ b/src/virtualenv/activation/python/activate_this.py
|
||||||
|
@@ -14,7 +14,7 @@ except NameError:
|
||||||
|
raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")
|
||||||
|
|
||||||
|
bin_dir = os.path.dirname(abs_file)
|
||||||
|
-base = bin_dir[: -len("__BIN_NAME__") - 1] # strip away the bin part from the __file__, plus the path separator
|
||||||
|
+base = bin_dir[: -len(__BIN_NAME__) - 1] # strip away the bin part from the __file__, plus the path separator
|
||||||
|
|
||||||
|
# prepend bin to PATH (this file is inside the bin directory)
|
||||||
|
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
|
||||||
|
@@ -22,9 +22,9 @@ os.environ["VIRTUAL_ENV"] = base # virtual env is right above bin directory
|
||||||
|
|
||||||
|
# add the virtual environments libraries to the host python import mechanism
|
||||||
|
prev_length = len(sys.path)
|
||||||
|
-for lib in "__LIB_FOLDERS__".split(os.pathsep):
|
||||||
|
+for lib in __LIB_FOLDERS__.split(os.pathsep):
|
||||||
|
path = os.path.realpath(os.path.join(bin_dir, lib))
|
||||||
|
- site.addsitedir(path.decode("utf-8") if "__DECODE_PATH__" else path)
|
||||||
|
+ site.addsitedir(path.decode("utf-8") if __DECODE_PATH__ else path)
|
||||||
|
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]
|
||||||
|
|
||||||
|
sys.real_prefix = sys.prefix
|
||||||
|
diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py
|
||||||
|
index cc9dbda..b1dfa6b 100644
|
||||||
|
--- a/src/virtualenv/activation/via_template.py
|
||||||
|
+++ b/src/virtualenv/activation/via_template.py
|
||||||
|
@@ -1,4 +1,5 @@
|
||||||
|
import os
|
||||||
|
+import shlex
|
||||||
|
import sys
|
||||||
|
from abc import ABCMeta, abstractmethod
|
||||||
|
|
||||||
|
@@ -19,6 +20,16 @@ class ViaTemplateActivator(Activator, metaclass=ABCMeta):
|
||||||
|
def templates(self):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
+ @staticmethod
|
||||||
|
+ def quote(string):
|
||||||
|
+ """
|
||||||
|
+ Quote strings in the activation script.
|
||||||
|
+
|
||||||
|
+ :param string: the string to quote
|
||||||
|
+ :return: quoted string that works in the activation script
|
||||||
|
+ """
|
||||||
|
+ return shlex.quote(string)
|
||||||
|
+
|
||||||
|
def generate(self, creator):
|
||||||
|
dest_folder = creator.bin_dir
|
||||||
|
replacements = self.replacements(creator, dest_folder)
|
||||||
|
@@ -58,8 +69,8 @@ class ViaTemplateActivator(Activator, metaclass=ABCMeta):
|
||||||
|
binary = read_binary(self.__module__, template)
|
||||||
|
text = binary.decode("utf-8", errors="strict")
|
||||||
|
for key, value in replacements.items():
|
||||||
|
- value = self._repr_unicode(creator, value)
|
||||||
|
- text = text.replace(key, value)
|
||||||
|
+ value_uni = self._repr_unicode(creator, value)
|
||||||
|
+ text = text.replace(key, self.quote(value_uni))
|
||||||
|
return text
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
diff --git a/tests/conftest.py b/tests/conftest.py
|
||||||
|
index a7ec4e0..b2fae66 100644
|
||||||
|
--- a/tests/conftest.py
|
||||||
|
+++ b/tests/conftest.py
|
||||||
|
@@ -292,7 +292,11 @@ def is_inside_ci():
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def special_char_name():
|
||||||
|
- base = "e-$ èрт🚒♞中片-j"
|
||||||
|
+ base = "'\";&&e-$ èрт🚒♞中片-j"
|
||||||
|
+ if IS_WIN:
|
||||||
|
+ # get rid of invalid characters on Windows
|
||||||
|
+ base = base.replace('"', "")
|
||||||
|
+ base = base.replace(";", "")
|
||||||
|
# workaround for pypy3 https://bitbucket.org/pypy/pypy/issues/3147/venv-non-ascii-support-windows
|
||||||
|
encoding = "ascii" if IS_WIN else sys.getfilesystemencoding()
|
||||||
|
# let's not include characters that the file system cannot encode)
|
||||||
|
diff --git a/tests/unit/activation/conftest.py b/tests/unit/activation/conftest.py
|
||||||
|
index e24a4fc..2d8b3ce 100644
|
||||||
|
--- a/tests/unit/activation/conftest.py
|
||||||
|
+++ b/tests/unit/activation/conftest.py
|
||||||
|
@@ -4,7 +4,6 @@ import subprocess
|
||||||
|
import sys
|
||||||
|
from os.path import dirname, normcase
|
||||||
|
from pathlib import Path
|
||||||
|
-from shlex import quote
|
||||||
|
from subprocess import Popen
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
@@ -146,7 +145,7 @@ class ActivationTester:
|
||||||
|
assert out[-1] == "None", raw
|
||||||
|
|
||||||
|
def quote(self, s):
|
||||||
|
- return quote(s)
|
||||||
|
+ return self.of_class.quote(s)
|
||||||
|
|
||||||
|
def python_cmd(self, cmd):
|
||||||
|
return f"{os.path.basename(sys.executable)} -c {self.quote(cmd)}"
|
||||||
|
diff --git a/tests/unit/activation/test_batch.py b/tests/unit/activation/test_batch.py
|
||||||
|
index 9e6d6d6..e95f7ea 100644
|
||||||
|
--- a/tests/unit/activation/test_batch.py
|
||||||
|
+++ b/tests/unit/activation/test_batch.py
|
||||||
|
@@ -1,5 +1,3 @@
|
||||||
|
-from shlex import quote
|
||||||
|
-
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from virtualenv.activation import BatchActivator
|
||||||
|
@@ -25,10 +23,12 @@ def test_batch(activation_tester_class, activation_tester, tmp_path):
|
||||||
|
return ["@echo off", "", "chcp 65001 1>NUL"] + super()._get_test_lines(activate_script)
|
||||||
|
|
||||||
|
def quote(self, s):
|
||||||
|
- """double quotes needs to be single, and single need to be double"""
|
||||||
|
- return "".join(("'" if c == '"' else ('"' if c == "'" else c)) for c in quote(s))
|
||||||
|
+ if '"' in s or " " in s:
|
||||||
|
+ text = s.replace('"', r"\"")
|
||||||
|
+ return f'"{text}"'
|
||||||
|
+ return s
|
||||||
|
|
||||||
|
def print_prompt(self):
|
||||||
|
- return "echo %PROMPT%"
|
||||||
|
+ return 'echo "%PROMPT%"'
|
||||||
|
|
||||||
|
activation_tester(Batch)
|
||||||
|
diff --git a/tests/unit/activation/test_powershell.py b/tests/unit/activation/test_powershell.py
|
||||||
|
index 761237f..f495353 100644
|
||||||
|
--- a/tests/unit/activation/test_powershell.py
|
||||||
|
+++ b/tests/unit/activation/test_powershell.py
|
||||||
|
@@ -1,5 +1,4 @@
|
||||||
|
import sys
|
||||||
|
-from shlex import quote
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@@ -19,10 +18,6 @@ def test_powershell(activation_tester_class, activation_tester, monkeypatch):
|
||||||
|
self.activate_cmd = "."
|
||||||
|
self.script_encoding = "utf-16"
|
||||||
|
|
||||||
|
- def quote(self, s):
|
||||||
|
- """powershell double quote needed for quotes within single quotes"""
|
||||||
|
- return quote(s).replace('"', '""')
|
||||||
|
-
|
||||||
|
def _get_test_lines(self, activate_script):
|
||||||
|
# for BATCH utf-8 support need change the character code page to 650001
|
||||||
|
return super()._get_test_lines(activate_script)
|
||||||
|
@@ -33,4 +28,19 @@ def test_powershell(activation_tester_class, activation_tester, monkeypatch):
|
||||||
|
def print_prompt(self):
|
||||||
|
return "prompt"
|
||||||
|
|
||||||
|
+ def quote(self, s):
|
||||||
|
+ """
|
||||||
|
+ Tester will pass strings to native commands on Windows so extra
|
||||||
|
+ parsing rules are used. Check `PowerShellActivator.quote` for more
|
||||||
|
+ details.
|
||||||
|
+ """
|
||||||
|
+ text = PowerShellActivator.quote(s)
|
||||||
|
+ return text.replace('"', '""') if sys.platform == "win32" else text
|
||||||
|
+
|
||||||
|
+ def activate_call(self, script):
|
||||||
|
+ # Commands are called without quotes in PowerShell
|
||||||
|
+ cmd = self.activate_cmd
|
||||||
|
+ scr = self.quote(str(script))
|
||||||
|
+ return f"{cmd} {scr}".strip()
|
||||||
|
+
|
||||||
|
activation_tester(PowerShell)
|
||||||
|
--
|
||||||
|
2.46.2
|
||||||
|
|
@ -0,0 +1,464 @@
|
|||||||
|
From 71de653a714958d6649501b874009bafec74d3f4 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Philipp A <flying-sheep@web.de>
|
||||||
|
Date: Tue, 23 Apr 2024 18:46:41 +0200
|
||||||
|
Subject: [PATCH 1/2] Allow builtin interpreter discovery to find specific
|
||||||
|
Python versions given a general spec (#2709)
|
||||||
|
MIME-Version: 1.0
|
||||||
|
Content-Type: text/plain; charset=UTF-8
|
||||||
|
Content-Transfer-Encoding: 8bit
|
||||||
|
|
||||||
|
Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
|
||||||
|
|
||||||
|
Backported to 20.21.1 (without typing improvements).
|
||||||
|
---
|
||||||
|
docs/changelog/2709.bugfix.rst | 1 +
|
||||||
|
src/virtualenv/discovery/builtin.py | 78 +++++++++++++-------------
|
||||||
|
src/virtualenv/discovery/py_spec.py | 42 ++++++--------
|
||||||
|
tests/unit/discovery/test_discovery.py | 15 ++++-
|
||||||
|
4 files changed, 67 insertions(+), 69 deletions(-)
|
||||||
|
create mode 100644 docs/changelog/2709.bugfix.rst
|
||||||
|
|
||||||
|
diff --git a/docs/changelog/2709.bugfix.rst b/docs/changelog/2709.bugfix.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..904da84
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/docs/changelog/2709.bugfix.rst
|
||||||
|
@@ -0,0 +1 @@
|
||||||
|
+allow builtin discovery to discover specific interpreters (e.g. ``python3.12``) given an unspecific spec (e.g. ``python3``) - by :user:`flying-sheep`.
|
||||||
|
diff --git a/src/virtualenv/discovery/builtin.py b/src/virtualenv/discovery/builtin.py
|
||||||
|
index 40320d3..f79b5a5 100644
|
||||||
|
--- a/src/virtualenv/discovery/builtin.py
|
||||||
|
+++ b/src/virtualenv/discovery/builtin.py
|
||||||
|
@@ -1,6 +1,7 @@
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
+from pathlib import Path
|
||||||
|
|
||||||
|
from virtualenv.info import IS_WIN
|
||||||
|
|
||||||
|
@@ -67,7 +68,7 @@ def get_interpreter(key, try_first_with, app_data=None, env=None):
|
||||||
|
proposed_paths.add(key)
|
||||||
|
|
||||||
|
|
||||||
|
-def propose_interpreters(spec, try_first_with, app_data, env=None):
|
||||||
|
+def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa: C901, PLR0912
|
||||||
|
# 0. try with first
|
||||||
|
env = os.environ if env is None else env
|
||||||
|
for py_exe in try_first_with:
|
||||||
|
@@ -101,20 +102,17 @@ def propose_interpreters(spec, try_first_with, app_data, env=None):
|
||||||
|
for interpreter in propose_interpreters(spec, app_data, env):
|
||||||
|
yield interpreter, True
|
||||||
|
# finally just find on path, the path order matters (as the candidates are less easy to control by end user)
|
||||||
|
- paths = get_paths(env)
|
||||||
|
tested_exes = set()
|
||||||
|
- for pos, path in enumerate(paths):
|
||||||
|
- path_str = str(path)
|
||||||
|
- logging.debug(LazyPathDump(pos, path_str, env))
|
||||||
|
- for candidate, match in possible_specs(spec):
|
||||||
|
- found = check_path(candidate, path_str)
|
||||||
|
- if found is not None:
|
||||||
|
- exe = os.path.abspath(found)
|
||||||
|
- if exe not in tested_exes:
|
||||||
|
- tested_exes.add(exe)
|
||||||
|
- interpreter = PathPythonInfo.from_exe(exe, app_data, raise_on_error=False, env=env)
|
||||||
|
- if interpreter is not None:
|
||||||
|
- yield interpreter, match
|
||||||
|
+ find_candidates = path_exe_finder(spec)
|
||||||
|
+ for pos, path in enumerate(get_paths(env)):
|
||||||
|
+ logging.debug(LazyPathDump(pos, path, env))
|
||||||
|
+ for exe, impl_must_match in find_candidates(path):
|
||||||
|
+ if exe in tested_exes:
|
||||||
|
+ continue
|
||||||
|
+ tested_exes.add(exe)
|
||||||
|
+ interpreter = PathPythonInfo.from_exe(str(exe), app_data, raise_on_error=False, env=env)
|
||||||
|
+ if interpreter is not None:
|
||||||
|
+ yield interpreter, impl_must_match
|
||||||
|
|
||||||
|
|
||||||
|
def get_paths(env):
|
||||||
|
@@ -125,14 +123,14 @@ def get_paths(env):
|
||||||
|
except (AttributeError, ValueError):
|
||||||
|
path = os.defpath
|
||||||
|
if not path:
|
||||||
|
- paths = []
|
||||||
|
- else:
|
||||||
|
- paths = [p for p in path.split(os.pathsep) if os.path.exists(p)]
|
||||||
|
- return paths
|
||||||
|
+ return None
|
||||||
|
+ for p in map(Path, path.split(os.pathsep)):
|
||||||
|
+ if p.exists():
|
||||||
|
+ yield p
|
||||||
|
|
||||||
|
|
||||||
|
class LazyPathDump:
|
||||||
|
- def __init__(self, pos, path, env):
|
||||||
|
+ def __init__(self, pos, path, env) -> None:
|
||||||
|
self.pos = pos
|
||||||
|
self.path = path
|
||||||
|
self.env = env
|
||||||
|
@@ -141,35 +139,35 @@ class LazyPathDump:
|
||||||
|
content = f"discover PATH[{self.pos}]={self.path}"
|
||||||
|
if self.env.get("_VIRTUALENV_DEBUG"): # this is the over the board debug
|
||||||
|
content += " with =>"
|
||||||
|
- for file_name in os.listdir(self.path):
|
||||||
|
+ for file_path in self.path.iterdir():
|
||||||
|
try:
|
||||||
|
- file_path = os.path.join(self.path, file_name)
|
||||||
|
- if os.path.isdir(file_path) or not os.access(file_path, os.X_OK):
|
||||||
|
+ if file_path.is_dir() or not (file_path.stat().st_mode & os.X_OK):
|
||||||
|
continue
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
content += " "
|
||||||
|
- content += file_name
|
||||||
|
+ content += file_path.name
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
-def check_path(candidate, path):
|
||||||
|
- _, ext = os.path.splitext(candidate)
|
||||||
|
- if sys.platform == "win32" and ext != ".exe":
|
||||||
|
- candidate = candidate + ".exe"
|
||||||
|
- if os.path.isfile(candidate):
|
||||||
|
- return candidate
|
||||||
|
- candidate = os.path.join(path, candidate)
|
||||||
|
- if os.path.isfile(candidate):
|
||||||
|
- return candidate
|
||||||
|
- return None
|
||||||
|
-
|
||||||
|
-
|
||||||
|
-def possible_specs(spec):
|
||||||
|
- # 4. then maybe it's something exact on PATH - if it was direct lookup implementation no longer counts
|
||||||
|
- yield spec.str_spec, False
|
||||||
|
- # 5. or from the spec we can deduce a name on path that matches
|
||||||
|
- yield from spec.generate_names()
|
||||||
|
+def path_exe_finder(spec):
|
||||||
|
+ """Given a spec, return a function that can be called on a path to find all matching files in it."""
|
||||||
|
+ pat = spec.generate_re(windows=sys.platform == "win32")
|
||||||
|
+ direct = spec.str_spec
|
||||||
|
+ if sys.platform == "win32":
|
||||||
|
+ direct = f"{direct}.exe"
|
||||||
|
+
|
||||||
|
+ def path_exes(path):
|
||||||
|
+ # 4. then maybe it's something exact on PATH - if it was direct lookup implementation no longer counts
|
||||||
|
+ yield (path / direct), False
|
||||||
|
+ # 5. or from the spec we can deduce if a name on path matches
|
||||||
|
+ for exe in path.iterdir():
|
||||||
|
+ match = pat.fullmatch(exe.name)
|
||||||
|
+ if match:
|
||||||
|
+ # the implementation must match when we find “python[ver]”
|
||||||
|
+ yield exe.absolute(), match["impl"] == "python"
|
||||||
|
+
|
||||||
|
+ return path_exes
|
||||||
|
|
||||||
|
|
||||||
|
class PathPythonInfo(PythonInfo):
|
||||||
|
diff --git a/src/virtualenv/discovery/py_spec.py b/src/virtualenv/discovery/py_spec.py
|
||||||
|
index 103c7ae..cbfdfb8 100644
|
||||||
|
--- a/src/virtualenv/discovery/py_spec.py
|
||||||
|
+++ b/src/virtualenv/discovery/py_spec.py
|
||||||
|
@@ -1,10 +1,9 @@
|
||||||
|
"""A Python specification is an abstract requirement definition of an interpreter"""
|
||||||
|
|
||||||
|
+from __future__ import annotations
|
||||||
|
+
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
-from collections import OrderedDict
|
||||||
|
-
|
||||||
|
-from virtualenv.info import fs_is_case_sensitive
|
||||||
|
|
||||||
|
PATTERN = re.compile(r"^(?P<impl>[a-zA-Z]+)?(?P<version>[0-9.]+)?(?:-(?P<arch>32|64))?$")
|
||||||
|
|
||||||
|
@@ -12,7 +11,7 @@ PATTERN = re.compile(r"^(?P<impl>[a-zA-Z]+)?(?P<version>[0-9.]+)?(?:-(?P<arch>32
|
||||||
|
class PythonSpec:
|
||||||
|
"""Contains specification about a Python Interpreter"""
|
||||||
|
|
||||||
|
- def __init__(self, str_spec, implementation, major, minor, micro, architecture, path):
|
||||||
|
+ def __init__(self, str_spec, implementation, major, minor, micro, architecture, path) -> None: # noqa: PLR0913
|
||||||
|
self.str_spec = str_spec
|
||||||
|
self.implementation = implementation
|
||||||
|
self.major = major
|
||||||
|
@@ -22,7 +21,7 @@ class PythonSpec:
|
||||||
|
self.path = path
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
- def from_string_spec(cls, string_spec):
|
||||||
|
+ def from_string_spec(cls, string_spec): # noqa: C901, PLR0912
|
||||||
|
impl, major, minor, micro, arch, path = None, None, None, None, None, None
|
||||||
|
if os.path.isabs(string_spec):
|
||||||
|
path = string_spec
|
||||||
|
@@ -64,27 +63,18 @@ class PythonSpec:
|
||||||
|
|
||||||
|
return cls(string_spec, impl, major, minor, micro, arch, path)
|
||||||
|
|
||||||
|
- def generate_names(self):
|
||||||
|
- impls = OrderedDict()
|
||||||
|
- if self.implementation:
|
||||||
|
- # first consider implementation as it is
|
||||||
|
- impls[self.implementation] = False
|
||||||
|
- if fs_is_case_sensitive():
|
||||||
|
- # for case sensitive file systems consider lower and upper case versions too
|
||||||
|
- # trivia: MacBooks and all pre 2018 Windows-es were case insensitive by default
|
||||||
|
- impls[self.implementation.lower()] = False
|
||||||
|
- impls[self.implementation.upper()] = False
|
||||||
|
- impls["python"] = True # finally consider python as alias, implementation must match now
|
||||||
|
- version = self.major, self.minor, self.micro
|
||||||
|
- try:
|
||||||
|
- version = version[: version.index(None)]
|
||||||
|
- except ValueError:
|
||||||
|
- pass
|
||||||
|
- for impl, match in impls.items():
|
||||||
|
- for at in range(len(version), -1, -1):
|
||||||
|
- cur_ver = version[0:at]
|
||||||
|
- spec = f"{impl}{'.'.join(str(i) for i in cur_ver)}"
|
||||||
|
- yield spec, match
|
||||||
|
+ def generate_re(self, *, windows):
|
||||||
|
+ """Generate a regular expression for matching against a filename."""
|
||||||
|
+ version = r"{}(\.{}(\.{})?)?".format(
|
||||||
|
+ *(r"\d+" if v is None else v for v in (self.major, self.minor, self.micro))
|
||||||
|
+ )
|
||||||
|
+ impl = "python" if self.implementation is None else f"python|{re.escape(self.implementation)}"
|
||||||
|
+ suffix = r"\.exe" if windows else ""
|
||||||
|
+ # Try matching `direct` first, so the `direct` group is filled when possible.
|
||||||
|
+ return re.compile(
|
||||||
|
+ rf"(?P<impl>{impl})(?P<v>{version}){suffix}$",
|
||||||
|
+ flags=re.IGNORECASE,
|
||||||
|
+ )
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_abs(self):
|
||||||
|
diff --git a/tests/unit/discovery/test_discovery.py b/tests/unit/discovery/test_discovery.py
|
||||||
|
index c4a2cc7..51bae24 100644
|
||||||
|
--- a/tests/unit/discovery/test_discovery.py
|
||||||
|
+++ b/tests/unit/discovery/test_discovery.py
|
||||||
|
@@ -14,16 +14,25 @@ from virtualenv.info import fs_supports_symlink
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
|
||||||
|
@pytest.mark.parametrize("case", ["mixed", "lower", "upper"])
|
||||||
|
-def test_discovery_via_path(monkeypatch, case, tmp_path, caplog, session_app_data):
|
||||||
|
+@pytest.mark.parametrize("specificity", ["more", "less"])
|
||||||
|
+def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, session_app_data): # noqa: PLR0913
|
||||||
|
caplog.set_level(logging.DEBUG)
|
||||||
|
current = PythonInfo.current_system(session_app_data)
|
||||||
|
- core = f"somethingVeryCryptic{'.'.join(str(i) for i in current.version_info[0:3])}"
|
||||||
|
name = "somethingVeryCryptic"
|
||||||
|
if case == "lower":
|
||||||
|
name = name.lower()
|
||||||
|
elif case == "upper":
|
||||||
|
name = name.upper()
|
||||||
|
- exe_name = f"{name}{current.version_info.major}{'.exe' if sys.platform == 'win32' else ''}"
|
||||||
|
+ if specificity == "more":
|
||||||
|
+ # e.g. spec: python3, exe: /bin/python3.12
|
||||||
|
+ core_ver = current.version_info.major
|
||||||
|
+ exe_ver = ".".join(str(i) for i in current.version_info[0:2])
|
||||||
|
+ elif specificity == "less":
|
||||||
|
+ # e.g. spec: python3.12.1, exe: /bin/python3
|
||||||
|
+ core_ver = ".".join(str(i) for i in current.version_info[0:3])
|
||||||
|
+ exe_ver = current.version_info.major
|
||||||
|
+ core = f"somethingVeryCryptic{core_ver}"
|
||||||
|
+ exe_name = f"{name}{exe_ver}{'.exe' if sys.platform == 'win32' else ''}"
|
||||||
|
target = tmp_path / current.install_path("scripts")
|
||||||
|
target.mkdir(parents=True)
|
||||||
|
executable = target / exe_name
|
||||||
|
--
|
||||||
|
2.45.2
|
||||||
|
|
||||||
|
|
||||||
|
From 4bb73d175614aa6041b1e9e57d7302b9c253b5ef Mon Sep 17 00:00:00 2001
|
||||||
|
From: Ofek Lev <ofekmeister@gmail.com>
|
||||||
|
Date: Sat, 27 Apr 2024 14:43:00 -0400
|
||||||
|
Subject: [PATCH 2/2] Fix PATH-based Python discovery on Windows (#2712)
|
||||||
|
|
||||||
|
---
|
||||||
|
docs/changelog/2712.bugfix.rst | 1 +
|
||||||
|
src/virtualenv/discovery/builtin.py | 44 ++++++++++++++++++++------
|
||||||
|
src/virtualenv/discovery/py_spec.py | 10 +++++-
|
||||||
|
src/virtualenv/info.py | 5 +++
|
||||||
|
tests/unit/discovery/test_discovery.py | 8 +++--
|
||||||
|
5 files changed, 55 insertions(+), 13 deletions(-)
|
||||||
|
create mode 100644 docs/changelog/2712.bugfix.rst
|
||||||
|
|
||||||
|
diff --git a/docs/changelog/2712.bugfix.rst b/docs/changelog/2712.bugfix.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..fee5436
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/docs/changelog/2712.bugfix.rst
|
||||||
|
@@ -0,0 +1 @@
|
||||||
|
+fix PATH-based Python discovery on Windows - by :user:`ofek`.
|
||||||
|
diff --git a/src/virtualenv/discovery/builtin.py b/src/virtualenv/discovery/builtin.py
|
||||||
|
index f79b5a5..456c68b 100644
|
||||||
|
--- a/src/virtualenv/discovery/builtin.py
|
||||||
|
+++ b/src/virtualenv/discovery/builtin.py
|
||||||
|
@@ -3,7 +3,7 @@ import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
-from virtualenv.info import IS_WIN
|
||||||
|
+from virtualenv.info import IS_WIN, fs_path_id
|
||||||
|
|
||||||
|
from .discover import Discover
|
||||||
|
from .py_info import PythonInfo
|
||||||
|
@@ -68,9 +68,10 @@ def get_interpreter(key, try_first_with, app_data=None, env=None):
|
||||||
|
proposed_paths.add(key)
|
||||||
|
|
||||||
|
|
||||||
|
-def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa: C901, PLR0912
|
||||||
|
+def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa: C901, PLR0912, PLR0915
|
||||||
|
# 0. try with first
|
||||||
|
env = os.environ if env is None else env
|
||||||
|
+ tested_exes: set[str] = set()
|
||||||
|
for py_exe in try_first_with:
|
||||||
|
path = os.path.abspath(py_exe)
|
||||||
|
try:
|
||||||
|
@@ -78,7 +79,12 @@ def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
- yield PythonInfo.from_exe(os.path.abspath(path), app_data, env=env), True
|
||||||
|
+ exe_raw = os.path.abspath(path)
|
||||||
|
+ exe_id = fs_path_id(exe_raw)
|
||||||
|
+ if exe_id in tested_exes:
|
||||||
|
+ continue
|
||||||
|
+ tested_exes.add(exe_id)
|
||||||
|
+ yield PythonInfo.from_exe(exe_raw, app_data, env=env), True
|
||||||
|
|
||||||
|
# 1. if it's a path and exists
|
||||||
|
if spec.path is not None:
|
||||||
|
@@ -88,29 +94,44 @@ def propose_interpreters(spec, try_first_with, app_data=None, env=None): # noqa
|
||||||
|
if spec.is_abs:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
- yield PythonInfo.from_exe(os.path.abspath(spec.path), app_data, env=env), True
|
||||||
|
+ exe_raw = os.path.abspath(spec.path)
|
||||||
|
+ exe_id = fs_path_id(exe_raw)
|
||||||
|
+ if exe_id not in tested_exes:
|
||||||
|
+ tested_exes.add(exe_id)
|
||||||
|
+ yield PythonInfo.from_exe(exe_raw, app_data, env=env), True
|
||||||
|
if spec.is_abs:
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
# 2. otherwise try with the current
|
||||||
|
- yield PythonInfo.current_system(app_data), True
|
||||||
|
+ current_python = PythonInfo.current_system(app_data)
|
||||||
|
+ exe_raw = str(current_python.executable)
|
||||||
|
+ exe_id = fs_path_id(exe_raw)
|
||||||
|
+ if exe_id not in tested_exes:
|
||||||
|
+ tested_exes.add(exe_id)
|
||||||
|
+ yield current_python, True
|
||||||
|
|
||||||
|
# 3. otherwise fallback to platform default logic
|
||||||
|
if IS_WIN:
|
||||||
|
from .windows import propose_interpreters
|
||||||
|
|
||||||
|
for interpreter in propose_interpreters(spec, app_data, env):
|
||||||
|
+ exe_raw = str(interpreter.executable)
|
||||||
|
+ exe_id = fs_path_id(exe_raw)
|
||||||
|
+ if exe_id in tested_exes:
|
||||||
|
+ continue
|
||||||
|
+ tested_exes.add(exe_id)
|
||||||
|
yield interpreter, True
|
||||||
|
# finally just find on path, the path order matters (as the candidates are less easy to control by end user)
|
||||||
|
- tested_exes = set()
|
||||||
|
find_candidates = path_exe_finder(spec)
|
||||||
|
for pos, path in enumerate(get_paths(env)):
|
||||||
|
logging.debug(LazyPathDump(pos, path, env))
|
||||||
|
for exe, impl_must_match in find_candidates(path):
|
||||||
|
- if exe in tested_exes:
|
||||||
|
+ exe_raw = str(exe)
|
||||||
|
+ exe_id = fs_path_id(exe_raw)
|
||||||
|
+ if exe_id in tested_exes:
|
||||||
|
continue
|
||||||
|
- tested_exes.add(exe)
|
||||||
|
- interpreter = PathPythonInfo.from_exe(str(exe), app_data, raise_on_error=False, env=env)
|
||||||
|
+ tested_exes.add(exe_id)
|
||||||
|
+ interpreter = PathPythonInfo.from_exe(exe_raw, app_data, raise_on_error=False, env=env)
|
||||||
|
if interpreter is not None:
|
||||||
|
yield interpreter, impl_must_match
|
||||||
|
|
||||||
|
@@ -159,7 +180,10 @@ def path_exe_finder(spec):
|
||||||
|
|
||||||
|
def path_exes(path):
|
||||||
|
# 4. then maybe it's something exact on PATH - if it was direct lookup implementation no longer counts
|
||||||
|
- yield (path / direct), False
|
||||||
|
+ direct_path = path / direct
|
||||||
|
+ if direct_path.exists():
|
||||||
|
+ yield direct_path, False
|
||||||
|
+
|
||||||
|
# 5. or from the spec we can deduce if a name on path matches
|
||||||
|
for exe in path.iterdir():
|
||||||
|
match = pat.fullmatch(exe.name)
|
||||||
|
diff --git a/src/virtualenv/discovery/py_spec.py b/src/virtualenv/discovery/py_spec.py
|
||||||
|
index cbfdfb8..04b63b8 100644
|
||||||
|
--- a/src/virtualenv/discovery/py_spec.py
|
||||||
|
+++ b/src/virtualenv/discovery/py_spec.py
|
||||||
|
@@ -70,9 +70,17 @@ class PythonSpec:
|
||||||
|
)
|
||||||
|
impl = "python" if self.implementation is None else f"python|{re.escape(self.implementation)}"
|
||||||
|
suffix = r"\.exe" if windows else ""
|
||||||
|
+ version_conditional = (
|
||||||
|
+ "?"
|
||||||
|
+ # Windows Python executables are almost always unversioned
|
||||||
|
+ if windows
|
||||||
|
+ # Spec is an empty string
|
||||||
|
+ or self.major is None
|
||||||
|
+ else ""
|
||||||
|
+ )
|
||||||
|
# Try matching `direct` first, so the `direct` group is filled when possible.
|
||||||
|
return re.compile(
|
||||||
|
- rf"(?P<impl>{impl})(?P<v>{version}){suffix}$",
|
||||||
|
+ rf"(?P<impl>{impl})(?P<v>{version}){version_conditional}{suffix}$",
|
||||||
|
flags=re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
diff --git a/src/virtualenv/info.py b/src/virtualenv/info.py
|
||||||
|
index a4fc4bf..dd96f10 100644
|
||||||
|
--- a/src/virtualenv/info.py
|
||||||
|
+++ b/src/virtualenv/info.py
|
||||||
|
@@ -47,11 +47,16 @@ def fs_supports_symlink():
|
||||||
|
return _CAN_SYMLINK
|
||||||
|
|
||||||
|
|
||||||
|
+def fs_path_id(path: str) -> str:
|
||||||
|
+ return path.casefold() if fs_is_case_sensitive() else path
|
||||||
|
+
|
||||||
|
+
|
||||||
|
__all__ = (
|
||||||
|
"IS_PYPY",
|
||||||
|
"IS_CPYTHON",
|
||||||
|
"IS_WIN",
|
||||||
|
"fs_is_case_sensitive",
|
||||||
|
+ "fs_path_id",
|
||||||
|
"fs_supports_symlink",
|
||||||
|
"ROOT",
|
||||||
|
"IS_ZIPAPP",
|
||||||
|
diff --git a/tests/unit/discovery/test_discovery.py b/tests/unit/discovery/test_discovery.py
|
||||||
|
index 51bae24..6c64b40 100644
|
||||||
|
--- a/tests/unit/discovery/test_discovery.py
|
||||||
|
+++ b/tests/unit/discovery/test_discovery.py
|
||||||
|
@@ -14,7 +14,7 @@ from virtualenv.info import fs_supports_symlink
|
||||||
|
|
||||||
|
@pytest.mark.skipif(not fs_supports_symlink(), reason="symlink not supported")
|
||||||
|
@pytest.mark.parametrize("case", ["mixed", "lower", "upper"])
|
||||||
|
-@pytest.mark.parametrize("specificity", ["more", "less"])
|
||||||
|
+@pytest.mark.parametrize("specificity", ["more", "less", "none"])
|
||||||
|
def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, session_app_data): # noqa: PLR0913
|
||||||
|
caplog.set_level(logging.DEBUG)
|
||||||
|
current = PythonInfo.current_system(session_app_data)
|
||||||
|
@@ -31,7 +31,11 @@ def test_discovery_via_path(monkeypatch, case, specificity, tmp_path, caplog, se
|
||||||
|
# e.g. spec: python3.12.1, exe: /bin/python3
|
||||||
|
core_ver = ".".join(str(i) for i in current.version_info[0:3])
|
||||||
|
exe_ver = current.version_info.major
|
||||||
|
- core = f"somethingVeryCryptic{core_ver}"
|
||||||
|
+ elif specificity == "none":
|
||||||
|
+ # e.g. spec: python3.12.1, exe: /bin/python
|
||||||
|
+ core_ver = ".".join(str(i) for i in current.version_info[0:3])
|
||||||
|
+ exe_ver = ""
|
||||||
|
+ core = "" if specificity == "none" else f"{name}{core_ver}"
|
||||||
|
exe_name = f"{name}{exe_ver}{'.exe' if sys.platform == 'win32' else ''}"
|
||||||
|
target = tmp_path / current.install_path("scripts")
|
||||||
|
target.mkdir(parents=True)
|
||||||
|
--
|
||||||
|
2.45.2
|
||||||
|
|
@ -0,0 +1,60 @@
|
|||||||
|
From 11c30f6c69c4516b406c1c62f472d37898c58b93 Mon Sep 17 00:00:00 2001
|
||||||
|
From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= <miro@hroncok.cz>
|
||||||
|
Date: Mon, 4 Dec 2023 14:26:52 +0100
|
||||||
|
Subject: [PATCH] Run CI tests on Python 3.13, fix tests (#2673)
|
||||||
|
|
||||||
|
---
|
||||||
|
docs/changelog/2673.feature.rst | 1 +
|
||||||
|
tests/unit/create/test_creator.py | 6 +++++-
|
||||||
|
tests/unit/create/via_global_ref/builtin/testing/path.py | 6 +++++-
|
||||||
|
3 files changed, 11 insertions(+), 2 deletions(-)
|
||||||
|
create mode 100644 docs/changelog/2673.feature.rst
|
||||||
|
|
||||||
|
diff --git a/docs/changelog/2673.feature.rst b/docs/changelog/2673.feature.rst
|
||||||
|
new file mode 100644
|
||||||
|
index 0000000..0adf4a0
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/docs/changelog/2673.feature.rst
|
||||||
|
@@ -0,0 +1 @@
|
||||||
|
+The tests now pass on the CI with Python 3.13.0a2 - by :user:`hroncok`.
|
||||||
|
diff --git a/tests/unit/create/test_creator.py b/tests/unit/create/test_creator.py
|
||||||
|
index 8b9d688..fc21ad8 100644
|
||||||
|
--- a/tests/unit/create/test_creator.py
|
||||||
|
+++ b/tests/unit/create/test_creator.py
|
||||||
|
@@ -231,7 +231,11 @@ def test_create_no_seed(python, creator, isolated, system, coverage_env, special
|
||||||
|
assert os.path.exists(make_file)
|
||||||
|
|
||||||
|
git_ignore = (dest / ".gitignore").read_text(encoding="utf-8")
|
||||||
|
- assert git_ignore.splitlines() == ["# created by virtualenv automatically", "*"]
|
||||||
|
+ if creator_key == "venv" and sys.version_info >= (3, 13):
|
||||||
|
+ comment = "# Created by venv; see https://docs.python.org/3/library/venv.html"
|
||||||
|
+ else:
|
||||||
|
+ comment = "# created by virtualenv automatically"
|
||||||
|
+ assert git_ignore.splitlines() == [comment, "*"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_vcs_ignore_exists(tmp_path):
|
||||||
|
diff --git a/tests/unit/create/via_global_ref/builtin/testing/path.py b/tests/unit/create/via_global_ref/builtin/testing/path.py
|
||||||
|
index b2e1b85..d833de6 100644
|
||||||
|
--- a/tests/unit/create/via_global_ref/builtin/testing/path.py
|
||||||
|
+++ b/tests/unit/create/via_global_ref/builtin/testing/path.py
|
||||||
|
@@ -44,11 +44,15 @@ class PathMockABC(FakeDataABC, Path):
|
||||||
|
"""Mocks the behavior of `Path`"""
|
||||||
|
|
||||||
|
_flavour = getattr(Path(), "_flavour", None)
|
||||||
|
-
|
||||||
|
if hasattr(_flavour, "altsep"):
|
||||||
|
# Allows to pass some tests for Windows via PosixPath.
|
||||||
|
_flavour.altsep = _flavour.altsep or "\\"
|
||||||
|
|
||||||
|
+ # Python 3.13 renamed _flavour to parser
|
||||||
|
+ parser = getattr(Path(), "parser", None)
|
||||||
|
+ if hasattr(parser, "altsep"):
|
||||||
|
+ parser.altsep = parser.altsep or "\\"
|
||||||
|
+
|
||||||
|
def exists(self):
|
||||||
|
return self.is_file() or self.is_dir()
|
||||||
|
|
||||||
|
--
|
||||||
|
2.43.0
|
||||||
|
|
Loading…
Reference in new issue