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 23, 2024
1 parent 8eebac0 commit 3c41d2b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,11 +946,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 @@ -1217,10 +1220,7 @@ impl PythonRequest {
for implementation in
ImplementationName::long_names().chain(ImplementationName::short_names())
{
if let Some(remainder) = value
.to_ascii_lowercase()
.strip_prefix(Into::<&str>::into(*implementation))
{
if let Some(remainder) = value.to_ascii_lowercase().strip_prefix(implementation) {
// e.g. `pypy`
if remainder.is_empty() {
return Self::Implementation(
Expand Down Expand Up @@ -1395,7 +1395,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
16 changes: 16 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,21 @@ 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::Default
.executable_names(None)
.into_iter()
.any(|default_name| name == default_name.to_string())
}
}

#[derive(Error, Debug)]
Expand Down

0 comments on commit 3c41d2b

Please sign in to comment.