[New-bugs-announce] [issue26669] time.localtime(float("NaN")) does not raise a ValueError on all platforms

Gregory P. Smith report at bugs.python.org
Wed Mar 30 03:17:23 EDT 2016

New submission from Gregory P. Smith:

time.localtime(float("NaN")) raises a ValueError on x86_64 using the few compilers I have tested it with. (this makes sense)

>>> time.localtime(float("NaN"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: (75, 'Value too large for defined data type')

On an arm and arm64 system, it does not and treats NaN as 0. (nonsense!)

>>> time.localtime(float("NaN"))
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

The root of this problem appears to be the (potentially questionable? I'll ask a C compiler person...) code in Python/pytime.c's (3.x) Modules/timemodule.c's (2.7) double to time_t conversion function.

I'm not sure what it does is supposed to be well defined behavior with NaN...

The easy fix is to add:

#include <math.h>


add || isnan(x) || isinf(x) to the check that raises a ValueError in

https://hg.python.org/cpython/file/4c903ceeb4d1/Python/pytime.c#l149 (3.x)
https://hg.python.org/cpython/file/2.7/Modules/timemodule.c#l102 (2.7)

Along with a relevant assertRaises(ValueError) unittest for NaN, inf and -inf in test_time.py.

components: Extension Modules, Interpreter Core
messages: 262653
nosy: gregory.p.smith
priority: normal
severity: normal
status: open
title: time.localtime(float("NaN")) does not raise a ValueError on all platforms
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list