Skip to content

Commit

Permalink
Allow alternative implementations to be selected if they use the defa…
Browse files Browse the repository at this point in the history
…ult executable name
  • Loading branch information
zanieb committed Sep 19, 2024
1 parent f692e7c commit 27c19f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
12 changes: 8 additions & 4 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,11 +943,14 @@ pub(crate) fn find_python_installation(

// If it's an alternative implementation, and alternative implementations aren't allowed
// skip it
let allows_alternative_implementation = request.allows_alternative_implementations()
|| installation.source.allows_alternative_implementations()
|| (installation.uses_default_executable_name()
&& installation.source == PythonSource::SearchPath);
if !matches!(
installation.implementation(),
LenientImplementationName::Known(ImplementationName::CPython),
) && !request.allows_alternative_implementations()
&& !installation.source.allows_alternative_implementations()
) && !allows_alternative_implementation
{
debug!("Skipping alternative implementation {}", installation.key());
continue;
Expand Down Expand Up @@ -1390,7 +1393,8 @@ impl PythonRequest {

pub(crate) fn allows_alternative_implementations(&self) -> bool {
match self {
Self::Any => false,
Self::Default => false,
Self::Any => true,
Self::Version(_) => false,
Self::Directory(_) | Self::File(_) | Self::ExecutableName(_) => true,
Self::Implementation(_) => true,
Expand Down Expand Up @@ -1593,7 +1597,7 @@ impl VersionRequest {
};

match self {
Self::Any | Self::Range(_) => {
Self::Default | Self::Any | Self::Range(_) => {
result.push(python3);
result.push(python);
}
Expand Down
17 changes: 17 additions & 0 deletions crates/uv-python/src/installation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::managed::{ManagedPythonInstallation, ManagedPythonInstallations};
use crate::platform::{Arch, Libc, Os};
use crate::{
downloads, Error, Interpreter, PythonDownloads, PythonPreference, PythonSource, PythonVersion,
VersionRequest,
};

/// A Python interpreter and accompanying tools.
Expand Down Expand Up @@ -199,6 +200,22 @@ impl PythonInstallation {
pub fn into_interpreter(self) -> Interpreter {
self.interpreter
}

/// Whether or not this Python installation uses a default Python name, like `python`,
/// `python3`, or `python.exe`.
pub(crate) fn uses_default_executable_name(&self) -> bool {
let Some(file_name) = self.interpreter.sys_executable().file_name() else {
return false;
};
let Some(name) = file_name.to_str() else {
return false;
};
VersionRequest::Any
.default_names()
.into_iter()
.flatten()
.any(|default_name| name == default_name)
}
}

#[derive(Error, Debug)]
Expand Down

0 comments on commit 27c19f2

Please sign in to comment.