Skip to content

gh-152849: Fix OverflowError message for out-of-range float timestamps#152850

Merged
StanFromIreland merged 5 commits into
python:mainfrom
tonghuaroot:fix-gh-152849-pytime-overflow-msg
Jul 6, 2026
Merged

gh-152849: Fix OverflowError message for out-of-range float timestamps#152850
StanFromIreland merged 5 commits into
python:mainfrom
tonghuaroot:fix-gh-152849-pytime-overflow-msg

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

pytime_from_double() in Python/pytime.c converts a float number of
seconds to a PyTime_t (nanoseconds). On overflow it called
pytime_time_t_overflow(), which reports "timestamp out of range for platform time_t" — but this path never converts to time_t. The integer
path, pytime_from_object(), already reports the same conceptual overflow
with pytime_overflow() ("timestamp too large to convert to C PyTime_t").

This swaps the float path to pytime_overflow() so both paths agree and the
message names the type actually involved. The old wording was wrong in fact,
not only in type: PyTime_t saturates at roughly 292 years (int64
nanoseconds), while 64-bit time_t spans far longer, so e.g.
time.sleep(1e10) (~317 years) overflows PyTime_t yet is well within
time_t range — the reported time_t limit was never the actual constraint.
The correct helper already exists in the same file, so this is a one-line
change that only affects the message text; it targets main only.

User-visible via any float-seconds API, e.g.:

>>> time.sleep(2.0 ** 63)   # before: "... platform time_t"; after: "... C PyTime_t"

Verified in a build of main: the _PyTime_FromSecondsObject() float path
(and time.sleep()) now raises the PyTime_t message for both positive and
negative out-of-range values, matching the integer path; the full
test_time suite passes. A regression test asserting the float-path overflow
message is added to TestCPyTime (fails before the fix, passes after).

…estamps

pytime_from_double() converts a float number of seconds to a PyTime_t
(nanoseconds) but reported overflow with pytime_time_t_overflow()
("out of range for platform time_t"), naming a type it never uses. The
integer path already uses pytime_overflow(); switch the float path to it
so both conversion paths report the same PyTime_t overflow.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(not a full review)

Comment thread Lib/test/test_time.py Outdated
Comment thread Lib/test/test_time.py Outdated
Comment thread Python/pytime.c
Comment thread Lib/test/test_time.py

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Few little nits.

Comment thread Lib/test/test_time.py Outdated
Comment thread Lib/test/test_time.py Outdated
Comment thread Misc/NEWS.d/next/Library/2026-07-02-13-30-00.gh-issue-152849.K9dRvP.rst Outdated
Co-authored-by: Stan Ulbrych <stan@python.org>
@StanFromIreland StanFromIreland merged commit 2d7a74e into python:main Jul 6, 2026
54 checks passed
@StanFromIreland StanFromIreland added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 6, 2026
@miss-islington-app

Copy link
Copy Markdown

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

@miss-islington-app

Copy link
Copy Markdown

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

@miss-islington-app

Copy link
Copy Markdown

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

@bedevere-app

bedevere-app Bot commented Jul 6, 2026

Copy link
Copy Markdown

GH-153207 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jul 6, 2026
@bedevere-app

bedevere-app Bot commented Jul 6, 2026

Copy link
Copy Markdown

GH-153208 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 6, 2026
@bedevere-app

bedevere-app Bot commented Jul 6, 2026

Copy link
Copy Markdown

GH-153209 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 6, 2026
@StanFromIreland

Copy link
Copy Markdown
Member

Merged, thanks!

StanFromIreland added a commit that referenced this pull request Jul 6, 2026
…amps in the `time` module (GH-152850) (#153207)

(cherry picked from commit 2d7a74e)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jul 6, 2026
…amps in the `time` module (GH-152850) (#153208)

(cherry picked from commit 2d7a74e)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
StanFromIreland added a commit that referenced this pull request Jul 6, 2026
…amps in the `time` module (GH-152850) (#153209)

(cherry picked from commit 2d7a74e)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants