Skip to content
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

Closed
gpshead opened this issue Mar 30, 2016 · 10 comments
Closed
Assignees
Labels
extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@gpshead
Copy link
Member

gpshead commented Mar 30, 2016

BPO 26669
Nosy @gpshead, @mdickinson, @abalkin, @vstinner, @Mariatta, @isidentical
PRs
  • bpo-26669: Fix nan arg value error in pytime.c #3085
  • [3.6] bpo-26669: Fix nan arg value error in pytime.c (GH-3085) #3467
  • bpo-26669: Reject float infinity in time functions #11507
  • bpo-26669: Reject float infinity in time functions #11507
  • bpo-26669: Reject float infinity in time functions #11507
  • bpo-26669: Reject float infinity in time functions #11507
  • 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:

    assignee = 'https://github.com/abalkin'
    closed_at = <Date 2019-10-29.11:12:36.394>
    created_at = <Date 2016-03-30.07:17:23.433>
    labels = ['extension-modules', 'interpreter-core', 'type-bug']
    title = 'time.localtime(float("NaN")) does not raise a ValueError on all platforms'
    updated_at = <Date 2019-10-29.11:12:36.392>
    user = 'https://github.com/gpshead'

    bugs.python.org fields:

    activity = <Date 2019-10-29.11:12:36.392>
    actor = 'vstinner'
    assignee = 'belopolsky'
    closed = True
    closed_date = <Date 2019-10-29.11:12:36.394>
    closer = 'vstinner'
    components = ['Extension Modules', 'Interpreter Core']
    creation = <Date 2016-03-30.07:17:23.433>
    creator = 'gregory.p.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26669
    keywords = ['patch']
    message_count = 10.0
    messages = ['262653', '300237', '301041', '301754', '301755', '301760', '301761', '333403', '355640', '355644']
    nosy_count = 6.0
    nosy_names = ['gregory.p.smith', 'mark.dickinson', 'belopolsky', 'vstinner', 'Mariatta', 'BTaskaya']
    pr_nums = ['3085', '3467', '11507', '11507', '11507', '11507']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26669'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5', 'Python 3.6']

    @gpshead
    Copy link
    Member Author

    gpshead commented Mar 30, 2016

    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)
    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.

    @gpshead gpshead added extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 30, 2016
    @abalkin abalkin self-assigned this Sep 14, 2016
    @abalkin abalkin added the type-bug An unexpected behavior, bug, or error label Sep 14, 2016
    @mdickinson
    Copy link
    Member

    potentially questionable? I'll ask a C compiler person...

    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 time_t type, so the questionability extends beyond just the NaN handling.

    @vstinner
    Copy link
    Member

    I like PR 3085 to raise explicitly a ValueError with an helpful error message.

    @vstinner
    Copy link
    Member

    vstinner commented Sep 8, 2017

    New changeset 829dacc by Victor Stinner (Han Lee) in branch 'master':
    bpo-26669: Fix nan arg value error in pytime.c (bpo-3085)
    829dacc

    @vstinner
    Copy link
    Member

    vstinner commented Sep 8, 2017

    For Python 2.7, you have at least to fix these 2 functions: parse_time_double_args(), _PyTime_DoubleToTimet().

    @Mariatta
    Copy link
    Member

    Mariatta commented Sep 9, 2017

    New changeset a4baf1c by Mariatta (Miss Islington (bot)) in branch '3.6':
    [3.6] bpo-26669: Fix nan arg value error in pytime.c (GH-3085) (GH-3467)
    a4baf1c

    @Mariatta
    Copy link
    Member

    Mariatta commented Sep 9, 2017

    This has been backported to 3.6.
    Is backport to 2.7 needed? If not we'll close the issue.

    @vstinner
    Copy link
    Member

    I wrote PR 11507 to raise ValueError rather than OverflowError for -inf and +inf.

    @isidentical
    Copy link
    Sponsor Member

    Victor's PR 11507 is closed, what actions are going to be taken next? Close the issue as Mariatta said?

    @vstinner
    Copy link
    Member

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants