Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip Python 2 versions when locating Python #3476

Merged
merged 1 commit into from
May 9, 2024
Merged
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
87 changes: 68 additions & 19 deletions crates/uv-interpreter/src/find_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,77 @@ fn find_python(

let interpreter = match Interpreter::query(&path, cache) {
Ok(interpreter) => interpreter,
Err(
err @ Error::QueryScript {
err: InterpreterInfoError::UnsupportedPythonVersion { .. },
..
},
) => {
if selector.major() <= Some(2) {
return Err(err);

// If the Python version is < 3.4, the `-I` flag is not supported, so
// we can't run the script at all, and need to sniff it from the output.
Err(Error::PythonSubcommandOutput { stderr, .. })
if stderr.contains("Unknown option: -I")
&& stderr.contains("usage: python [option]") =>
{
// If the user _requested_ a version prior to 3.4, raise an error, as
// 3.4 is the minimum supported version for invoking the interpreter
// query script at all.
match selector {
PythonVersionSelector::Major(major) if major < 3 => {
return Err(Error::UnsupportedPython(major.to_string()));
}
PythonVersionSelector::MajorMinor(major, minor)
if (major, minor) < (3, 4) =>
{
return Err(Error::UnsupportedPython(format!(
"{major}.{minor}"
)));
}
PythonVersionSelector::MajorMinorPatch(major, minor, patch)
if (major, minor) < (3, 4) =>
{
return Err(Error::UnsupportedPython(format!(
"{major}.{minor}.{patch}"
)));
}
_ => {}
}

debug!(
"Found a Python installation that isn't supported by uv, skipping."
);
continue;
}

Err(Error::QueryScript {
err: InterpreterInfoError::UnsupportedPythonVersion { .. },
..
}) => {
// If the user _requested_ a version prior to 3.7, raise an error, as
// 3.7 is the minimum supported version for running the interpreter
// query script.
match selector {
PythonVersionSelector::Major(major) if major < 3 => {
return Err(Error::UnsupportedPython(major.to_string()));
}
PythonVersionSelector::MajorMinor(major, minor)
if (major, minor) < (3, 7) =>
{
return Err(Error::UnsupportedPython(format!(
"{major}.{minor}"
)));
}
PythonVersionSelector::MajorMinorPatch(major, minor, patch)
if (major, minor) < (3, 7) =>
{
return Err(Error::UnsupportedPython(format!(
"{major}.{minor}.{patch}"
)));
}
_ => {}
}
// Skip over Python 2 or older installation when querying for a recent python installation.
debug!("Found a Python 2 installation that isn't supported by uv, skipping.");

debug!(
"Found a Python installation that isn't supported by uv, skipping."
);
continue;
}

Err(error) => return Err(error),
};

Expand Down Expand Up @@ -400,15 +458,6 @@ impl PythonVersionSelector {
],
}
}

fn major(self) -> Option<u8> {
match self {
Self::Default => None,
Self::Major(major) => Some(major),
Self::MajorMinor(major, _) => Some(major),
Self::MajorMinorPatch(major, _, _) => Some(major),
}
}
}

fn warn_on_unsupported_python(interpreter: &Interpreter) {
Expand Down
2 changes: 2 additions & 0 deletions crates/uv-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub enum Error {
stdout: String,
stderr: String,
},
#[error("Requested Python version ({0}) is unsupported")]
UnsupportedPython(String),
#[error("Failed to write to cache")]
Encode(#[from] rmp_serde::encode::Error),
#[error("Broken virtualenv: Failed to parse pyvenv.cfg")]
Expand Down
Loading