-
Notifications
You must be signed in to change notification settings - Fork 1.3k
decorator with type hint breaks #4173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This code is also breaking: from typing import Any
def hello_world() -> Any:
return 'Hello world!' |
Here's the full code that breaks, this is a wasm32-unknown-unknown environment without wasm_bindgen: use rustpython;
fn custom_getrandom(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
Ok(())
}
getrandom::register_custom_getrandom!(custom_getrandom);
#[ic_cdk_macros::query]
fn simple_query() -> String {
let interpreter = rustpython::vm::Interpreter::without_stdlib(Default::default());
let result = interpreter.enter(|vm| {
let scope = vm.new_scope_with_builtins();
vm.run_code_string(
scope.clone(),
r#"
from typing import Any
def hello_world() -> Any:
return "Hello Dan and Ben!"
result = hello_world()
"#,
"".to_owned(),
)
.unwrap();
let result_value = scope.globals.get_item("result", vm).unwrap();
let result_string: String = result_value.try_into_value(vm).unwrap();
result_string
});
result
} |
Does this possibly have to do with |
I get an error like this: thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: PyBaseException', query/src/main.rs:28:10 It would help if I could get some useful information out of |
To make things clear, do you mean CPython when you said 'my machine'?
Would this be ok? |
On my machine, I mean compiling and running RustPython for my laptop computer, so probably x86. Usually I'm compiling to Wasm to run in an Internet Computer replica, which is a much stricter wasm32-unknown-unknown environment. I will try your suggestions, I thought vm.scope_with_builtins or whatever it was called provided this std library |
Hmmm...what if I were able to get a |
I just need to do something like this? let interpreter = rustpython::vm::Interpreter::with_init(Default::default(), |vm| {
vm.add_native_modules(rustpython::vm::stdlib::get_module_inits());
}); I still get a runtime error but if I'm on the right track, this doesn't seem too difficult. Based on the API you have I would expect a |
I have turned all features off, do I need to enable a feature to get the stdlib to work? |
I have in my Cargo.toml:
Here's all of the code for a self-contained example: use rustpython::{self, vm::convert::ToPyObject};
fn custom_getrandom(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
Ok(())
}
getrandom::register_custom_getrandom!(custom_getrandom);
#[ic_cdk_macros::query]
fn simple_query() -> String {
// let interpreter = rustpython::vm::Interpreter::without_stdlib(Default::default());
let interpreter = rustpython::vm::Interpreter::with_init(Default::default(), |vm| {
vm.add_native_modules(rustpython::vm::stdlib::get_module_inits());
});
let result = interpreter.enter(|vm| {
let scope = vm.new_scope_with_builtins();
// vm.invoke(func, args)
// let py_object_ref_string = "hello there".to_string().to_pyobject(vm);
// let py_object_ref_bool = true.to_pyobject(vm);
// vm.invoke(func, args)
// scope = scope;
// TODO importing and using Any breaks!
vm.run_code_string(
scope.clone(),
r#"
from typing import Any
def hello_world() -> Any:
return "Hello Dan and Ben!"
result = hello_world()
"#,
"".to_owned(),
)
.unwrap();
let result_value = scope.globals.get_item("result", vm).unwrap();
let result_string: String = result_value.try_into_value(vm).unwrap();
result_string
});
result
} Am I doing something wrong? |
When running similar code using RustPython on my machine compiled to x86, I was able to figure out the error:
Did I do something wrong with |
I have figured it out. This was very tricky to figure out. It might be good to add better documentation or refactor the code to make this easier. Also, this might be a bug, because I would expect So the problem seems to be that I needed to add the let interpreter = rustpython::vm::Interpreter::with_init(Default::default(), |vm| {
vm.add_frozen(rustpython_pylib::frozen_stdlib());
}); So that worked when compiling and running the code on my machine, x86 Ubuntu Linux (I don't know what target that is). I haven't actually been able to get this running in my So a couple questions:
|
This code runs fine on my machine but not from within RustPython:
When you remove the Any type hint the code executes fine in RustPython.
The text was updated successfully, but these errors were encountered: