Always invoke found interpreter when uv run python is used (#6363)

Alternative to https://github.com/astral-sh/uv/pull/6362

Resolves the error mentioned in #6361
This commit is contained in:
Zanie Blue 2024-08-21 16:41:35 -05:00 committed by GitHub
parent fa0c20d5b1
commit 787f2a7bca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -659,7 +659,7 @@ pub(crate) async fn parse_script(
// Parse the input command.
let command = RunCommand::from(command);
let RunCommand::Python(target, _) = &command else {
let RunCommand::PythonScript(target, _) = &command else {
return Ok(None);
};
@ -716,8 +716,10 @@ fn can_skip_ephemeral(
#[derive(Debug)]
enum RunCommand {
/// Execute `python`.
Python(Vec<OsString>),
/// Execute a `python` script.
Python(PathBuf, Vec<OsString>),
PythonScript(PathBuf, Vec<OsString>),
/// Execute an external command.
External(OsString, Vec<OsString>),
/// Execute an empty command (in practice, `python` with no arguments).
@ -728,7 +730,8 @@ impl RunCommand {
/// Return the name of the target executable, for display purposes.
fn display_executable(&self) -> Cow<'_, str> {
match self {
Self::Python(_, _) | Self::Empty => Cow::Borrowed("python"),
Self::Python(_) => Cow::Borrowed("python"),
Self::PythonScript(_, _) | Self::Empty => Cow::Borrowed("python"),
Self::External(executable, _) => executable.to_string_lossy(),
}
}
@ -736,7 +739,12 @@ impl RunCommand {
/// Convert a [`RunCommand`] into a [`Command`].
fn as_command(&self, interpreter: &Interpreter) -> Command {
match self {
Self::Python(target, args) => {
Self::Python(args) => {
let mut process = Command::new(interpreter.sys_executable());
process.args(args);
process
}
Self::PythonScript(target, args) => {
let mut process = Command::new(interpreter.sys_executable());
process.arg(target);
process.args(args);
@ -755,7 +763,14 @@ impl RunCommand {
impl std::fmt::Display for RunCommand {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Python(target, args) => {
Self::Python(args) => {
write!(f, "python")?;
for arg in args {
write!(f, " {}", arg.to_string_lossy())?;
}
Ok(())
}
Self::PythonScript(target, args) => {
write!(f, "python {}", target.display())?;
for arg in args {
write!(f, " {}", arg.to_string_lossy())?;
@ -786,12 +801,14 @@ impl From<&ExternalCommand> for RunCommand {
};
let target_path = PathBuf::from(&target);
if target_path
if target.eq_ignore_ascii_case("python") {
Self::Python(args.to_vec())
} else if target_path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("py"))
&& target_path.exists()
{
Self::Python(target_path, args.to_vec())
Self::PythonScript(target_path, args.to_vec())
} else {
Self::External(
target.clone(),