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

Add example projects #5384

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add example projects
  • Loading branch information
youknowone committed Aug 6, 2024
commit e4cdd9a33cc262e390fd609d72c4b5be9e48db40
6 changes: 6 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 148,12 @@ jobs:
- name: check compilation without threading
run: cargo check ${{ env.CARGO_ARGS }}

- name: Test example projects
run:
cargo run --manifest-path example_projects/barebone/Cargo.toml
cargo run --manifest-path example_projects/frozen_stdlib/Cargo.toml
if: runner.os == 'Linux'

- name: prepare AppleSilicon build
uses: dtolnay/rust-toolchain@stable
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 176,4 @@ perf = "warn"
style = "warn"
complexity = "warn"
suspicious = "warn"
correctness = "warn"
correctness = "warn"
2 changes: 2 additions & 0 deletions example_projects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 1,2 @@
*/target
*/Cargo.lock
10 changes: 10 additions & 0 deletions example_projects/aheui-rust.md
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
# aheui-rust

- Crate link: https://github.com/youknowone/aheui-rust/tree/main/rpaheui
- Creating a frozenlib: https://github.com/youknowone/aheui-rust/blob/main/rpaheui/src/lib.rs

This crate shows you how to embed an entire Python project into bytecode and ship it. Follow the `FROZEN` constant and see how it's made and how you can use it.

If you'd like to learn more about how to initialize the Standard Library with the `freeze-stdlib` feature, check out the example project at `example_projects/frozen_stdlib/src/main.rs`.

Just a heads-up, it doesn't automatically resolve dependencies. If you have more dependencies than the standard library, Don't forget to also freeze them.
9 changes: 9 additions & 0 deletions example_projects/barebone/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
[package]
name = "example-barebone"
version = "0.1.0"
edition = "2021"

[dependencies]
rustpython-vm = { path = "../../vm", default-features = false }

[workspace]
17 changes: 17 additions & 0 deletions example_projects/barebone/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
use rustpython_vm::Interpreter;

pub fn main() {
let interp = Interpreter::without_stdlib(Default::default());
let value = interp.enter(|vm| {
let max = vm.builtins.get_attr("max", vm)?;
let value = max.call((vm.ctx.new_int(5), vm.ctx.new_int(10)), vm)?;
vm.print((vm.ctx.new_str("python print"), value.clone()))?;
Ok(value)
});
match value {
Ok(value) => println!("Rust repr: {:?}", value),
Err(err) => {
interp.finalize(err);
}
}
}
11 changes: 11 additions & 0 deletions example_projects/frozen_stdlib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 1,11 @@
[package]
name = "example_frozen_stdlib"
version = "0.1.0"
edition = "2021"

[dependencies]
rustpython = { path = "../../", default-features = false, features = ["freeze-stdlib"] }
rustpython-vm = { path = "../../vm", default-features = false, features = ["freeze-stdlib"] }
rustpython-pylib = { path = "../../pylib", default-features = false, features = ["freeze-stdlib"] }

[workspace]
39 changes: 39 additions & 0 deletions example_projects/frozen_stdlib/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,39 @@
/// Setting up a project with a frozen stdlib can be done *either* by using `rustpython::InterpreterConfig` or `rustpython_vm::Interpreter::with_init`.
/// See each function for example.
///
/// See also: `aheui-rust.md` for freezing your own package.

use rustpython_vm::{PyResult, VirtualMachine};

fn run(keyword: &str, vm: &VirtualMachine) -> PyResult<()> {
let json = vm.import("json", 0)?;
let json_loads = json.get_attr("loads", vm)?;
let template = r#"{"key": "value"}"#;
let json_string = template.replace("value", keyword);
let dict = json_loads.call((vm.ctx.new_str(json_string),), vm)?;
vm.print((dict,))?;
Ok(())
}

fn interpreter_with_config() {
let interpreter = rustpython::InterpreterConfig::new()
.init_stdlib()
.interpreter();
// Use interpreter.enter to reuse the same interpreter later
interpreter.run(|vm| run("rustpython::InterpreterConfig", vm));
}

fn interpreter_with_vm() {
let interpreter = rustpython_vm::Interpreter::with_init(Default::default(), |vm| {
// This is unintuitive, but the stdlib is out of the vm crate.
// Any suggestion to improve this is welcome.
vm.add_frozen(rustpython_pylib::FROZEN_STDLIB);
});
// Use interpreter.enter to reuse the same interpreter later
interpreter.run(|vm| run("rustpython_vm::Interpreter::with_init", vm));
}

fn main() {
interpreter_with_config();
interpreter_with_vm();
}