Skip to content

sys.setswitchinterval #5817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
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
2 changes: 0 additions & 2 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,8 +1691,6 @@ def f(zomg: 'zomg_annotation'):
for attr in self.module.WRAPPER_ASSIGNMENTS:
self.assertEqual(getattr(g, attr), getattr(f, attr))

# TODO: RUSTPYTHON
@unittest.expectedFailure
@threading_helper.requires_working_threading()
def test_lru_cache_threaded(self):
n, m = 5, 11
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@ def test_getdefaultencoding(self):
# testing sys.settrace() is done in test_sys_settrace.py
# testing sys.setprofile() is done in test_sys_setprofile.py

# TODO: RUSTPYTHON, AttributeError: module 'sys' has no attribute 'setswitchinterval'
@unittest.expectedFailure
def test_switchinterval(self):
self.assertRaises(TypeError, sys.setswitchinterval)
self.assertRaises(TypeError, sys.setswitchinterval, "a")
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_syslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ def test_openlog_noargs(self):
syslog.openlog()
syslog.syslog('test message from python test_syslog')

# TODO: RUSTPYTHON; AttributeError: module 'sys' has no attribute 'getswitchinterval'
@unittest.expectedFailure
@threading_helper.requires_working_threading()
def test_syslog_threaded(self):
start = threading.Event()
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,6 @@ def child():
b"Woke up, sleep function is: <built-in function sleep>")
self.assertEqual(err, b"")

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_enumerate_after_join(self):
# Try hard to trigger #1703448: a thread is still returned in
# threading.enumerate() after it has been join()ed.
Expand Down
19 changes: 19 additions & 0 deletions vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,25 @@ mod sys {
crate::vm::thread::COROUTINE_ORIGIN_TRACKING_DEPTH.with(|cell| cell.get()) as _
}

#[pyfunction]
fn getswitchinterval(vm: &VirtualMachine) -> f64 {
// Return the stored switch interval
vm.state.switch_interval.load()
}

// TODO: vm.state.switch_interval is currently not used anywhere in the VM
#[pyfunction]
fn setswitchinterval(interval: f64, vm: &VirtualMachine) -> PyResult<()> {
// Validate the interval parameter like CPython does
if interval <= 0.0 {
return Err(vm.new_value_error("switch interval must be strictly positive".to_owned()));
}

// Store the switch interval value
vm.state.switch_interval.store(interval);
Ok(())
}

#[derive(FromArgs)]
struct SetAsyncgenHooksArgs {
#[pyarg(any, optional)]
Expand Down
2 changes: 2 additions & 0 deletions vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub struct PyGlobalState {
pub after_forkers_child: PyMutex<Vec<PyObjectRef>>,
pub after_forkers_parent: PyMutex<Vec<PyObjectRef>>,
pub int_max_str_digits: AtomicCell<usize>,
pub switch_interval: AtomicCell<f64>,
}

pub fn process_hash_secret_seed() -> u32 {
Expand Down Expand Up @@ -189,6 +190,7 @@ impl VirtualMachine {
after_forkers_child: PyMutex::default(),
after_forkers_parent: PyMutex::default(),
int_max_str_digits,
switch_interval: AtomicCell::new(0.005),
}),
initialized: false,
recursion_depth: Cell::new(0),
Expand Down
Loading