Skip to content

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

Closed

Conversation

gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Apr 20, 2023

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 an OverflowError. According to Microsoft docs, localtime_s only fails if either pointer is NULL, or it overflows. So we can confidently set the error to overflow if none of the pointer is NULL.

@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label Apr 20, 2023
@gaogaotiantian
Copy link
Member Author

Hi @pganssle and @abalkin , do you think you could give this a review?

@gaogaotiantian
Copy link
Member Author

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) {
Copy link
Member

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.

Copy link
Member Author

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:

  1. 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.
  2. Document that tm can't be NULL in the comments and/or do an assertion in the function.

I'm fine with either solution.

Copy link
Member

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.

Copy link
Member Author

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!

@gaogaotiantian gaogaotiantian requested a review from vstinner July 17, 2023 21:27
@gaogaotiantian
Copy link
Member Author

@vstinner circling back to this, do you think this is ready to merge? Thanks!

@vstinner
Copy link
Member

I skip my turn. I don't know if it's better to raise an OSError or an OverflowError. I'm not an expert in time/datetime modules.

Maybe @pganssle or @abalkin have an opinion about it.

@brandtbucher brandtbucher self-requested a review September 12, 2023 21:22
Copy link
Member

@brandtbucher brandtbucher left a 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.

@bedevere-app
Copy link

bedevere-app bot commented Oct 18, 2023

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 have made the requested changes; please review again. 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.

@gaogaotiantian
Copy link
Member Author

Since the docs themselves say that OSError can be raised, I'm not sure that there's really a bug to fix here.

Yeah it was added in the docs last year #94818. Personally I believe this is an obvious overflow error, and OSError does not describe it correctly. I understand the backward compatibility concern and it is a rare case. The message OSError: [Errno 22] Invalid argument is confusing to me though.

I'm okay if this is not merged.

@vstinner
Copy link
Member

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.

The message OSError: [Errno 22] Invalid argument is confusing to me though.

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

@vstinner vstinner closed this Oct 21, 2023
@gaogaotiantian gaogaotiantian deleted the fix-timestamp-exception branch March 4, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting changes stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants