|
| 1 | +//! This is the `rustpython` binary. If you're looking to embed RustPython into your application, |
| 2 | +//! you're likely looking for the [`rustpython-vm`](https://docs.rs/rustpython-vm) crate. |
| 3 | +//! |
| 4 | +//! You can install `rustpython` with `cargo install rustpython`, or if you'd like to inject your |
| 5 | +//! own native modules you can make a binary crate that depends on the `rustpython` crate (and |
| 6 | +//! probably `rustpython-vm`, too), and make a `main.rs` that looks like: |
| 7 | +//! |
| 8 | +//! ```no_run |
| 9 | +//! use rustpython_vm::{pymodule, py_freeze}; |
| 10 | +//! fn main() { |
| 11 | +//! rustpython::run(|vm| { |
| 12 | +//! vm.add_native_module("mymod".to_owned(), Box::new(mymod::make_module)); |
| 13 | +//! vm.add_frozen(py_freeze!(source = "def foo(): pass", module_name = "otherthing")); |
| 14 | +//! }); |
| 15 | +//! } |
| 16 | +//! |
| 17 | +//! #[pymodule] |
| 18 | +//! mod mymod { |
| 19 | +//! use rustpython_vm::builtins::PyStrRef; |
| 20 | +//TODO: use rustpython_vm::prelude::*; |
| 21 | +//! use rustpython_vm::common::borrow::BorrowValue; |
| 22 | +//! |
| 23 | +//! #[pyfunction] |
| 24 | +//! fn do_thing(x: i32) -> i32 { |
| 25 | +//! x + 1 |
| 26 | +//! } |
| 27 | +//! |
| 28 | +//! #[pyfunction] |
| 29 | +//! fn other_thing(s: PyStrRef) -> (String, usize) { |
| 30 | +//! let new_string = format!("hello from rust, {}!", s); |
| 31 | +//! let prev_len = s.borrow_value().len(); |
| 32 | +//! (new_string, prev_len) |
| 33 | +//! } |
| 34 | +//! } |
| 35 | +//! ``` |
| 36 | +//! |
| 37 | +//! The binary will have all the standard arguments of a python interpreter (including a REPL!) but |
| 38 | +//! it will have your modules loaded into the vm. |
| 39 | +
|
1 | 40 | #[macro_use] |
2 | 41 | extern crate clap; |
3 | 42 | extern crate env_logger; |
@@ -25,6 +64,8 @@ mod shell; |
25 | 64 |
|
26 | 65 | pub use rustpython_vm; |
27 | 66 |
|
| 67 | +/// The main cli of the `rustpython` interpreter. This function will exit with `process::exit()` |
| 68 | +/// based on the return code of the python code ran through the cli. |
28 | 69 | pub fn run<F>(init: F) -> ! |
29 | 70 | where |
30 | 71 | F: FnOnce(&mut VirtualMachine), |
|
0 commit comments