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

Function register_callback

crates/vm/src/stdlib/sys/monitoring.rs:587–619  ·  view source on GitHub ↗
(
    tool_id: i32,
    event: i32,
    func: PyObjectRef,
    vm: &VirtualMachine,
)

Source from the content-addressed store, hash-verified

585}
586
587fn register_callback(
588 tool_id: i32,
589 event: i32,
590 func: PyObjectRef,
591 vm: &VirtualMachine,
592) -> PyResult<PyObjectRef> {
593 let tool = check_valid_tool(tool_id, vm)?;
594 let event_id = parse_single_event(event, vm)?;
595
596 let mut state = vm.state.monitoring.lock();
597 let prev = state
598 .callbacks
599 .remove(&(tool, event_id))
600 .unwrap_or_else(|| vm.ctx.none());
601 let branch_id = EVENT_BRANCH.trailing_zeros() as usize;
602 let branch_left_id = EVENT_BRANCH_LEFT.trailing_zeros() as usize;
603 let branch_right_id = EVENT_BRANCH_RIGHT.trailing_zeros() as usize;
604 if !vm.is_none(&func) {
605 state.callbacks.insert((tool, event_id), func.clone());
606 // BRANCH is a composite event: also register for BRANCH_LEFT/RIGHT
607 if event_id == branch_id {
608 state.callbacks.insert((tool, branch_left_id), func.clone());
609 state.callbacks.insert((tool, branch_right_id), func);
610 }
611 } else {
612 // Also clear BRANCH_LEFT/RIGHT when clearing BRANCH
613 if event_id == branch_id {
614 state.callbacks.remove(&(tool, branch_left_id));
615 state.callbacks.remove(&(tool, branch_right_id));
616 }
617 }
618 Ok(prev)
619}
620
621fn get_events(tool_id: i32, vm: &VirtualMachine) -> PyResult<u32> {
622 let tool = check_valid_tool(tool_id, vm)?;

Callers

nothing calls this directly

Calls 8

check_valid_toolFunction · 0.85
parse_single_eventFunction · 0.85
noneMethod · 0.80
lockMethod · 0.45
removeMethod · 0.45
is_noneMethod · 0.45
insertMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected