rustpython_vm/
eval.rs

1use crate::{compiler, scope::Scope, PyResult, VirtualMachine};
2
3pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
4    match vm.compile(source, compiler::Mode::Eval, source_path.to_owned()) {
5        Ok(bytecode) => {
6            debug!("Code object: {:?}", bytecode);
7            vm.run_code_obj(bytecode, scope)
8        }
9        Err(err) => Err(vm.new_syntax_error(&err, Some(source))),
10    }
11}
12
13#[cfg(test)]
14mod tests {
15    use super::*;
16    use crate::Interpreter;
17
18    #[test]
19    fn test_print_42() {
20        Interpreter::without_stdlib(Default::default()).enter(|vm| {
21            let source = String::from("print('Hello world')");
22            let vars = vm.new_scope_with_builtins();
23            let result = eval(vm, &source, vars, "<unittest>").expect("this should pass");
24            assert!(vm.is_none(&result));
25        })
26    }
27}