diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-05-16 15:33:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-16 15:33:01 (GMT) |
commit | 1699a5ee13a39da73f5d9a8c5def4438200b7b59 (patch) | |
tree | 0c95fbf007fd37d124035b74cd4fcd29bc373b84 /Misc | |
parent | 4d05114ae74a5028c7d645a8257e18c5b9ddfa78 (diff) | |
download | cpython-1699a5ee13a39da73f5d9a8c5def4438200b7b59.zip cpython-1699a5ee13a39da73f5d9a8c5def4438200b7b59.tar.gz cpython-1699a5ee13a39da73f5d9a8c5def4438200b7b59.tar.bz2 |
Check result of utc_to_seconds and skip fold probe in pure Python (GH-91582) (GH-92748)
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
(cherry picked from commit 83c0247d47b99f4571e35ea95361436e1d2a61cd)
Co-authored-by: Paul Ganssle <1377457+pganssle@users.noreply.github.com>
Diffstat (limited to 'Misc')
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-04-15-13-16-25.gh-issue-91581.9OGsrN.rst | 6 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-05-11-14-34-09.gh-issue-91581.glkou2.rst | 5 |
2 files changed, 11 insertions, 0 deletions
diff --git a/Misc/NEWS.d/next/Library/2022-04-15-13-16-25.gh-issue-91581.9OGsrN.rst b/Misc/NEWS.d/next/Library/2022-04-15-13-16-25.gh-issue-91581.9OGsrN.rst new file mode 100644 index 0000000..1c3008f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-15-13-16-25.gh-issue-91581.9OGsrN.rst @@ -0,0 +1,6 @@ +Remove an unhandled error case in the C implementation of calls to +:meth:`datetime.fromtimestamp <datetime.datetime.fromtimestamp>` with no time +zone (i.e. getting a local time from an epoch timestamp). This should have no +user-facing effect other than giving a possibly more accurate error message +when called with timestamps that fall on 10000-01-01 in the local time. Patch +by Paul Ganssle. diff --git a/Misc/NEWS.d/next/Library/2022-05-11-14-34-09.gh-issue-91581.glkou2.rst b/Misc/NEWS.d/next/Library/2022-05-11-14-34-09.gh-issue-91581.glkou2.rst new file mode 100644 index 0000000..846f578 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-05-11-14-34-09.gh-issue-91581.glkou2.rst @@ -0,0 +1,5 @@ +:meth:`~datetime.datetime.utcfromtimestamp` no longer attempts to resolve +``fold`` in the pure Python implementation, since the fold is never 1 in UTC. +In addition to being slightly faster in the common case, this also prevents +some errors when the timestamp is close to :attr:`datetime.min +<datetime.datetime.min>`. Patch by Paul Ganssle. |