From b0e90d1b45bbd4850508a1d279db2148b99f3a7a Mon Sep 17 00:00:00 2001 From: Markus Moenig Date: Sat, 2 Aug 2025 08:35:35 +0700 Subject: [PATCH 1/4] Create embedded_id.rs --- examples/embedded_id.rs | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/embedded_id.rs diff --git a/examples/embedded_id.rs b/examples/embedded_id.rs new file mode 100644 index 0000000000..6f09d2a5ed --- /dev/null +++ b/examples/embedded_id.rs @@ -0,0 +1,50 @@ +use rustpython::vm::*; +use rustpython_vm as vm; +use rustpython_vm::builtins::PyModule; + +// A global fn called from Python which extracts the id from the module and prints it. +fn print_id(vm: &vm::VirtualMachine) { + let module = vm.import("__id_module", 0).ok().unwrap(); + let obj = module.get_attr("__id", vm).ok().unwrap(); + let id = obj.try_to_value::(vm).ok().unwrap(); + + println!("The id is {}", id); +} + +fn main() -> vm::PyResult<()> { + vm::Interpreter::without_stdlib(Default::default()).enter(|vm| { + let scope = vm.new_scope_with_builtins(); + + // Register the global function + let _ = + scope + .globals + .set_item("print_id", vm.new_function("print_id", print_id).into(), vm); + + // Create a module and set an id. + let module = PyModule::new().into_ref(&vm.ctx); + module + .as_object() + .set_attr("__id", vm.new_pyobj(42_i32), vm) + .ok() + .unwrap(); + + // Register the module + let sys = vm.import("sys", 0).ok().unwrap(); + let modules = sys.get_attr("modules", vm).ok().unwrap(); + modules + .set_item("__id_module", module.into(), vm) + .ok() + .unwrap(); + + // Execute the code + let source = r#"print_id()"#; + let code_obj = vm + .compile(source, vm::compiler::Mode::Exec, "".to_owned()) + .map_err(|err| vm.new_syntax_error(&err, Some(source)))?; + + vm.run_code_obj(code_obj, scope)?; + + Ok(()) + }) +} From 406c13dd04e1e7b0a747a3a19ded314acef985de Mon Sep 17 00:00:00 2001 From: Markus Moenig Date: Mon, 4 Aug 2025 16:44:27 +0700 Subject: [PATCH 2/4] Renamed and fixed return types --- examples/embedded_id.rs | 50 ------------------------------------ examples/module_injection.rs | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 50 deletions(-) delete mode 100644 examples/embedded_id.rs create mode 100644 examples/module_injection.rs diff --git a/examples/embedded_id.rs b/examples/embedded_id.rs deleted file mode 100644 index 6f09d2a5ed..0000000000 --- a/examples/embedded_id.rs +++ /dev/null @@ -1,50 +0,0 @@ -use rustpython::vm::*; -use rustpython_vm as vm; -use rustpython_vm::builtins::PyModule; - -// A global fn called from Python which extracts the id from the module and prints it. -fn print_id(vm: &vm::VirtualMachine) { - let module = vm.import("__id_module", 0).ok().unwrap(); - let obj = module.get_attr("__id", vm).ok().unwrap(); - let id = obj.try_to_value::(vm).ok().unwrap(); - - println!("The id is {}", id); -} - -fn main() -> vm::PyResult<()> { - vm::Interpreter::without_stdlib(Default::default()).enter(|vm| { - let scope = vm.new_scope_with_builtins(); - - // Register the global function - let _ = - scope - .globals - .set_item("print_id", vm.new_function("print_id", print_id).into(), vm); - - // Create a module and set an id. - let module = PyModule::new().into_ref(&vm.ctx); - module - .as_object() - .set_attr("__id", vm.new_pyobj(42_i32), vm) - .ok() - .unwrap(); - - // Register the module - let sys = vm.import("sys", 0).ok().unwrap(); - let modules = sys.get_attr("modules", vm).ok().unwrap(); - modules - .set_item("__id_module", module.into(), vm) - .ok() - .unwrap(); - - // Execute the code - let source = r#"print_id()"#; - let code_obj = vm - .compile(source, vm::compiler::Mode::Exec, "".to_owned()) - .map_err(|err| vm.new_syntax_error(&err, Some(source)))?; - - vm.run_code_obj(code_obj, scope)?; - - Ok(()) - }) -} diff --git a/examples/module_injection.rs b/examples/module_injection.rs new file mode 100644 index 0000000000..eff693d0ea --- /dev/null +++ b/examples/module_injection.rs @@ -0,0 +1,48 @@ +use rustpython::vm::*; +use rustpython_vm as vm; +use rustpython_vm::builtins::PyModule; + +/// A global fn called from Python which extracts the id from the injected module and returns / prints it to the console. +/// This is useful in a multi-threaded environment where you may have several threads sharing global functions. The +/// id would allow a local context for each thread, for example by using a global Arc>. +fn get_id(vm: &vm::VirtualMachine) -> PyResult { + let module = vm.import("__id_module", 0)?; + let obj = module.get_attr("__id", vm)?; + let id = obj.try_to_value::(vm)?; + + println!("The id is {}", id); + + Ok(id) +} + +fn main() -> vm::PyResult<()> { + vm::Interpreter::without_stdlib(Default::default()).enter(|vm| { + let scope = vm.new_scope_with_builtins(); + + // Register the global function + let _ = scope + .globals + .set_item("get_id", vm.new_function("get_id", get_id).into(), vm); + + // Create a module and set an id + let module = PyModule::new().into_ref(&vm.ctx); + module + .as_object() + .set_attr("__id", vm.new_pyobj(42_i32), vm)?; + + // Register the module + let sys = vm.import("sys", 0)?; + let modules = sys.get_attr("modules", vm)?; + modules.set_item("__id_module", module.into(), vm)?; + + // Execute the code + let source = r#"get_id()"#; + let code_obj = vm + .compile(source, vm::compiler::Mode::Exec, "".to_owned()) + .map_err(|err| vm.new_syntax_error(&err, Some(source)))?; + + vm.run_code_obj(code_obj, scope)?; + + Ok(()) + }) +} From 9268e08aad944e11d704e5777e11ef658990596f Mon Sep 17 00:00:00 2001 From: Markus Moenig Date: Mon, 4 Aug 2025 16:54:37 +0700 Subject: [PATCH 3/4] Update examples/module_injection.rs Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- examples/module_injection.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/module_injection.rs b/examples/module_injection.rs index eff693d0ea..a70c57cc4e 100644 --- a/examples/module_injection.rs +++ b/examples/module_injection.rs @@ -1,4 +1,3 @@ -use rustpython::vm::*; use rustpython_vm as vm; use rustpython_vm::builtins::PyModule; From 5eff4822cb5236b0469769b6b0ca0dd5aba54df7 Mon Sep 17 00:00:00 2001 From: Markus Moenig Date: Tue, 5 Aug 2025 10:22:58 +0700 Subject: [PATCH 4/4] Fix compile error, sorry --- examples/module_injection.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/module_injection.rs b/examples/module_injection.rs index a70c57cc4e..4dc739a544 100644 --- a/examples/module_injection.rs +++ b/examples/module_injection.rs @@ -1,4 +1,4 @@ -use rustpython_vm as vm; +use rustpython::vm::*; use rustpython_vm::builtins::PyModule; /// A global fn called from Python which extracts the id from the injected module and returns / prints it to the console. @@ -14,7 +14,7 @@ fn get_id(vm: &vm::VirtualMachine) -> PyResult { Ok(id) } -fn main() -> vm::PyResult<()> { +fn main() -> PyResult<()> { vm::Interpreter::without_stdlib(Default::default()).enter(|vm| { let scope = vm.new_scope_with_builtins(); @@ -37,7 +37,7 @@ fn main() -> vm::PyResult<()> { // Execute the code let source = r#"get_id()"#; let code_obj = vm - .compile(source, vm::compiler::Mode::Exec, "".to_owned()) + .compile(source, compiler::Mode::Exec, "".to_owned()) .map_err(|err| vm.new_syntax_error(&err, Some(source)))?; vm.run_code_obj(code_obj, scope)?;