From 8b6b36a64e11bcd3f4e6f5a1400b67d26b96dc3b Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 21 Apr 2025 20:30:37 -0700 Subject: [PATCH] further updates --- src/SUMMARY.md | 1 + src/error_handling.md | 7 +++++++ src/getting_started/installation.md | 6 ++---- src/interop/python_from_rust.md | 30 ++++++++++++++++++++++++++++- src/introduction/index.md | 6 +++++- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/error_handling.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 7bbc211..6f530e3 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -7,3 +7,4 @@ - [Interop](interop/index.md) - [Calling Rust from Python](interop/rust_from_python.md) - [Calling Python from Rust](interop/python_from_rust.md) +- [Error Handling](error_handling.md) \ No newline at end of file diff --git a/src/error_handling.md b/src/error_handling.md new file mode 100644 index 0000000..7b3dcf4 --- /dev/null +++ b/src/error_handling.md @@ -0,0 +1,7 @@ +# Error Handling + +Most errors are considered `PyResult`s, this is re-exported as `rustpython_vm::PyResult`. + +The error in a `PyResult` is a `PyBaseExceptionRef`, which is a reference to a base error (documented as `PyBaseException`). + +It contains methods that return more information. diff --git a/src/getting_started/installation.md b/src/getting_started/installation.md index d7ae156..aec6704 100644 --- a/src/getting_started/installation.md +++ b/src/getting_started/installation.md @@ -25,10 +25,9 @@ The tag should be pointed to the latest tag found at https://github.com/RustPyth ## Features By default `threading`, `stdlib`, and `importlib` are enabled. -### `bz2` -If you'd like to use the `bz2` module, you can enable the `bz2` feature. ### `stdlib` -`stdlib` is the default feature that enables the standard library. +`stdlib` is the default feature that enables the standard library, +parts of the stdlib will still be accessible even if this is disabled. ### `sqlite` If you'd like to use the `sqlite3` module, you can enable the `sqlite` feature. ### `ssl` @@ -37,4 +36,3 @@ which also lets you install the pip package manager. Note that on Windows, you may need to install OpenSSL, or you can enable the ssl-vendor feature instead, which compiles OpenSSL for you but requires a C compiler, perl, and make. OpenSSL version 3 is expected and tested in CI. Older versions may not work. - diff --git a/src/interop/python_from_rust.md b/src/interop/python_from_rust.md index dae5f91..44c49e0 100644 --- a/src/interop/python_from_rust.md +++ b/src/interop/python_from_rust.md @@ -1,2 +1,30 @@ # Calling Python from Rust -TODO. \ No newline at end of file + +## Running code + +The simplest way to do this is `VirtualMachine::run_code_string`. +This requires a scope, which can be created with `VirtualMachine::new_scope_with_builtins` +```rust +let scope = vm.new_scope_with_builtins(); +vm.run_code_string(scope, "print('hello')", "").unwrap(); +``` + +If your code is from a path, consider using `VirtualMachine::run_script` + +```rust +let scope = vm.new_scope_with_builtins(); +vm.run_code_string(scope, "hello.py").unwrap(); +``` + +### Compilation +```rust +vm.compile("print('hello')", vm::compiler::Mode::Exec, "".to_owned()) +``` +once compiled, the code can be called any number of times with +```rust +let scope = vm.new_scope_with_builtins(); +vm.run_code_obj(code_obj, scope)?; +``` + +## Calling specific code +TODO. diff --git a/src/introduction/index.md b/src/introduction/index.md index 5ff8c66..f4c9a2f 100644 --- a/src/introduction/index.md +++ b/src/introduction/index.md @@ -1,3 +1,7 @@ # Introduction -RustPython is a Python interpreter written in Rust. +RustPython is a Python 3 interpreter written in Rust. +RustPython can be embedded in existing rust projects or can be run via WASM to run python in a sandboxed enviorment, or in the browser. + +RustPython provides a mostly complete version of the standard library. +Major incompatabilities can be found here: https://rustpython.github.io/pages/whats-left