Skip to content
wizrdsPublic

About

A Rust library for executing guest Python code transparently.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

GuestPy

GuestPy lets a Rust application load and run Python code inside an interpreter, call the functions and read the values that code defines, and expose Rust functions, classes, and modules for that Python code to call back into. GuestPy has no interpreter of its own and instead runs on a pluggable backend, so the same host code works unchanged against either of two interchangeable backends: real CPython or an all-Rust interpreter, requiring no separate Python installation.

The library provides:

  • isolated guests sharing one runtime;
  • direct Rust-to-Python and Python-to-Rust calls;
  • typed guest-module facades;
  • Bundle source packages;
  • serde-backed conversion for ordinary Rust data;
  • macros for defining Rust host classes and host modules;
  • awaitable guest coroutines;
  • timeouts and cancellation controls; and
  • runtime-global and guest-local capability binding.

GuestPy does not manage Python package installation or provide filesystem source loading. Applications remain responsible for choosing guest source and deciding which capabilities each guest receives.

Installation

Add GuestPy to your project and enable one backend feature: pyo3 for real CPython, or rustpython for RustPython. Either feature alone is enough; GuestPy re-exports the chosen backend's concrete type through its own crate, so ordinary embedding needs no second, backend-specific dependency:

[dependencies]
guestpy = { git = "https://github.com/wizrds/guestpy", features = ["pyo3"] }

To use RustPython instead:

[dependencies]
guestpy = { git = "https://github.com/wizrds/guestpy", features = ["rustpython"] }

GuestPy has no default features. Enable only the optional behavior the application needs:

Feature Behavior
pyo3 Compiles in the guestpy-pyo3 backend crate (real CPython, via the pyo3 crate) and re-exports it as guestpy::pyo3.
rustpython Compiles in the guestpy-rustpython backend crate and re-exports it as guestpy::rustpython.
embedded Enables embedded guest bundles.
serde Enables serde-backed Rust data conversion.
tokio Enables Tokio cancellation support and filesystem guest bundles.
bytes Enables Bytes conversion for guest data.

Quick start

Build a runtime, build a guest, load Python source, and call its exported function:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

fn main() -> Result<(), Error> {
    let guest = Runtime::<CPython>::builder()
        .build()?
        .guest()
        .build()?;

    guest.exec(
        r#"
def add(left, right):
    return left + right
"#,
    )?;

    let sum = guest
        .globals()?
        .item::<Function<_>, _>("add")?
        .call::<_, i64>((20, 22))?;

    assert_eq!(sum, 42);

    Ok(())
}

Runtime::<CPython>::builder() selects CPython. Replace CPython with guestpy::rustpython::RustPython to use RustPython. The calls in this example are synchronous; GuestPy converts the arguments and result at the boundary.

Runtimes, guests, and isolation

A Runtime<B> owns the selected backend. Each call to Runtime::guest builds a fresh, isolated guest:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

let runtime = Runtime::<CPython>::builder()
    .build()?;
let first = runtime.guest().build()?;
let second = runtime.guest().build()?;

first.exec("name = 'first'")?;
second.exec("name = 'second'")?;

assert_eq!(first.globals()?.item::<String, _>("name")?, "first");
assert_eq!(second.globals()?.item::<String, _>("name")?, "second");

Bind host libraries, native libraries, and source bundles to a runtime when every guest should receive them. Bind them to a guest builder when a capability belongs to one guest only. A runtime or guest builder can also deny imports by name.

Execution control

Configure a timeout and a cancellation signal on the runtime when an application needs to stop guest execution:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

let cancellation = Cancellation::new();
let runtime = Runtime::<CPython>::builder()
    .timeout(std::time::Duration::from_millis(50))
    .cancellation(cancellation.clone())
    .build()?;
let guest = runtime.guest().build()?;

assert!(matches!(
    guest.exec("while True: pass"),
    Err(Error::Timeout),
));

cancellation.cancel();

assert!(matches!(
    guest.eval::<i64>("1 + 1"),
    Err(Error::Cancelled),
));

Use the selected backend's configuration type with RuntimeBuilder::config when interpreter-specific configuration is required.

Loading guest code

Guest::exec runs Python statements directly, with no return value. Guest::eval evaluates a single Python expression and converts its result:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

let guest = Runtime::<CPython>::builder()
    .build()?
    .guest()
    .build()?;

assert_eq!(guest.eval::<i64>("6 * 7")?, 42);

Use Guest::guest_module(name, source) to load a named Python module and work with its exports:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

let guest = Runtime::<CPython>::builder()
    .build()?
    .guest()
    .build()?;

let module = guest.guest_module(
    "dynamic",
    r#"
settings = {'prefix': 'hello'}

def greet(name):
    return f"{settings['prefix']} {name}"
"#,
)?;

assert_eq!(
    module.object("settings")?.get::<String>("prefix")?,
    "hello",
);
assert_eq!(
    module.function("greet")?.call::<_, String>(("Ada",))?,
    "hello Ada",
);

This dynamic handle API is useful when the module shape is not known until runtime.

Loading several modules and packages together with Bundle

Use Bundle when guest code is a package or spans several modules:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

let bundle = Bundle::builder()
    .module(
        "app.main",
        r#"
from app.util import double

def run():
    return double(21)
"#,
    )
    .package("app", "")
    .module("app.util", "def double(value):\n    return value * 2\n")
    .build()?;

let guest = Runtime::<CPython>::builder()
    .build()?
    .guest()
    .build()?;

let module = guest.load(&bundle)?;

assert_eq!(module.function("run")?.call::<_, i64>(())?, 42);

Build a bundle from modules, packages, data, or a complete installed Python directory, then load it directly or bind it so guest code can import it. Pure Python files remain portable across backends. Compiled native modules must match the selected backend, interpreter ABI, operating system, and architecture. CPython native modules are process-global, so separate guests cannot load different binaries under the same dotted module name. RustPython reports an unsupported error only when guest code imports a bundled native module.

The embedded and tokio features provide embedded and filesystem bundle construction when an application needs them.

Typed guest facades

When a guest module has a known interface, guestpy::guest_module! generates a typed Rust facade:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

guestpy::guest_module! {
    pub module Math {
        fn add(left: i64, right: i64) -> i64;

        value answer: i64;
    }
}

let guest = Runtime::<CPython>::builder()
    .build()?
    .guest()
    .build()?;

let math = Math::from(
    guest.guest_module(
        "math",
        r#"
def add(left, right):
    return left + right

answer = 42
"#,
    )?,
);

assert_eq!(math.add(20, 22)?, 42);
assert_eq!(math.answer()?, 42);

Declare the functions and values the host needs. The generated facade converts each result into the descriptor type declared in the macro.

Typed guest classes

Use guestpy::guest_class! when the host knows the interface of a Python class or instance:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

guestpy::guest_class! {
    pub class Client {
        fn get(path: String) -> Response<B>;

        value prefix: String;
    }
}

guestpy::guest_class! {
    pub class Response {
        fn status() -> i64;
    }
}

guestpy::guest_module! {
    pub module Plugin {
        #[guestpy(name = "Client")]
        value client_class: Class<B, Client<B>>;

        value default_client: Client<B>;
    }
}

let guest = Runtime::<CPython>::builder()
    .build()?
    .guest()
    .build()?;

let plugin = Plugin::from(
    guest.guest_module(
        "plugin",
        r#"
class Response:
    def __init__(self, code):
        self.code = code

    def status(self):
        return self.code

class Client:
    def __init__(self, prefix):
        self.prefix = prefix

    def get(self, path):
        return Response(len(self.prefix + path))

default_client = Client('default:')
"#,
    )?,
);

let client = plugin
    .client_class()?
    .construct(("api:".to_owned(),))?;

assert_eq!(client.prefix()?, "api:");
assert_eq!(client.get("users".to_owned())?.status()?, 9);

A Class<B, R> remembers the descriptor used to convert its constructed instance. A generated class facade wraps an Instance<B> and calls Python attributes normally, so the same facade works for Python-defined classes or host-injected classes. Use Object<B> for unrestricted dynamic access, Instance<B> for a dynamic instance view, and Instance<B, C> when the live object carries a checked host payload C that Rust must borrow.

Guest-side async

Calling a Python async def function returns a coroutine object; converting it to a Coroutine<B, T> or Awaitable<B, T> and awaiting that value from an async Rust function drives the guest's own asyncio event loop until the coroutine completes, and produces the awaited Python return value as a T:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

#[tokio::main]
async fn main() -> Result<(), Error> {
    let guest = Runtime::<CPython>::builder()
        .build()?
        .guest()
        .build()?;

    guest.exec(
        r#"
import asyncio

async def double(value):
    await asyncio.sleep(0)
    return value * 2
"#,
    )?;

    let doubled = guest
        .globals()?
        .item::<Function<_>, _>("double")?
        .call::<_, Coroutine<_, i64>>((21,))?
        .await?;

    assert_eq!(doubled, 42);

    Ok(())
}

Use Coroutine<B, T> when the result must be a coroutine. Use Awaitable<B, T> when guest code may return either a direct value or an awaitable. Other host and guest operations are synchronous.

Plain Rust data

Derive ToGuest and FromGuest for ordinary serde-compatible Rust structs and enums under the serde Cargo feature. These derives delegate the entire conversion to the type's own serde::Serialize/serde::de::DeserializeOwned implementation, so ordinary #[serde(...)] field and variant attributes define the exact Python-side representation:

[dependencies]
serde = { version = "1", features = ["derive"] }
use guestpy::prelude::*;

#[derive(
    Debug,
    PartialEq,
    serde::Serialize,
    serde::Deserialize,
    guestpy::ToGuest,
    guestpy::FromGuest,
)]
struct Request {
    #[serde(rename = "userId")]
    user_id: u64,
    note: Option<String>,
}

The derives follow the type's serde representation. Standard Rust primitives, options, collections, arrays, tuples, and iterable values also cross the boundary directly. Use a host class when guest code needs to retain Rust object identity rather than receive copied data.

Union and protocol inputs

Mark an enum with #[guestpy(union)] when a host API accepts several guest value types. Every variant must contain exactly one unnamed field:

#[derive(guestpy::FromGuest, guestpy::ToGuest)]
#[guestpy(union)]
enum Identifier {
    Number(i64),
    Text(String),
}

FromGuest tries variants in declaration order and selects the first conversion that succeeds. A conversion mismatch continues to the next variant, while guest exceptions and other failures return immediately. If no variant matches, guest code receives a TypeError naming every accepted type. Nested unions flatten and deduplicate those names. Because every attempt sees a clone of the same guest handle rather than a copy of the guest object, put variants that consume one-shot iterators after variants that only inspect the value.

Use #[guestpy(union, backend = B)] when the enum already declares B as its backend type parameter. Without backend = B, the derives introduce their own backend parameter.

Host classes

#[guestpy::host_class] exposes an ordinary Rust type as a Python class. Mark the constructor, methods, and properties guest code needs:

use guestpy::prelude::*;

struct Vector2 {
    x: f64,
    y: f64,
}

#[guestpy::host_class]
impl Vector2 {
    #[guestpy(constructor)]
    fn new(x: f64, y: f64) -> Result<Self, Error> {
        Ok(Self { x, y })
    }

    #[guestpy(method)]
    fn length(&self) -> Result<f64, Error> {
        Ok(self.x.hypot(self.y))
    }

    #[guestpy(get)]
    fn x(&self) -> Result<f64, Error> {
        Ok(self.x)
    }
}

Guest code sees the generated class exactly as if it were written in Python:

vector = Vector2(3, 4)
print(vector.x)
print(vector.length())

Returning a host-class value to Rust preserves the live guest instance rather than cloning the Rust payload. Use Instance<B, Vector2>::borrow_with or borrow_with_mut when the host needs direct payload access; ordinary facade calls still use Python dispatch.

Subclassing

A host class can inherit from other host classes and Python classes. List bases in Python MRO order; Rust types name host bases, while a "module:qualname" string names a Python base:

#[guestpy::host_class(
    backend = B,
    extends(Headers, "collections.abc:Mapping"),
)]
impl CaseInsensitiveHeaders {}

GuestPy resolves imported bases through the runtime's Python importer, so the resulting class follows the normal Python inheritance and metaclass rules.

The macro also supports mutable methods, class-level members, Python protocol methods, and asynchronous host work. Refer to the API documentation when one of those capabilities is needed.

Backend-generic host classes

A host class that stores guest values rather than plain Rust data is generic over the backend. Name the backend parameter with backend so the macro knows which one it is:

use guestpy::prelude::*;

struct Envelope<B: Backend> {
    payload: Object<B>,
}

#[guestpy::host_class(backend = B)]
impl<B: Backend> Envelope<B> {
    #[guestpy(constructor)]
    fn new(payload: Object<B>) -> Result<Self, Error> {
        Ok(Self { payload })
    }

    #[guestpy(get)]
    fn payload(&self) -> Result<Object<B>, Error> {
        Ok(self.payload.clone())
    }
}

Register it like any other class, naming the parameter the generated module method is generic over:

struct Mail;

#[guestpy::host_module(name = "host_mail", classes(Envelope<B>))]
impl Mail {}

A host class does not have to carry the backend in its own type. A class that holds no guest data still needs to speak in terms of one, and naming a parameter the impl does not declare is how it does so; the macro declares that parameter on each exported member:

struct Contract;

#[guestpy::host_class(backend = B)]
impl Contract {
    #[guestpy(method)]
    fn invoke(#[guestpy(this)] this: &Object<B>) -> Result<String, Error> {
        this.type_name()
    }
}

In both forms backend names a type parameter. Whether it is the one the impl already declares or one the macro adds to the members is decided by the impl, not by the attribute. To pin a class to a single interpreter instead, name the backend type with pin:

#[guestpy::host_class(backend(pin = CPython))]
impl Envelope { ... }

Host modules

#[guestpy::host_module] exposes Rust functions and classes through a Python-importable module:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

struct Geometry;

#[guestpy::host_module(name = "host_geometry")]
impl Geometry {
    #[guestpy(function)]
    fn hypot(left: f64, right: f64) -> Result<f64, Error> {
        Ok(left.hypot(right))
    }
}

let runtime = Runtime::<CPython>::builder()
    .bind(Geometry.module())
    .build()?;

Guest Python code imports the generated exports as ordinary module attributes:

import host_geometry

print(host_geometry.hypot(5, 12))

The macro can also expose values, getters, nested objects, initialization, and guest-visible exceptions. Bind one module directly to a runtime or guest builder according to the scope that should receive it. Use HostLibrary when that scope needs more than one host module.

Native libraries

Native libraries are the backend-specific escape hatch for a module that cannot be expressed as a host module. Prefer #[guestpy::host_module] for normal integrations. For CPython, build a native pyo3 module and bind it directly. This advanced example also needs a direct pyo3 dependency because it constructs pyo3 values itself:

use std::rc::Rc;

use guestpy::prelude::*;
use guestpy::pyo3::CPython;
use pyo3::{Python, types::PyModule};

let native = NativeModule::<_>::new(
    "host_environment",
    Rc::new(|py: Python<'_>| {
        let module = PyModule::new(py, "host_environment")?;

        module.add("runtime", "guestpy")?;

        Ok(module.into())
    }),
);

let runtime = Runtime::<CPython>::builder()
    .bind_native(native)
    .build()?;

Bind one native module directly to a runtime or guest builder. Use NativeLibrary when that scope needs more than one native module or initializer.

Direct guest entry

Guest::enter gives a synchronous Enter<'py, B> outside a host callback, for code that needs one but is not already running inside guest work, such as reconstructing a typed exception after a call returns:

let timeout = guest.enter(|enter| Ok(RequestTimeout::caught(enter, &exception)?))?;

It runs under the same timeout and cancellation policy as any other guest operation.

Errors

Every GuestPy operation returns Result<T, guestpy::Error>. Inspect a guest exception when Python code fails, and use normal Rust error handling for engine, conversion, import, and execution-control failures. Match a known Python exception when the host can recover from it, and propagate every other error:

use guestpy::prelude::*;
use guestpy::pyo3::CPython;

fn read_count(guest: &Guest<CPython>) -> Result<Option<i64>, Error> {
    match guest.eval::<i64>("int('not a number')") {
        Ok(count) => Ok(Some(count)),
        Err(Error::Guest(exception)) if exception.matches("ValueError") => {
            eprintln!(
                "guest rejected the count: {}",
                exception.message(),
            );

            Ok(None)
        }
        Err(error) => Err(error),
    }
}

GuestException::matches recognizes the exception's Python inheritance hierarchy. Use its qualified_name, message, and optional traceback when reporting a guest failure. Host callables can return application errors that convert into guestpy::Error:

#[derive(Debug, thiserror::Error)]
#[error("geometry operation failed")]
struct GeometryError;

impl From<GeometryError> for guestpy::Error {
    fn from(error: GeometryError) -> Self {
        guestpy::Error::sourced_unexpected(error.to_string(), error)
    }
}

Typed host exceptions

Use a typed host exception when Python needs to catch a structured failure raised by Rust. Derive HostException, register it with the host module that exposes the operation, then return Raise::host from that operation:

use guestpy::prelude::*;

#[derive(HostException)]
#[guestpy(builtin = "TimeoutError")]
struct RequestTimeout {
    #[guestpy(arg)]
    message: String,
    request_id: String,
}

#[host_module(name = "http", exceptions(RequestTimeout))]
impl Http {
    #[guestpy(function)]
    fn fetch<B>(request_id: String) -> Result<(), Error>
    where
        B: Backend,
        String: ToGuest<B>,
    {
        Err(Raise::<B>::host(RequestTimeout {
            message: "request timed out".to_owned(),
            request_id,
        })
        .into())
    }
}

The registered exception behaves like an ordinary Python exception. Its #[guestpy(arg)] fields become constructor arguments, while its other fields become attributes:

import http

try:
    http.fetch("request-1")
except http.RequestTimeout as error:
    assert error.args == ("request timed out",)
    assert error.request_id == "request-1"

If Python does not catch the exception, Rust receives it as Error::Guest. Match the typed class without re-entering the guest:

match client.call::<_, Response>((request,)) {
    Err(Error::Guest(exception))
        if RequestTimeout::class().matches(&exception) =>
    {
        retry()
    }
    Err(Error::Guest(exception))
        if ExceptionClass::builtin("KeyError").matches(&exception) =>
    {
        use_default()
    }
    result => result,
}

When a host callback needs the structured fields again, request its existing entry context and reconstruct the typed exception there:

#[guestpy(function)]
fn forward<B>(
    #[guestpy(enter)] enter: &Enter<'_, B>,
    client: Client<B>,
) -> Result<Response<B>, Error>
where
    B: Backend + BackendValues,
    RequestTimeout: FromRaised<B>,
{
    match client.send() {
        Err(Error::Guest(exception)) => {
            match RequestTimeout::caught(enter, &exception)? {
                Some(timeout) => retry(timeout.request_id),
                None => Err(Error::Guest(exception)),
            }
        }
        result => result,
    }
}

License

GuestPy is licensed under the ISC License. See LICENSE.

Support and feedback

Report problems and feature requests through the repository issue tracker.

About

A Rust library for executing guest Python code transparently.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages