Files
RustPython/vm/src/eval.rs
yanganto edf70924a9 Set initial parameter in PySettings
- Initialize VM after VM created
	- Some(true):
		VM will be created and initialized with the importers, which require external
		filesystem access
	- Some(false):
		VM will be created and initialized with the importers wihout external filesystem access
	- None:
		VM will be created but not initialized, so it is ready to be injected with modules.
		After injection, the `initialize_as_external` or `initialize_without_external`
		should be called, such that the VM will be ready to use.

- Prevent double initialize VM by `initialized` flag
2019-12-05 10:14:40 +08:00

31 lines
930 B
Rust

use crate::pyobject::PyResult;
use crate::scope::Scope;
use crate::vm::VirtualMachine;
use rustpython_compiler::compile;
pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str) -> PyResult {
match vm.compile(source, compile::Mode::Eval, source_path.to_string()) {
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::eval;
use super::VirtualMachine;
use crate::pyobject::IdProtocol;
#[test]
fn test_print_42() {
let source = String::from("print('Hello world')");
let vm = VirtualMachine::default();
let vars = vm.new_scope_with_builtins();
let result = eval(&vm, &source, vars, "<unittest>").expect("this should pass");
assert!(result.is(&vm.ctx.none()));
}
}