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
14 changes: 10 additions & 4 deletions crates/host_env/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,24 @@ pub fn current_time_t() -> TimeT {

#[cfg(windows)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn gmtime_from_timestamp(when: TimeT) -> Option<libc::tm> {
pub fn gmtime_from_timestamp(when: TimeT) -> std::io::Result<libc::tm> {
let mut out = core::mem::MaybeUninit::<libc::tm>::uninit();
let err = unsafe { _gmtime64_s(out.as_mut_ptr(), &when) };
(err == 0).then(|| unsafe { out.assume_init() })
if err != 0 {
return Err(crate::os::io_error_from_errno(err));
}
Ok(unsafe { out.assume_init() })
}

#[cfg(windows)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn localtime_from_timestamp(when: TimeT) -> Option<libc::tm> {
pub fn localtime_from_timestamp(when: TimeT) -> std::io::Result<libc::tm> {
let mut out = core::mem::MaybeUninit::<libc::tm>::uninit();
let err = unsafe { _localtime64_s(out.as_mut_ptr(), &when) };
(err == 0).then(|| unsafe { out.assume_init() })
if err != 0 {
return Err(crate::os::io_error_from_errno(err));
}
Ok(unsafe { out.assume_init() })
}

#[cfg(windows)]
Expand Down
5 changes: 5 additions & 0 deletions crates/stdlib/src/_datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub(crate) use _datetime::{PyTzInfo, datetime_type, module_def, timedelta_from_s

#[pymodule]
mod _datetime {
#[cfg(windows)]
use crate::vm::convert::ToPyException;
use crate::vm::{
AsObject, Py, PyObject, PyObjectCell, PyObjectRef, PyPayload, PyRef, PyResult,
VirtualMachine,
Expand Down Expand Up @@ -1825,6 +1827,9 @@ mod _datetime {
} else {
rustpython_host_env::time::gmtime_from_timestamp(t_c)
};
#[cfg(windows)]
let tm = tm.map_err(|error| error.to_pyexception(vm))?;
#[cfg(unix)]
let Some(tm) = tm else {
return Err(vm.new_last_errno_error());
};
Expand Down
13 changes: 8 additions & 5 deletions crates/vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,9 @@ mod decl {
}
let secs = float.floor();
#[cfg_attr(target_env = "musl", allow(deprecated))]
if secs < libc::time_t::MIN as f64 || secs > libc::time_t::MAX as f64 {
let minimum = libc::time_t::MIN as f64;
// The signed maximum rounds up when represented by f64.
if !(minimum..-minimum).contains(&secs) {
return Err(vm.new_overflow_error("timestamp out of range for platform time_t"));
}
#[cfg_attr(target_env = "musl", allow(deprecated))]
Expand Down Expand Up @@ -1248,6 +1250,7 @@ mod platform {
use crate::{
PyRef, PyResult, VirtualMachine,
builtins::{PyNamespace, PyUtf8StrRef},
convert::ToPyException,
};
use core::time::Duration;
use rustpython_host_env::time as host_time;
Expand Down Expand Up @@ -1281,17 +1284,17 @@ mod platform {
when: host_time::TimeT,
vm: &VirtualMachine,
) -> PyResult<StructTimeData> {
let tm = host_time::gmtime_from_timestamp(when)
.ok_or_else(|| vm.new_overflow_error("timestamp out of range for platform time_t"))?;
let tm =
host_time::gmtime_from_timestamp(when).map_err(|error| error.to_pyexception(vm))?;
Ok(struct_time_from_tm(vm, tm, "UTC", 0))
}

pub(super) fn localtime_from_timestamp(
when: host_time::TimeT,
vm: &VirtualMachine,
) -> PyResult<StructTimeData> {
let tm = host_time::localtime_from_timestamp(when)
.ok_or_else(|| vm.new_overflow_error("timestamp out of range for platform time_t"))?;
let tm =
host_time::localtime_from_timestamp(when).map_err(|error| error.to_pyexception(vm))?;

// Get timezone info from Windows API
let info = get_tz_info();
Expand Down
6 changes: 6 additions & 0 deletions extra_tests/snippets/stdlib_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3949,6 +3949,12 @@ def test_timestamp_errno():
assert error.errno == errno.EINVAL
else:
raise AssertionError("Windows localtime accepted a negative timestamp")
try:
datetime_module.datetime.fromtimestamp(-86400, datetime_module.timezone.utc)
except OSError as error:
assert error.errno == errno.EINVAL
else:
raise AssertionError("Windows gmtime accepted an out-of-range timestamp")


test_astimezone_subclass_state()
Expand Down
82 changes: 82 additions & 0 deletions extra_tests/snippets/stdlib_time.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,88 @@
import sys
import time

if sys.platform == "win32":
# Record the native runtime before evaluating the range regression below.
print("Windows time runtime:", sys.version, sys.implementation, sys.flags)
print("Windows time word size:", sys.maxsize)
if hasattr(sys, "getwindowsversion"):
print("Windows version:", sys.getwindowsversion())
try:
import sysconfig

build_keys = (
"Py_DEBUG",
"Py_GIL_DISABLED",
"Py_ENABLE_SHARED",
"SIZEOF_TIME_T",
"CC",
"CFLAGS",
)
print(
"Windows time build:",
dict(zip(build_keys, sysconfig.get_config_vars(*build_keys))),
)
except Exception as error:
print("Windows time build unavailable:", type(error).__name__, str(error))
for converter in (time.gmtime, time.localtime, time.ctime):
for timestamp in (
0,
-1,
-43200,
-43201,
-86400,
2**40,
2**63 - 1,
-(2**63),
-float(2**63),
float(2**63),
):
try:
result = converter(timestamp)
except Exception as error:
print(
"Windows time result:",
converter.__name__,
repr(timestamp),
type(error).__name__,
str(error),
getattr(error, "errno", None),
getattr(error, "winerror", None),
)
else:
print(
"Windows time result:",
converter.__name__,
repr(timestamp),
"success",
repr(result),
)

if sys.platform != "wasi":
for converter in (time.gmtime, time.localtime, time.ctime):
# 2**63 is outside signed time_t even though its maximum rounds up to
# that value when represented as a float.
try:
converter(float(2**63))
except OverflowError:
pass
else:
raise AssertionError("out-of-range timestamp did not raise OverflowError")

if sys.platform == "win32":
import errno

for converter in (time.gmtime, time.localtime, time.ctime):
# Python 3.14 preserves UCRT conversion failures as OSError. Use a
# negative timestamp below gmtime's near-epoch allowance in UCRT.
for timestamp in (-86400, 2**40, 2**63 - 1, -(2**63), -float(2**63)):
try:
converter(timestamp)
except OSError as error:
assert error.errno == errno.EINVAL, (converter, timestamp, error)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove prohibited test logic additions

This adds new assertion/control flow and timestamp data to extra_tests/snippets/stdlib_time.py (and the same commit adds another case to stdlib_datetime.py), but the root policy explicitly forbids changes to test assertions, logic, or data and limits accepted test-file edits to expected-failure decorator changes. Remove these additions (or revise the repository policy first) rather than landing them in the test files.

AGENTS.md reference: AGENTS.md:L273-L279

Useful? React with 👍 / 👎.

else:
raise AssertionError("UCRT timestamp failure did not raise OSError")

x = time.gmtime(1000)

assert x.tm_year == 1970
Expand Down
Loading