New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time.localtime(float("NaN")) does not raise a ValueError on all platforms #70856
Comments
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> and 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) Along with a relevant assertRaises(ValueError) unittest for NaN, inf and -inf in test_time.py. |
Questionable indeed. Attempting to cast a NaN to an integer type results in undefined behaviour. Unfortunately, so does attempting to cast any double value that's outside the range represented by the |
I like PR 3085 to raise explicitly a ValueError with an helpful error message. |
For Python 2.7, you have at least to fix these 2 functions: parse_time_double_args(), _PyTime_DoubleToTimet(). |
This has been backported to 3.6. |
I wrote PR 11507 to raise ValueError rather than OverflowError for -inf and +inf. |
Victor's PR 11507 is closed, what actions are going to be taken next? Close the issue as Mariatta said? |
Python 2 users have this bug since 2010, I think that it's fine to not fix it. Python 2 support ends at the end of the year. I close the issue. The issue has been fixed in Python 3.6 and newer. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: