diff options
author | Paul Ganssle <1377457+pganssle@users.noreply.github.com> | 2022-05-12 21:00:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-12 21:00:50 (GMT) |
commit | 83c0247d47b99f4571e35ea95361436e1d2a61cd (patch) | |
tree | d22cb6f14811bd7d12339c1b19134fa279285174 /Lib/datetime.py | |
parent | a834e2d8e1230c17193c19b425e83e0bf736179e (diff) | |
download | cpython-83c0247d47b99f4571e35ea95361436e1d2a61cd.zip cpython-83c0247d47b99f4571e35ea95361436e1d2a61cd.tar.gz cpython-83c0247d47b99f4571e35ea95361436e1d2a61cd.tar.bz2 |
Check result of utc_to_seconds and skip fold probe in pure Python (#91582)
The `utc_to_seconds` call can fail, here's a minimal reproducer on
Linux:
TZ=UTC python -c "from datetime import *; datetime.fromtimestamp(253402300799 + 1)"
The old behavior still raised an error in a similar way, but only
because subsequent calculations happened to fail as well. Better to fail
fast.
This also refactors the tests to split out the `fromtimestamp` and
`utcfromtimestamp` tests, and to get us closer to the actual desired
limits of the functions. As part of this, we also changed the way we
detect platforms where the same limits don't necessarily apply (e.g.
Windows).
As part of refactoring the tests to hit this condition explicitly (even
though the user-facing behvior doesn't change in any way we plan to
guarantee), I noticed that there was a difference in the places that
`datetime.utcfromtimestamp` fails in the C and pure Python versions, which
was fixed by skipping the "probe for fold" logic for UTC specifically —
since UTC doesn't have any folds or gaps, we were never going to find a
fold value anyway. This should prevent some failures in the pure python
`utcfromtimestamp` method on timestamps close to 0001-01-01.
There are two separate news entries for this because one is a
potentially user-facing change, the other is an internal code
correctness change that, if anything, changes some error messages. The
two happen to be coupled because of the test refactoring, but they are
probably best thought of as independent changes.
Fixes GH-91581
Diffstat (limited to 'Lib/datetime.py')
-rw-r--r-- | Lib/datetime.py | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py index afbb6fe..00ded32 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1754,7 +1754,7 @@ class datetime(date): y, m, d, hh, mm, ss, weekday, jday, dst = converter(t) ss = min(ss, 59) # clamp out leap seconds if the platform has them result = cls(y, m, d, hh, mm, ss, us, tz) - if tz is None: + if tz is None and not utc: # As of version 2015f max fold in IANA database is # 23 hours at 1969-09-30 13:00:00 in Kwajalein. # Let's probe 24 hours in the past to detect a transition: @@ -1775,7 +1775,7 @@ class datetime(date): probe2 = cls(y, m, d, hh, mm, ss, us, tz) if probe2 == result: result._fold = 1 - else: + elif tz is not None: result = tz.fromutc(result) return result |