Skip to content

Commit 78fae73

Browse files
committed
sys.get_coroutine_origin_tracking_depth
1 parent ed3811a commit 78fae73

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

vm/src/stdlib/sys.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,20 @@ mod sys {
692692
vm.use_tracing.set(tracing);
693693
}
694694

695+
#[pyfunction]
696+
fn set_coroutine_origin_tracking_depth(depth: i32, vm: &VirtualMachine) -> PyResult<()> {
697+
if depth < 0 {
698+
return Err(vm.new_value_error("depth must be >= 0".to_owned()));
699+
}
700+
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.with(|cell| cell.set(depth as _));
701+
Ok(())
702+
}
703+
704+
#[pyfunction]
705+
fn get_coroutine_origin_tracking_depth() -> i32 {
706+
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.with(|cell| cell.get()) as _
707+
}
708+
695709
/// sys.flags
696710
///
697711
/// Flags provided through command line arguments or environment vars.

vm/src/vm/thread.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::{AsObject, PyObject, VirtualMachine};
22
use itertools::Itertools;
33
use std::{
4-
cell::RefCell,
5-
ptr::{null, NonNull},
4+
cell::{Cell, RefCell},
5+
ptr::NonNull,
66
thread_local,
77
};
88

99
thread_local! {
1010
pub(super) static VM_STACK: RefCell<Vec<NonNull<VirtualMachine>>> = Vec::with_capacity(1).into();
11-
static VM_CURRENT: RefCell<*const VirtualMachine> = null::<VirtualMachine>().into();
11+
static VM_CURRENT: RefCell<*const VirtualMachine> = std::ptr::null::<VirtualMachine>().into();
12+
13+
pub(crate) static COROUTINE_ORIGIN_TRACKING_DEPTH: Cell<u32> = const { Cell::new(0) };
1214
}
1315

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

0 commit comments

Comments
 (0)