Skip to content

Commit c6ac24c

Browse files
committed
Clean up cfgs, handle 32bit timespec creation, skip tests for windows.
1 parent ee77f3c commit c6ac24c

File tree

2 files changed

+64
-138
lines changed

2 files changed

+64
-138
lines changed

Lib/test/test_time.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ def test_data_attributes(self):
6060
time.timezone
6161
time.tzname
6262

63+
# TODO: RUSTPYTHON
64+
@unittest.skipIf(sys.platform == "win32", "Implement get_clock_info for Windows.")
6365
def test_time(self):
6466
time.time()
6567
info = time.get_clock_info('time')
6668
self.assertFalse(info.monotonic)
6769
self.assertTrue(info.adjustable)
6870

71+
# TODO: RUSTPYTHON
72+
@unittest.skipIf(sys.platform == "win32", "Implement monotonic_ns for Windows.")
6973
def test_time_ns_type(self):
7074
def check_ns(sec, ns):
7175
self.assertIsInstance(ns, int)
@@ -472,6 +476,8 @@ def test_mktime_error(self):
472476
pass
473477
self.assertEqual(time.strftime('%Z', tt), tzname)
474478

479+
# TODO: RUSTPYTHON
480+
@unittest.skipIf(sys.platform == "win32", "Implement get_clock_info for Windows.")
475481
def test_monotonic(self):
476482
# monotonic() should not go backward
477483
times = [time.monotonic() for n in range(100)]
@@ -498,6 +504,8 @@ def test_monotonic(self):
498504
def test_perf_counter(self):
499505
time.perf_counter()
500506

507+
# TODO: RUSTPYTHON
508+
@unittest.skipIf(sys.platform == "win32", "Implement get_clock_info for Windows.")
501509
def test_process_time(self):
502510
# process_time() should not include time spend during a sleep
503511
start = time.process_time()
@@ -511,6 +519,8 @@ def test_process_time(self):
511519
self.assertTrue(info.monotonic)
512520
self.assertFalse(info.adjustable)
513521

522+
# TODO: RUSTPYTHON
523+
@unittest.skipIf(sys.platform == "win32", "Implement get_clock_info for Windows.")
514524
def test_thread_time(self):
515525
if not hasattr(time, 'thread_time'):
516526
if sys.platform.startswith(('linux', 'win')):
@@ -567,6 +577,8 @@ def test_localtime_failure(self):
567577
self.assertRaises(ValueError, time.localtime, float("nan"))
568578
self.assertRaises(ValueError, time.ctime, float("nan"))
569579

580+
# TODO: RUSTPYTHON
581+
@unittest.skipIf(sys.platform == "win32", "Implement get_clock_info for Windows.")
570582
def test_get_clock_info(self):
571583
clocks = ['monotonic', 'perf_counter', 'process_time', 'time']
572584

vm/src/stdlib/time.rs

Lines changed: 52 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -90,52 +90,28 @@ mod time {
9090
Ok(Date::now() / 1000.0)
9191
}
9292

93-
#[cfg(not(any(
94-
target_os = "macos",
95-
target_os = "android",
96-
target_os = "dragonfly",
97-
target_os = "freebsd",
98-
target_os = "linux",
99-
)))]
93+
#[cfg(any(windows, target_os = "wasi"))]
10094
#[pyfunction]
10195
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
10296
// TODO: implement proper monotonic time for other platforms.
10397
Ok(duration_since_system_now(vm)?.as_secs_f64())
10498
}
10599

106-
#[cfg(not(any(
107-
target_os = "macos",
108-
target_os = "android",
109-
target_os = "dragonfly",
110-
target_os = "freebsd",
111-
target_os = "linux",
112-
)))]
100+
#[cfg(any(windows, target_os = "wasi"))]
113101
#[pyfunction]
114102
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
115103
// TODO: implement proper monotonic time for other platforms.
116104
Ok(duration_since_system_now(vm)?.as_nanos())
117105
}
118106

119-
#[cfg(not(any(
120-
target_os = "macos",
121-
target_os = "android",
122-
target_os = "dragonfly",
123-
target_os = "freebsd",
124-
target_os = "linux",
125-
)))]
107+
#[cfg(any(windows, target_os = "wasi"))]
126108
#[pyfunction]
127109
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
128110
// TODO: implement proper monotonic time for other platforms.
129111
Ok(duration_since_system_now(vm)?.as_secs_f64())
130112
}
131113

132-
#[cfg(not(any(
133-
target_os = "macos",
134-
target_os = "android",
135-
target_os = "dragonfly",
136-
target_os = "freebsd",
137-
target_os = "linux",
138-
)))]
114+
#[cfg(any(windows, target_os = "wasi"))]
139115
#[pyfunction]
140116
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
141117
// TODO: implement proper monotonic time for other platforms.
@@ -251,8 +227,8 @@ mod time {
251227
target_os = "dragonfly",
252228
target_os = "freebsd",
253229
target_os = "linux",
254-
target_os = "openbsd",
255-
target_os = "solaris",
230+
target_os = "fuchsia",
231+
target_os = "emscripten",
256232
)))]
257233
fn get_thread_time(vm: &VirtualMachine) -> PyResult<std::time::Duration> {
258234
Err(vm.new_not_implemented_error("thread time unsupported in this system".to_owned()))
@@ -420,8 +396,10 @@ mod time {
420396
mod unix {
421397
#[allow(unused_imports)]
422398
use super::{SEC_TO_NS, US_TO_NS};
399+
#[cfg_attr(target_os = "macos", allow(unused_imports))]
423400
use crate::{
424401
builtins::{try_bigint_to_f64, PyFloat, PyIntRef, PyNamespace, PyStrRef},
402+
stdlib::os,
425403
utils::Either,
426404
PyRef, PyResult, VirtualMachine,
427405
};
@@ -430,47 +408,42 @@ mod unix {
430408
#[cfg(target_os = "solaris")]
431409
#[pyattr]
432410
use libc::CLOCK_HIGHRES;
433-
#[cfg(not(target_os = "redox"))]
411+
#[cfg(not(any(
412+
target_os = "illumos",
413+
target_os = "netbsd",
414+
target_os = "solaris",
415+
target_os = "openbsd",
416+
)))]
417+
#[pyattr]
434418
use libc::CLOCK_PROCESS_CPUTIME_ID;
419+
#[cfg(not(any(
420+
target_os = "illumos",
421+
target_os = "netbsd",
422+
target_os = "solaris",
423+
target_os = "openbsd",
424+
target_os = "redox",
425+
)))]
426+
#[pyattr]
427+
use libc::CLOCK_THREAD_CPUTIME_ID;
435428
#[cfg(target_os = "linux")]
436429
#[pyattr]
437430
use libc::{CLOCK_BOOTTIME, CLOCK_MONOTONIC_RAW, CLOCK_TAI};
438431
#[pyattr]
439-
use libc::{CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_THREAD_CPUTIME_ID};
440-
#[cfg(target_os = "redox")]
441-
// TODO: will be upstreamed to libc sometime soon
442-
const CLOCK_PROCESS_CPUTIME_ID: libc::clockid_t = 2;
443-
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
432+
use libc::{CLOCK_MONOTONIC, CLOCK_REALTIME};
433+
#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "dragonfly"))]
444434
#[pyattr]
445435
use libc::{CLOCK_PROF, CLOCK_UPTIME};
446436

447-
#[cfg(any(
448-
target_os = "macos",
449-
target_os = "android",
450-
target_os = "dragonfly",
451-
target_os = "freebsd",
452-
target_os = "linux",
453-
))]
454437
fn get_clock_time(clk_id: PyIntRef, vm: &VirtualMachine) -> PyResult<Duration> {
455438
let mut timespec = std::mem::MaybeUninit::uninit();
456439
let ts: libc::timespec = unsafe {
457440
if libc::clock_gettime(clk_id.try_to_primitive(vm)?, timespec.as_mut_ptr()) == -1 {
458-
return Err(vm.new_os_error("Invalid argument".to_owned()));
441+
return Err(os::errno_err(vm));
459442
}
460443
timespec.assume_init()
461444
};
462445
Ok(Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32))
463446
}
464-
#[cfg(not(any(
465-
target_os = "macos",
466-
target_os = "android",
467-
target_os = "dragonfly",
468-
target_os = "freebsd",
469-
target_os = "linux",
470-
)))]
471-
fn get_clock_time(_clk_id: PyIntRef, vm: &VirtualMachine) -> PyResult<Duration> {
472-
Err(vm.new_not_implemented_error("clock_gettime unsupported on this system".to_owned()))
473-
}
474447

475448
#[pyfunction]
476449
fn clock_gettime(clk_id: PyIntRef, vm: &VirtualMachine) -> PyResult<f64> {
@@ -482,66 +455,33 @@ mod unix {
482455
get_clock_time(clk_id, vm).map(|d| d.as_nanos())
483456
}
484457

485-
#[cfg(any(
486-
target_os = "macos",
487-
target_os = "android",
488-
target_os = "dragonfly",
489-
target_os = "freebsd",
490-
target_os = "linux",
491-
))]
458+
#[cfg(not(target_os = "redox"))]
492459
#[pyfunction]
493460
fn clock_getres(clk_id: PyIntRef, vm: &VirtualMachine) -> PyResult<f64> {
494461
let mut timespec = std::mem::MaybeUninit::uninit();
495462
let ts: libc::timespec = unsafe {
496463
if libc::clock_getres(clk_id.try_to_primitive(vm)?, timespec.as_mut_ptr()) == -1 {
497-
return Err(vm.new_os_error("Invalid argument".to_owned()));
464+
return Err(os::errno_err(vm));
498465
}
499466
timespec.assume_init()
500467
};
501468
Ok(Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32).as_secs_f64())
502469
}
503470

504-
#[cfg(not(any(
505-
target_os = "macos",
506-
target_os = "android",
507-
target_os = "dragonfly",
508-
target_os = "freebsd",
509-
target_os = "linux",
510-
)))]
511-
#[pyfunction]
512-
fn clock_getres(_clk_id: PyIntRef, vm: &VirtualMachine) -> PyResult<f64> {
513-
Err(vm.new_not_implemented_error("clock_getres unsupported on this system".to_owned()))
514-
}
515-
516-
#[cfg(any(
517-
target_os = "macos",
518-
target_os = "android",
519-
target_os = "dragonfly",
520-
target_os = "freebsd",
521-
target_os = "linux",
522-
))]
471+
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
523472
fn set_clock_time(
524473
clk_id: PyIntRef,
525474
timespec: libc::timespec,
526475
vm: &VirtualMachine,
527476
) -> PyResult<()> {
528477
let res = unsafe { libc::clock_settime(clk_id.try_to_primitive(vm)?, &timespec) };
529478
if res == -1 {
530-
return Err(vm.new_os_error("Invalid argument".to_owned()));
479+
return Err(os::errno_err(vm));
531480
}
532481
Ok(())
533482
}
534-
#[cfg(not(any(
535-
target_os = "macos",
536-
target_os = "android",
537-
target_os = "dragonfly",
538-
target_os = "freebsd",
539-
target_os = "linux",
540-
)))]
541-
fn set_clock_time(_clk_id: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
542-
Err(vm.new_not_implemented_error("clock_settime unsupported on this system".to_owned()))
543-
}
544483

484+
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
545485
#[pyfunction]
546486
fn clock_settime(
547487
clk_id: PyIntRef,
@@ -560,21 +500,25 @@ mod unix {
560500
set_clock_time(clk_id, ts, vm)
561501
}
562502

503+
#[cfg(not(any(target_os = "macos", target_os = "redox")))]
563504
#[pyfunction]
564505
fn clock_settime_ns(clk_id: PyIntRef, time: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
565-
let time: i64 = time.try_to_primitive(vm)?;
506+
let time: libc::time_t = time.try_to_primitive(vm)?;
566507
let ts = libc::timespec {
567-
tv_sec: time / SEC_TO_NS as libc::time_t,
568-
tv_nsec: time.rem_euclid(SEC_TO_NS) as _,
508+
tv_sec: time / (SEC_TO_NS as libc::time_t),
509+
tv_nsec: time.rem_euclid(SEC_TO_NS as libc::time_t) as _,
569510
};
570511
set_clock_time(clk_id, ts, vm)
571512
}
572513

514+
// Requires all CLOCK constants available and clock_getres
573515
#[cfg(any(
574516
target_os = "macos",
575517
target_os = "android",
576518
target_os = "dragonfly",
577519
target_os = "freebsd",
520+
target_os = "fuchsia",
521+
target_os = "emscripten",
578522
target_os = "linux",
579523
))]
580524
#[pyfunction]
@@ -620,56 +564,30 @@ mod unix {
620564
target_os = "android",
621565
target_os = "dragonfly",
622566
target_os = "freebsd",
567+
target_os = "fuchsia",
568+
target_os = "emscripten",
623569
target_os = "linux",
624570
)))]
625571
#[pyfunction]
626572
fn get_clock_info(_name: PyStrRef, vm: &VirtualMachine) -> PyResult<PyNamespace> {
627573
Err(vm.new_not_implemented_error("get_clock_info unsupported on this system".to_owned()))
628574
}
629575

630-
#[cfg(any(
631-
target_os = "macos",
632-
target_os = "android",
633-
target_os = "dragonfly",
634-
target_os = "freebsd",
635-
target_os = "linux",
636-
))]
637576
#[pyfunction]
638577
fn monotonic(vm: &VirtualMachine) -> PyResult<f64> {
639578
clock_gettime(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
640579
}
641580

642-
#[cfg(any(
643-
target_os = "macos",
644-
target_os = "android",
645-
target_os = "dragonfly",
646-
target_os = "freebsd",
647-
target_os = "linux",
648-
))]
649581
#[pyfunction]
650582
fn monotonic_ns(vm: &VirtualMachine) -> PyResult<u128> {
651583
clock_gettime_ns(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
652584
}
653585

654-
#[cfg(any(
655-
target_os = "macos",
656-
target_os = "android",
657-
target_os = "dragonfly",
658-
target_os = "freebsd",
659-
target_os = "linux",
660-
))]
661586
#[pyfunction]
662587
fn perf_counter(vm: &VirtualMachine) -> PyResult<f64> {
663588
clock_gettime(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
664589
}
665590

666-
#[cfg(any(
667-
target_os = "macos",
668-
target_os = "android",
669-
target_os = "dragonfly",
670-
target_os = "freebsd",
671-
target_os = "linux",
672-
))]
673591
#[pyfunction]
674592
fn perf_counter_ns(vm: &VirtualMachine) -> PyResult<u128> {
675593
clock_gettime_ns(vm.ctx.new_int(CLOCK_MONOTONIC), vm)
@@ -693,14 +611,12 @@ mod unix {
693611
Ok(())
694612
}
695613

696-
#[cfg(any(
697-
target_os = "macos",
698-
target_os = "android",
699-
target_os = "dragonfly",
700-
target_os = "freebsd",
701-
target_os = "linux",
614+
#[cfg(not(any(
615+
target_os = "illumos",
616+
target_os = "netbsd",
702617
target_os = "openbsd",
703-
))]
618+
target_os = "redox"
619+
)))]
704620
pub(super) fn get_thread_time(vm: &VirtualMachine) -> PyResult<Duration> {
705621
let time: libc::timespec = unsafe {
706622
let mut time = std::mem::MaybeUninit::uninit();
@@ -717,14 +633,12 @@ mod unix {
717633
Ok(Duration::from_nanos(unsafe { libc::gethrvtime() }))
718634
}
719635

720-
#[cfg(any(
721-
target_os = "macos",
722-
target_os = "android",
723-
target_os = "dragonfly",
724-
target_os = "freebsd",
725-
target_os = "linux",
726-
target_os = "redox",
727-
))]
636+
#[cfg(not(any(
637+
target_os = "illumos",
638+
target_os = "netbsd",
639+
target_os = "solaris",
640+
target_os = "openbsd",
641+
)))]
728642
pub(super) fn get_process_time(vm: &VirtualMachine) -> PyResult<Duration> {
729643
let time: libc::timespec = unsafe {
730644
let mut time = std::mem::MaybeUninit::uninit();

0 commit comments

Comments
 (0)