From 4dc5323c349657ce9fa96fc4de6c959ae932c1ac Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Sun, 22 Jun 2025 17:45:36 +0900 Subject: [PATCH] sys.setswitchinterval --- Lib/test/test_functools.py | 2 -- Lib/test/test_sys.py | 2 -- Lib/test/test_syslog.py | 2 -- Lib/test/test_threading.py | 2 -- vm/src/stdlib/sys.rs | 19 +++++++++++++++++++ vm/src/vm/mod.rs | 2 ++ 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index fb2dcf7a51..f295953ffd 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -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 diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 590d9c8df8..12e683c94d 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -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") diff --git a/Lib/test/test_syslog.py b/Lib/test/test_syslog.py index 96945bfd8b..b378d62e5c 100644 --- a/Lib/test/test_syslog.py +++ b/Lib/test/test_syslog.py @@ -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() diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 92ff3dc380..65972389f4 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -412,8 +412,6 @@ def child(): b"Woke up, sleep function is: ") 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. diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index c66968c7a1..111198f758 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -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)] diff --git a/vm/src/vm/mod.rs b/vm/src/vm/mod.rs index 08fbff94f9..a32fbe4b62 100644 --- a/vm/src/vm/mod.rs +++ b/vm/src/vm/mod.rs @@ -106,6 +106,7 @@ pub struct PyGlobalState { pub after_forkers_child: PyMutex>, pub after_forkers_parent: PyMutex>, pub int_max_str_digits: AtomicCell, + pub switch_interval: AtomicCell, } pub fn process_hash_secret_seed() -> u32 { @@ -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),