Skip to content
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

Merged
merged 3 commits into from
May 3, 2023

Conversation

pganssle
Copy link
Member

@pganssle pganssle commented Apr 29, 2023

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.

@pganssle pganssle requested a review from abalkin as a code owner April 29, 2023 21:58
@pganssle pganssle changed the title #103944: Remove last use of utcfromtimestamp GH-103944: Remove last use of utcfromtimestamp Apr 29, 2023
sign = int(ts / ats)

ts, frac = divmod(ats, 1.)
ts *= sign
Copy link
Member

@abalkin abalkin Apr 30, 2023

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.

Copy link
Member Author

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.

@pganssle pganssle force-pushed the utcnow_deprecation branch from 6759ba2 to 82ac549 Compare April 30, 2023 15:42
# XXX Copied from test_float.
INF = float("inf")
NAN = float("nan")


def _utcfromtimestamp(klass, ts):
Copy link
Member

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)

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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.

pganssle added 3 commits May 1, 2023 10:57
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.
@pganssle pganssle force-pushed the utcnow_deprecation branch from 82ac549 to de0a954 Compare May 1, 2023 14:57
@pganssle
Copy link
Member Author

pganssle commented May 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants