bpo-26669: Fix nan arg value error in pytime.c by hahnlee · Pull Request #3085 · python/cpython

@Haypo I'm really sorry for the delay in writing the code.

Summary

System: Ubuntu 16.04 x86
pytime_object_to_timeval got error.
If the result is the same as expected, I will replace the passed test code with the changed _PyTime_FromSecondsObject and _PyTime_ObjectToDenominator.

  • pytime_object_to_time_t: pass
  • pytime_object_to_timeval : error assert(0 <= *usec && *usec < SEC_TO_US);
  • pytime_object_to_timespec: error assert(0 <= *nsec && *nsec < SEC_TO_NS);
  • PyTime_FromSecondsObject: pass (When add some C code)
  • PyTime_FromSeconds: TypeError: integer argument expected, got float
  • PyTime_FromSecondsObject: TypeError: integer argument expected, got float

Result

I tested the following code in _check_rounding.

# test nan
for time_rnd, _ in ROUNDING_MODES:
    with self.assertRaises(ValueError):
        pytime_converter(float('nan'), time_rnd)

Here are the results:

Python/pytime.c:116: _PyTime_DoubleToDenominator: Assertion `0.0 <= floatpart && floatpart < denominator' failed.

...so I tested one by one.
pytime_object_to_time_t: pass
pytime_object_to_timeval: error, pytime.c:116
pytime_object_to_timespec: error, pytime.c:116

PyTime_FromSeconds: error TypeError: integer argument expected, got float (did not use time_rnd)
PyTime_AsSecondsDouble: has same error PyTime_FromSecondsObject (did not use time_rnd)

And PyTime_FromSecondsObject: did not raise
_PyTime_FromSecondsObject call _PyTime_FromObject and _PyTime_FromObject has same logic at _PyTime_ObjectToTime_t (check float but do not check nan)
So I changed _PyTime_FromObject and passed test for _PyTime_FromSecondsObject.

In case pytime_object_to_timeval and pytime_object_to_timespec they call _PyTime_ObjectToDenominator and _PyTime_ObjectToDenominator has PyFloat_AsDouble too

I add Py_IS_NAN(d) at _PyTime_ObjectToDenominator

but pytime_object_to_timeval and pytime_object_to_timespec got assert error
it mean in case _PyTime_ObjectToDenominator, *obj float('nan') has problem in usec or nsec

i think PyTime_FromSeconds and PyTime_AsSecondsDouble seem reasonable.
but i'm not sure pytime_object_to_timeval and pytime_object_to_timespec has reasonable error.

If the result is the same as expected, I will replace the passed test code with the changed _PyTime_FromSecondsObject and _PyTime_ObjectToDenominator.