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

Function with_vm

crates/vm/src/vm/thread.rs:500–522  ·  view source on GitHub ↗
(obj: &PyObject, f: F)

Source from the content-addressed store, hash-verified

498}
499
500pub fn with_vm<F, R>(obj: &PyObject, f: F) -> Option<R>
501where
502 F: Fn(&VirtualMachine) -> R,
503{
504 let vm_owns_obj = |interp: NonNull<VirtualMachine>| {
505 // SAFETY: all references in VM_STACK should be valid
506 let vm = unsafe { interp.as_ref() };
507 obj.fast_isinstance(vm.ctx.types.object_type)
508 };
509 VM_STACK.with(|vms| {
510 let interp = match vms.borrow().iter().copied().exactly_one() {
511 Ok(x) => {
512 debug_assert!(vm_owns_obj(x));
513 x
514 }
515 Err(mut others) => others.find(|x| vm_owns_obj(*x))?,
516 };
517 // SAFETY: all references in VM_STACK should be valid, and should not be changed or moved
518 // at least until this function returns and the stack unwinds to an enter_vm() call
519 let vm = unsafe { interp.as_ref() };
520 Some(VM_CURRENT.set(vm, || f(vm)))
521 })
522}
523
524#[must_use = "ThreadedVirtualMachine does nothing unless you move it to another thread and call .run()"]
525#[cfg(feature = "threading")]

Callers 4

collect_innerMethod · 0.85
clearMethod · 0.85
call_slot_delMethod · 0.85
try_call_finalizerMethod · 0.85

Calls 9

fast_isinstanceMethod · 0.80
withMethod · 0.80
SomeClass · 0.50
fFunction · 0.50
as_refMethod · 0.45
iterMethod · 0.45
borrowMethod · 0.45
findMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected