mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
31 lines
903 B
Rust
31 lines
903 B
Rust
use crate::compile;
|
|
use crate::scope::Scope;
|
|
use crate::PyResult;
|
|
use crate::VirtualMachine;
|
|
|
|
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
|
|
match vm.compile(source, compile::Mode::Eval, source_path.to_owned()) {
|
|
Ok(bytecode) => {
|
|
debug!("Code object: {:?}", bytecode);
|
|
vm.run_code_obj(bytecode, scope)
|
|
}
|
|
Err(err) => Err(vm.new_syntax_error(&err)),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::Interpreter;
|
|
|
|
#[test]
|
|
fn test_print_42() {
|
|
Interpreter::without_stdlib(Default::default()).enter(|vm| {
|
|
let source = String::from("print('Hello world')");
|
|
let vars = vm.new_scope_with_builtins();
|
|
let result = eval(vm, &source, vars, "<unittest>").expect("this should pass");
|
|
assert!(vm.is_none(&result));
|
|
})
|
|
}
|
|
}
|