Skip to content

Commit 38dc3f2

Browse files
authored
GH-103944: Remove last use of utcfromtimestamp (#103995)
* Remove last use of `utcfromtimestamp` This was a weirdly valid use of `utcfromtimestamp` in the sense that the "timestamps" in TZif files are not epoch times, but actually something more properly thought of as "number of seconds since 1970 in the local time zone", so even though we didn't want UTC time, `utcfromtimestamp` was still a good way to get the thing we wanted. Since we're deprecating `utcfromtimestamp`, it's just as valid to use `timedelta` arithmetic here. We may be able to avoid the question entirely by switching these tests over to using `ZoneInfo` in the future. * Fix a few missing DeprecationWarnings in tests 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.
1 parent 0fc58c6 commit 38dc3f2

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Lib/test/datetimetester.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
33
See https://www.zope.dev/Members/fdrake/DateTimeWiki/TestCases
44
"""
5-
import io
6-
import itertools
75
import bisect
86
import copy
97
import decimal
10-
import sys
8+
import io
9+
import itertools
1110
import os
1211
import pickle
1312
import random
1413
import re
1514
import struct
15+
import sys
1616
import unittest
17+
import warnings
1718

1819
from array import array
1920

@@ -51,11 +52,12 @@
5152
for proto in range(pickle.HIGHEST_PROTOCOL + 1)]
5253
assert len(pickle_choices) == pickle.HIGHEST_PROTOCOL + 1
5354

55+
EPOCH_NAIVE = datetime(1970, 1, 1, 0, 0) # For calculating transitions
56+
5457
# An arbitrary collection of objects of non-datetime types, for testing
5558
# mixed-type comparisons.
5659
OTHERSTUFF = (10, 34.5, "abc", {}, [], ())
5760

58-
5961
# XXX Copied from test_float.
6062
INF = float("inf")
6163
NAN = float("nan")
@@ -2626,9 +2628,10 @@ def test_utcfromtimestamp_limits(self):
26262628
for test_name, ts in test_cases:
26272629
with self.subTest(test_name, ts=ts):
26282630
with self.assertRaises((ValueError, OverflowError)):
2629-
# converting a Python int to C time_t can raise a
2630-
# OverflowError, especially on 32-bit platforms.
2631-
self.theclass.utcfromtimestamp(ts)
2631+
with self.assertWarns(DeprecationWarning):
2632+
# converting a Python int to C time_t can raise a
2633+
# OverflowError, especially on 32-bit platforms.
2634+
self.theclass.utcfromtimestamp(ts)
26322635

26332636
def test_insane_fromtimestamp(self):
26342637
# It's possible that some platform maps time_t to double,
@@ -2645,8 +2648,9 @@ def test_insane_utcfromtimestamp(self):
26452648
# exempt such platforms (provided they return reasonable
26462649
# results!).
26472650
for insane in -1e200, 1e200:
2648-
self.assertRaises(OverflowError, self.theclass.utcfromtimestamp,
2649-
insane)
2651+
with self.assertWarns(DeprecationWarning):
2652+
self.assertRaises(OverflowError, self.theclass.utcfromtimestamp,
2653+
insane)
26502654

26512655
@unittest.skipIf(sys.platform == "win32", "Windows doesn't accept negative timestamps")
26522656
def test_negative_float_fromtimestamp(self):
@@ -3005,7 +3009,7 @@ def __new__(cls, *args, **kwargs):
30053009
for name, meth_name, kwargs in test_cases:
30063010
with self.subTest(name):
30073011
constr = getattr(DateTimeSubclass, meth_name)
3008-
if constr == "utcnow":
3012+
if meth_name == "utcnow":
30093013
with self.assertWarns(DeprecationWarning):
30103014
dt = constr(**kwargs)
30113015
else:
@@ -4733,8 +4737,10 @@ def test_tzinfo_utcfromtimestamp(self):
47334737
# Try with and without naming the keyword; for whatever reason,
47344738
# utcfromtimestamp() doesn't accept a tzinfo argument.
47354739
off42 = FixedOffset(42, "42")
4736-
self.assertRaises(TypeError, meth, ts, off42)
4737-
self.assertRaises(TypeError, meth, ts, tzinfo=off42)
4740+
with warnings.catch_warnings(category=DeprecationWarning):
4741+
warnings.simplefilter("ignore", category=DeprecationWarning)
4742+
self.assertRaises(TypeError, meth, ts, off42)
4743+
self.assertRaises(TypeError, meth, ts, tzinfo=off42)
47384744

47394745
def test_tzinfo_timetuple(self):
47404746
# TestDateTime tested most of this. datetime adds a twist to the
@@ -6102,15 +6108,14 @@ def stats(cls, start_year=1):
61026108
def transitions(self):
61036109
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
61046110
shift = ti[0] - prev_ti[0]
6105-
# TODO: Remove this use of utcfromtimestamp
6106-
yield datetime.utcfromtimestamp(t), shift
6111+
yield (EPOCH_NAIVE + timedelta(seconds=t)), shift
61076112

61086113
def nondst_folds(self):
61096114
"""Find all folds with the same value of isdst on both sides of the transition."""
61106115
for (_, prev_ti), (t, ti) in pairs(zip(self.ut, self.ti)):
61116116
shift = ti[0] - prev_ti[0]
61126117
if shift < ZERO and ti[1] == prev_ti[1]:
6113-
yield datetime.utcfromtimestamp(t), -shift, prev_ti[2], ti[2]
6118+
yield _utcfromtimestamp(datetime, t,), -shift, prev_ti[2], ti[2]
61146119

61156120
@classmethod
61166121
def print_all_nondst_folds(cls, same_abbr=False, start_year=1):

0 commit comments

Comments
 (0)