Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
sys.get_coroutine_origin_tracking_depth
  • Loading branch information
youknowone committed May 1, 2024
commit b6524b9295a1989b025f5a9cda7e76d53df7a35a
14 changes: 14 additions & 0 deletions vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,20 @@ mod sys {
vm.use_tracing.set(tracing);
}

#[pyfunction]
fn set_coroutine_origin_tracking_depth(depth: i32, vm: &VirtualMachine) -> PyResult<()> {
if depth < 0 {
return Err(vm.new_value_error("depth must be >= 0".to_owned()));
}
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.with(|cell| cell.set(depth as _));
Ok(())
}

#[pyfunction]
fn get_coroutine_origin_tracking_depth() -> i32 {
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.with(|cell| cell.get()) as _
}

/// sys.flags
///
/// Flags provided through command line arguments or environment vars.
Expand Down
9 changes: 5 additions & 4 deletions vm/src/vm/thread.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::{AsObject, PyObject, VirtualMachine};
use itertools::Itertools;
use std::{
cell::RefCell,
ptr::{null, NonNull},
cell::{Cell, RefCell},
ptr::NonNull,
thread_local,
};

thread_local! {
pub(super) static VM_STACK: RefCell<Vec<NonNull<VirtualMachine>>> = Vec::with_capacity(1).into();
static VM_CURRENT: RefCell<*const VirtualMachine> = null::<VirtualMachine>().into();
static VM_CURRENT: RefCell<*const VirtualMachine> = std::ptr::null::<VirtualMachine>().into();

pub(crate) static COROUTINE_ORIGIN_TRACKING_DEPTH: Cell<u32> = const { Cell::new(0) };
}

pub fn with_current_vm<R>(f: impl FnOnce(&VirtualMachine) -> R) -> R {
Expand Down Expand Up @@ -142,7 +144,6 @@ impl VirtualMachine {
/// specific guaranteed behavior.
#[cfg(feature = "threading")]
pub fn new_thread(&self) -> ThreadedVirtualMachine {
use std::cell::Cell;
let vm = VirtualMachine {
builtins: self.builtins.clone(),
sys_module: self.sys_module.clone(),
Expand Down