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

Adding support for .pyc files in uv run #6886

Merged
merged 3 commits into from
Aug 30, 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
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ impl TryFrom<&ExternalCommand> for RunCommand {
Ok(Self::Python(args.to_vec()))
} else if target_path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("py"))
.is_some_and(|ext| ext.eq_ignore_ascii_case("py") || ext.eq_ignore_ascii_case("pyc"))
&& target_path.exists()
{
Ok(Self::PythonScript(target_path, args.to_vec()))
Expand Down
102 changes: 102 additions & 0 deletions crates/uv/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,3 +1671,105 @@ fn run_isolated_incompatible_python() -> Result<()> {

Ok(())
}

#[test]
fn run_compiled_python_file() -> Result<()> {
let context = TestContext::new("3.12");

// Write a non-PEP 723 script.
let test_non_script = context.temp_dir.child("main.py");
test_non_script.write_str(indoc! { r#"
print("Hello, world!")
"#
})?;

// Run a non-PEP 723 script.
uv_snapshot!(context.filters(), context.run().arg("main.py"), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!

----- stderr -----
"###);

let compile_output = context
.run()
.arg("python")
.arg("-m")
.arg("compileall")
.arg(test_non_script.path())
.output()?;

assert!(
compile_output.status.success(),
"Failed to compile the python script"
);

// Run the compiled non-PEP 723 script.
let compiled_non_script = context.temp_dir.child("__pycache__/main.cpython-312.pyc");
uv_snapshot!(context.filters(), context.run().arg(compiled_non_script.path()), @r###"
success: true
exit_code: 0
----- stdout -----
Hello, world!

----- stderr -----
"###);

// If the script contains a PEP 723 tag, we should install its requirements.
let test_script = context.temp_dir.child("script.py");
test_script.write_str(indoc! { r#"
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "iniconfig",
# ]
# ///
import iniconfig
"#
})?;

uv_snapshot!(context.filters(), context.run().arg("script.py"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Reading inline script metadata from: script.py
Resolved 1 package in [TIME]
Prepared 1 package in [TIME]
Installed 1 package in [TIME]
+ iniconfig==2.0.0
"###);

// Compile the PEP 723 script.
let compile_output = context
.run()
.arg("python")
.arg("-m")
.arg("compileall")
.arg(test_script.path())
.output()?;

assert!(
compile_output.status.success(),
"Failed to compile the python script"
);

// Run the compiled PEP 723 script. This fails, since we can't read the script tag.
let compiled_script = context.temp_dir.child("__pycache__/script.cpython-312.pyc");
uv_snapshot!(context.filters(), context.run().arg(compiled_script.path()), @r###"
success: false
exit_code: 1
----- stdout -----

----- stderr -----
Traceback (most recent call last):
File "[TEMP_DIR]/script.py", line 7, in <module>
import iniconfig
ModuleNotFoundError: No module named 'iniconfig'
"###);

Ok(())
}
Loading