Allow uv run arguments when reading from stdin (#10034)

## Summary

Closes https://github.com/astral-sh/uv/issues/10033.
This commit is contained in:
Charlie Marsh
2024-12-19 12:39:56 -05:00
committed by GitHub
parent bd03243dd5
commit 5c6e584987
3 changed files with 20 additions and 5 deletions
+5 -4
View File
@@ -1181,7 +1181,7 @@ pub(crate) enum RunCommand {
/// [zipapp]: <https://docs.python.org/3/library/zipapp.html>
PythonZipapp(PathBuf, Vec<OsString>),
/// Execute a `python` script provided via `stdin`.
PythonStdin(Vec<u8>),
PythonStdin(Vec<u8>, Vec<OsString>),
/// Execute a Python script provided via a remote URL.
PythonRemote(tempfile::NamedTempFile, Vec<OsString>),
/// Execute an external command.
@@ -1208,7 +1208,7 @@ impl RunCommand {
Cow::Borrowed("python")
}
}
Self::PythonStdin(_) => Cow::Borrowed("python -c"),
Self::PythonStdin(..) => Cow::Borrowed("python -c"),
Self::External(executable, _) => executable.to_string_lossy(),
}
}
@@ -1261,7 +1261,7 @@ impl RunCommand {
process.args(args);
process
}
Self::PythonStdin(script) => {
Self::PythonStdin(script, args) => {
let mut process = Command::new(interpreter.sys_executable());
process.arg("-c");
@@ -1276,6 +1276,7 @@ impl RunCommand {
let script = String::from_utf8(script.clone()).expect("script is valid UTF-8");
process.arg(script);
}
process.args(args);
process
}
@@ -1414,7 +1415,7 @@ impl RunCommand {
if target.eq_ignore_ascii_case("-") {
let mut buf = Vec::with_capacity(1024);
std::io::stdin().read_to_end(&mut buf)?;
Ok(Self::PythonStdin(buf))
Ok(Self::PythonStdin(buf, args.to_vec()))
} else if target.eq_ignore_ascii_case("python") {
Ok(Self::Python(args.to_vec()))
} else if target_path
+1 -1
View File
@@ -175,7 +175,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
Some(RunCommand::PythonRemote(script, _)) => {
Pep723Metadata::read(&script).await?.map(Pep723Item::Remote)
}
Some(RunCommand::PythonStdin(contents)) => {
Some(RunCommand::PythonStdin(contents, _)) => {
Pep723Metadata::parse(contents)?.map(Pep723Item::Stdin)
}
_ => None,
+14
View File
@@ -2493,6 +2493,20 @@ fn run_zipapp() -> Result<()> {
Ok(())
}
#[test]
fn run_stdin_args() {
let context = TestContext::new("3.12");
uv_snapshot!(context.filters(), context.run().arg("python").arg("-c").arg("import sys; print(sys.argv)").arg("foo").arg("bar"), @r###"
success: true
exit_code: 0
----- stdout -----
['-c', 'foo', 'bar']
----- stderr -----
"###);
}
/// Run a module equivalent to `python -m foo`.
#[test]
fn run_module() {