-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
GH-103944: Remove last use of utcfromtimestamp
#103995
Conversation
utcfromtimestamp
utcfromtimestamp
Lib/test/datetimetester.py
Outdated
sign = int(ts / ats) | ||
|
||
ts, frac = divmod(ats, 1.) | ||
ts *= sign |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use math.copysign to copy sign directly from to here for a more efficient implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this whole thing was an effort to avoid importing math
, so it's something of a re-implementation of math.modf
. At the end of the day that seems... pointless, particularly for the test suite, so I just went ahead and used math.modf
.
6759ba2
to
82ac549
Compare
Lib/test/datetimetester.py
Outdated
# XXX Copied from test_float. | ||
INF = float("inf") | ||
NAN = float("nan") | ||
|
||
|
||
def _utcfromtimestamp(klass, ts): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why simple timedelta arithmetic will not work here:
def _utcfromtimestamp(klass, ts):
return klass(1970, 1, 1) + timedelta(seconds=ts)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pganssle - I ran test_datetime locally with this variant and all test cases pass. I am not sure I can push to your branch, but I will try to submit this change somehow to CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frankly, given that this is only used in tests, I am not sure how much effort is justified in polishing this, but it is probably good to keep test code simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I was just kinda mindlessly copying the implementation from datetime
, but using timedelta
arithmetic is much more elegant.
It's also only used in the one place, so I just inlined it.
There might be easier ways to do this in the future, or we may be able to avoid it entirely by switching to `ZoneInfo` for these tests, but this will remove the last valid use of `utcfromtimetamp` that I see.
In one test, we simply turn off DeprecationWarning rather than asserting about it, because whether the error condition happens before or after the warning seems to differ between the Python and C versions.
82ac549
to
de0a954
Compare
OK, I think that the DeprecationWarnings might be breaking a buildbot or otherwise making it hard to run the test suite, so I am going to go ahead and merge this. |
I guess we missed a few places where
DeprecationWarning
was being raised in the first round of this.This should pass
./python -We -m test -v test_datetime
now.