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
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2481,6 +2481,11 @@ def test_timestamp_aware(self):
self.assertEqual(t.timestamp(),
18000 + 3600 + 2*60 + 3 + 4*1e-6)

@unittest.skipUnless(sys.platform == "win32", "This only overflows on Windows")
def test_timestamp_overflow_windows(self):
t = self.theclass(9000, 1, 1, 1, 2, 3, 4)
self.assertRaises(OverflowError, t.timestamp)

@support.run_with_tz('MSK-03') # Something east of Greenwich
def test_microsecond_rounding(self):
for fts in [self.theclass.fromtimestamp,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :func:`datetime.datetime.timestamp()` raise ``OverflowError`` when overflow on Windows
11 changes: 10 additions & 1 deletion Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1307,8 +1307,17 @@ _PyTime_localtime(time_t t, struct tm *tm)

error = localtime_s(tm, &t);
if (error != 0) {
// There are only 3 cases where an error could occur:
// * tm is NULL
// * &t is NULL (impossible in this usage)
// * it overflows (t < 0 or t > _MAX__TIME64_T)
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!

PyErr_SetFromErrno(PyExc_OSError);
} else {
PyErr_SetString(PyExc_OverflowError,
"localtime argument out of range");
}
return -1;
}
return 0;
Expand Down