Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make adding a single module simpler for interpreter users
  • Loading branch information
youknowone committed Jun 14, 2023
commit c65c2ff8787322a7de7c4e9eac77112bd2dc4158
11 changes: 10 additions & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustpython_vm::{Interpreter, Settings, VirtualMachine};
use rustpython_vm::{builtins::PyModule, Interpreter, PyRef, Settings, VirtualMachine};

pub type InitHook = Box<dyn FnOnce(&mut VirtualMachine)>;

Expand Down Expand Up @@ -29,6 +29,15 @@ impl InterpreterConfig {
self.init_hooks.push(hook);
self
}
pub fn add_native_module(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a good idea to document this one then if users are really interested in it (ideally pointing to an example someplace).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for a nice advice, really.
I am sorry that I am currently barely maintaining the project and probably not going to finish it in near future.
I am going to merge this PR not because the advice is less useful but just because we eventually need to finish both the code and document. I had to finish at the moment, probably I shared example to the discord chat.

self,
name: String,
make_module: fn(&VirtualMachine) -> PyRef<PyModule>,
) -> 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))
Expand Down