From c65c2ff8787322a7de7c4e9eac77112bd2dc4158 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Fri, 31 Mar 2023 00:30:45 +0900 Subject: [PATCH] make adding a single module simpler for interpreter users --- src/interpreter.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index 89b0bc6e006..7951430947d 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1,4 +1,4 @@ -use rustpython_vm::{Interpreter, Settings, VirtualMachine}; +use rustpython_vm::{builtins::PyModule, Interpreter, PyRef, Settings, VirtualMachine}; pub type InitHook = Box; @@ -29,6 +29,15 @@ impl InterpreterConfig { self.init_hooks.push(hook); self } + pub fn add_native_module( + self, + name: String, + make_module: fn(&VirtualMachine) -> PyRef, + ) -> Self { + self.init_hook(Box::new(move |vm| { + vm.add_native_module(name, Box::new(make_module)) + })) + } #[cfg(feature = "stdlib")] pub fn init_stdlib(self) -> Self { self.init_hook(Box::new(init_stdlib))