Skip to content

Conversation

@hahnlee
Copy link
Contributor

@hahnlee hahnlee commented Aug 13, 2017

  • Make value error message for nan value
    Previously, it did not show error message in arm system
  • Add test case for nan value

https://bugs.python.org/issue26669

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@hahnlee hahnlee changed the title bpo-26669: Fix nan input error in pytime.c bpo-26669: Fix nan arg value error in pytime.c Aug 13, 2017
@hahnlee hahnlee closed this Aug 13, 2017
@hahnlee hahnlee reopened this Aug 13, 2017
Python/pytime.c Outdated

d = PyFloat_AsDouble(obj);
if (isnan(d)) {
PyErr_SetString(PyExc_ValueError, "Invalid Value");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be helpful for the error message specifically to mention that the value is NaN.

@pitrou
Copy link
Member

pitrou commented Aug 29, 2017

@Haypo, do you have any comments about this?


# Issue #26669: check for localtime() failure
self.assertRaises(ValueError, time.localtime, float("nan"))
self.assertRaises(ValueError, time.ctime, float("nan"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for "functional" tests", but I also would like to see unit tests on the following _testcapi functions:

  • pytime_object_to_time_t
  • pytime_object_to_timeval
  • pytime_object_to_timespec
  • PyTime_FromSeconds
  • PyTime_FromSecondsObject
  • PyTime_AsSecondsDouble

See CPyTimeTestCase in test_time.py.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@pitrou
Copy link
Member

pitrou commented Aug 31, 2017

Hmm... Did @warsaw hack the bot?

@vstinner
Copy link
Member

vstinner commented Sep 4, 2017

@Sn0wLe0pard: Would you mind to add requested unit tests? Otherwise, I might try to modify your PR to add them myself?

@hahnlee
Copy link
Contributor Author

hahnlee commented Sep 6, 2017

@Haypo I'll add test case as soon as possible.

@hahnlee
Copy link
Contributor Author

hahnlee commented Sep 7, 2017

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

@hahnlee
Copy link
Contributor Author

hahnlee commented Sep 8, 2017

pytime_object_to_timeval also has same error on macOS sierra

@hahnlee
Copy link
Contributor Author

hahnlee commented Sep 8, 2017

Oh... in case NaN numerator is zero. I add some code for numerator zero. then it pass all test cases.

@vstinner
Copy link
Member

vstinner commented Sep 8, 2017

Ok, it now LGTM!

@vstinner vstinner merged commit 829dacc into python:master Sep 8, 2017
@miss-islington
Copy link
Contributor

🐍🍒⛏🤖 Thanks @Sn0wLe0pard for the PR, and @Haypo for merging it 🌮🎉.I'm working now to backport this PR to: 3.6.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 8, 2017
* Fix pythonGH-26669

* Modify NaN check function and error message

* Fix pytime.c when arg is nan

* fix whitespace
(cherry picked from commit 829dacc)
@bedevere-bot
Copy link

GH-3467 is a backport of this pull request to the 3.6 branch.

Mariatta pushed a commit that referenced this pull request Sep 9, 2017
* Modify NaN check function and error message
* Fix pytime.c when arg is nan
* fix whitespace
(cherry picked from commit 829dacc)
@hahnlee hahnlee deleted the fix-issue-26669 branch September 11, 2017 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants