-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-103648: Make datetime.timestamp()
raise OverflowError
when overflow on Windows
#103653
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
gh-103648: Make datetime.timestamp()
raise OverflowError
when overflow on Windows
#103653
Conversation
Hi @vstinner , could you give this PR a review as you have been working on this file recently? Thanks! |
Python/pytime.c
Outdated
errno = error; | ||
PyErr_SetFromErrno(PyExc_OSError); | ||
if (tm == NULL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it possible that tm becomes NULL after calling localtime_s()? Is it a macro which sets tm
on error? _PyTime_localtime() must not be called with tm=NULL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the case listed by localtime_s
. I did not find any existing usage that could possibly call _PyTime_localtime
with tm = NULL
, but I did not find any documentation for it either. Of course it does not quite make sense to call it with tm = NULL
, but if someone ran into the issue in the future(for example, did a _PyTime_localtime(t, tm_ptr)
and forgot to check the pointer), they would get an OverflowError
instead of an OSError
, which would be confusing.
So two things we can do:
- Leave it as it is as a sanity check - the code path is kind of dead as of now, but could benefit in the future.
- Document that
tm
can't beNULL
in the comments and/or do an assertion in the function.
I'm fine with either solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add an assertion to ensure that the function is never called with tm=NULL. A runtime check is not needed. Right, private functions are not documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks for the review!
@vstinner circling back to this, do you think this is ready to merge? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not a domain expert, but given that this has been raising an OSError
for so long, I really think we should avoid breaking code that might be catching the OSError
.
Since the docs themselves say that OSError
can be raised, I'm not sure that there's really a bug to fix here.
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 |
Yeah it was added in the docs last year #94818. Personally I believe this is an obvious overflow error, and I'm okay if this is not merged. |
I close the issue. I always had doubts about this PR, that why I didn't merge it. @brandtbucher put the right words on what I'm thinking about it.
Python is a thin wrapper to operating system functions. And yeah, sometimes the OS reports surprising errors. Usually, the solution is more to provide better documentation and guide users on how to solve their issue. Maybe there are portable Python modules which don't rely on the Python time module and so don't inherit Windows limitation, but have a portable behavior (support all datetime range on all platforms). |
Currently on Windows, if
localtime_s()
returns an error in_PyTime_localtime
, we just set an OS error. However, the docs says it should raise anOverflowError
. According to Microsoft docs,localtime_s
only fails if either pointer isNULL
, or it overflows. So we can confidently set the error to overflow if none of the pointer is NULL.