GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Docs.rs
  • rustpython-0.6.0
  • Permalink
  • Docs.rs crate page
  • MIT
  • Repository
  • crates.io
  • Source
  • windelbouwman
  • github:rustpython:packaging
  • dirs ^6 normal
  • env_logger ^0.11 normal
  • flame ^0.2.2 normal optional
  • flamescope ^0.1.2 normal optional

This view

  • Original page
  • New address

GeezerVison is showing docs.rs — rustpython - Rust

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. 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::{InterpreterBuilder, InterpreterBuilderExt};
use rustpython_vm::{pymodule, py_freeze};

fn main() -> std::process::ExitCode {
    let builder = InterpreterBuilder::new().init_stdlib();
    // Add a native module using builder.ctx
    let my_mod_def = my_mod::module_def(&builder.ctx);
    let builder = builder
        .add_native_module(my_mod_def)
        // Add a frozen module
        .add_frozen_modules(py_freeze!(source = "def foo(): pass", module_name = "other_thing"));

    rustpython::run(builder)
}

#[pymodule]
mod my_mod {
    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.byte_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.

See rustpython_derive crate for documentation on macros used in the example above.

Re-exports§

pub use rustpython_pylib as pylib;
pub use self as vm;

Structs§

Interpreter
One isolated Python interpreter in the process (≈ CPython PyInterpreterState + main tstate).
InterpreterBuilder
Configuration builder for constructing an Interpreter.

Enums§

InstallPipMode
RunMode

Traits§

InterpreterBuilderExt
Extension trait for InterpreterBuilder to add rustpython-specific functionality.

Functions§

parse_opts
Create settings by examining command line arguments and environment variables.
run
The main cli of the rustpython interpreter. This function will return std::process::ExitCode based on the return code of the python code ran through the cli.
run_shell
Enter a repl loop
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.