Skip to content

time.localtime(float("NaN")) does not raise a ValueError on all platforms #70856

Closed

Description

@gpshead
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']

    Activity

    1. gpshead commented on Mar 30, 2016

      @gpshead
      MemberAuthor

      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.

    2. self-assigned this
      on Sep 14, 2016
    3. mdickinson commented on Aug 14, 2017

      @mdickinson
      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.

    4. vstinner commented on Aug 31, 2017

      @vstinner
      Member

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

    5. vstinner commented on Sep 8, 2017

      @vstinner
      Member

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

    6. vstinner commented on Sep 8, 2017

      @vstinner
      Member

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

    7. Mariatta commented on Sep 9, 2017

      @Mariatta
      Member

      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

    8. Mariatta commented on Sep 9, 2017

      @Mariatta
      Member

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

    9. vstinner commented on Jan 10, 2019

      @vstinner
      Member

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

    10. isidentical commented on Oct 29, 2019

      @isidentical
      SponsorMember

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

    11. vstinner commented on Oct 29, 2019

      @vstinner
      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.

    12. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    extension-modulesC modules in the Modules dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions