MCPcopy Index your code
hub / github.com/RustPython/RustPython / eval

Function eval

crates/vm/src/stdlib/builtins.rs:537–569  ·  view source on GitHub ↗
(
        source: Either<ArgStrOrBytesLike, PyRef<crate::builtins::PyCode>>,
        scope: ScopeArgs,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

535
536 #[pyfunction]
537 fn eval(
538 source: Either<ArgStrOrBytesLike, PyRef<crate::builtins::PyCode>>,
539 scope: ScopeArgs,
540 vm: &VirtualMachine,
541 ) -> PyResult {
542 let scope = scope.make_scope(vm, "eval")?;
543
544 // source as string
545 let code = match source {
546 Either::A(either) => {
547 let source: &[u8] = &either.borrow_bytes();
548 if source.contains(&0) {
549 return Err(vm.new_exception_msg(
550 vm.ctx.exceptions.syntax_error.to_owned(),
551 "source code string cannot contain null bytes".into(),
552 ));
553 }
554
555 let source = core::str::from_utf8(source).map_err(|err| {
556 let msg = format!(
557 "(unicode error) 'utf-8' codec can't decode byte 0x{:x?} in position {}: invalid start byte",
558 source[err.valid_up_to()],
559 err.valid_up_to()
560 );
561
562 vm.new_exception_msg(vm.ctx.exceptions.syntax_error.to_owned(), msg.into())
563 })?;
564 Ok(Either::A(vm.ctx.new_utf8_str(source.trim_start())))
565 }
566 Either::B(code) => Ok(Either::B(code)),
567 }?;
568 run_code(vm, code, scope, crate::compiler::Mode::Eval, "eval")
569 }
570
571 #[pyfunction]
572 fn exec(

Callers

nothing calls this directly

Calls 11

make_scopeMethod · 0.80
borrow_bytesMethod · 0.80
new_exception_msgMethod · 0.80
new_utf8_strMethod · 0.80
trim_startMethod · 0.80
run_codeFunction · 0.70
ErrClass · 0.50
AClass · 0.50
BClass · 0.50
containsMethod · 0.45
to_ownedMethod · 0.45

Tested by

no test coverage detected