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
10 changes: 8 additions & 2 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,9 +1141,15 @@ def test_FromSecondsObject(self):
_PyTime_FromSecondsObject,
lambda secs: secs * SEC_TO_NS)

def seconds_to_pytime(seconds):
floatpart, intpart = math.modf(seconds)
t = int(intpart) * 10 ** 9
ns = self.decimal_round(floatpart * float(10**9))
return t + ns

self.check_float_rounding(
_PyTime_FromSecondsObject,
lambda ns: self.decimal_round(ns * SEC_TO_NS))
seconds_to_pytime)

self.check_ratio_rounding(
_PyTime_FromSecondsObject,
Expand Down Expand Up @@ -1217,7 +1223,7 @@ def seconds_filter(secs):
@unittest.skipUnless(hasattr(_testinternalcapi, '_PyTime_AsTimespec'),
'need _testinternalcapi._PyTime_AsTimespec')
def test_AsTimespec(self):
# Test_PyTime_AsTimespec()
# Test _PyTime_AsTimespec()
from _testinternalcapi import _PyTime_AsTimespec

def timespec_converter(ns):
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
}

// Test _PyTime_AsTimeval_clamup()
// Test _PyTime_AsTimeval_clamp()
static PyObject *
test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
{
Expand Down
42 changes: 32 additions & 10 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,6 @@ pytime_double_to_time_t(time_t *sec, double value)
assumption that time_t is a two's complement integer type with no trap
representation, and that `PY_TIME_T_MIN` is within the representable
range of a C double.
Note: we want the `if` condition below to be true for NaNs; therefore,
resist any temptation to simplify by applying De Morgan's laws.
*/
const double time_t_min = (double)PY_TIME_T_MIN;
if (!(time_t_min <= value && value < -time_t_min)) {
Expand All @@ -442,7 +439,7 @@ pytime_double_to_time_t(time_t *sec, double value)


// Convert C double to C PyTime_t.
// Set *sec and return 0 on success.
// Set *tp and return 0 on success.
// Set OverflowError and return -1 on overflow.
//
// See also pytime_double_to_time_t().
Expand Down Expand Up @@ -917,23 +914,48 @@ pytime_from_pyfloat(PyTime_t *tp, PyObject *obj, _PyTime_round_t round,
{
double value = PyFloat_AsDouble(obj);
if (value == -1 && PyErr_Occurred()) {
*tp = 0;
return -1;
}

if (isnan(value)) {
PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)");
*tp = 0;
return -1;
}

double intpart;
/* volatile avoids optimization changing how numbers are rounded */
volatile double d;
volatile double floatpart;

/* convert to a number of nanoseconds */
d = value;
d *= (double)unit_to_ns;
d = pytime_round(d, round);
// Multiply by unit_to_ns using integers (PyTime_t), instead of double
// to avoid loss of precision.
floatpart = modf(value, &intpart);

@StanFromIreland StanFromIreland Dec 4, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This now reveals floating point errors, which were hidden before...

>>> from _testinternalcapi import _PyTime_FromSecondsObject
>>> _PyTime_FromSecondsObject(1.1, 3)
1100000001

This is also exposed by the socket timeouts which call PyTime_AsSecondsDouble:

>>> import socket
>>> s = socket.socket()
>>> s.settimeout(1.1)
>>> s.gettimeout()
1.100000001

PyTime_t t;
if (pytime_from_double_cast(&t, intpart) < 0) {
*tp = 0;
return -1; // overflow
}
if (pytime_mul(&t, unit_to_ns) < 0) {
pytime_overflow();
return -1;
}

floatpart *= unit_to_ns;
floatpart = pytime_round(floatpart, round);
// It's safe to cast floatpart to PyTime_t, since its value is in
// the range [-unit_to_ns; unit_to_ns], where unit_to_ns maximum
// is SEC_TO_NS (for _PyTime_FromSecondsObject()).
assert(-unit_to_ns <= floatpart && floatpart <= unit_to_ns);

PyTime_t ns = (PyTime_t)floatpart;
if (pytime_add(&t, ns) < 0) {
pytime_overflow();
return -1;
}

return pytime_from_double_cast(tp, d);
*tp = t;
return 0;
}


Expand Down
Loading