| 1 |
use pyo3::prelude::*; |
| 2 |
use std::env; |
| 3 |
|
| 4 |
#[pyfunction] |
| 5 |
fn get_21() -> usize { |
| 6 |
21 |
| 7 |
} |
| 8 |
|
| 9 |
/// Prints the CLI arguments, once from Rust's point of view and once from Python's point of view. |
| 10 |
#[pyfunction] |
| 11 |
fn print_cli_args(py: Python) -> PyResult<()> { |
| 12 |
// This one includes Python and the name of the wrapper script itself, e.g. |
| 13 |
// `["/home/ferris/.venv/bin/python", "/home/ferris/.venv/bin/print_cli_args", "a", "b", "c"]` |
| 14 |
println!("{:?}", env::args().collect::<Vec<_>>()); |
| 15 |
// This one includes only the name of the wrapper script itself, e.g. |
| 16 |
// `["/home/ferris/.venv/bin/print_cli_args", "a", "b", "c"])` |
| 17 |
println!( |
| 18 |
"{:?}", |
| 19 |
py.import("sys")? |
| 20 |
.getattr("argv")? |
| 21 |
.extract::<Vec<String>>()? |
| 22 |
); |
| 23 |
Ok(()) |
| 24 |
} |
| 25 |
|
| 26 |
#[pymodule] |
| 27 |
fn pyo3_mixed(m: &Bound<'_, PyModule>) -> PyResult<()> { |
| 28 |
m.add_wrapped(wrap_pyfunction!(get_21))?; |
| 29 |
m.add_wrapped(wrap_pyfunction!(print_cli_args))?; |
| 30 |
|
| 31 |
Ok(()) |
| 32 |
} |