Crate rustpython
source ·Expand description
This is the rustpython
binary. If you’re looking to embed RustPython into your application,
you’re likely looking for the rustpython-vm
crate.
You can install rustpython
with cargo install rustpython
, or if you’d like to inject your
own native modules you can make a binary crate that depends on the rustpython
crate (and
probably rustpython-vm
, too), and make a main.rs
that looks like:
use rustpython_vm::{pymodule, py_freeze};
fn main() {
rustpython::run(|vm| {
vm.add_native_module("mymod".to_owned(), Box::new(mymod::make_module));
vm.add_frozen(py_freeze!(source = "def foo(): pass", module_name = "otherthing"));
});
}
#[pymodule]
mod mymod {
use rustpython_vm::builtins::PyStrRef;
#[pyfunction]
fn do_thing(x: i32) -> i32 {
x + 1
}
#[pyfunction]
fn other_thing(s: PyStrRef) -> (String, usize) {
let new_string = format!("hello from rust, {}!", s);
let prev_len = s.as_str().len();
(new_string, prev_len)
}
}
The binary will have all the standard arguments of a python interpreter (including a REPL!) but it will have your modules loaded into the vm.
Re-exports§
pub use rustpython_vm as vm;
Structs§
- The convenient way to create rustpython_vm::Interpreter with stdlib and other stuffs.
Enums§
Functions§
- The main cli of the
rustpython
interpreter. This function will returnstd::process::ExitCode
based on the return code of the python code ran through the cli.