Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit38dc3f2

Browse files
authored
GH-103944: Remove last use ofutcfromtimestamp (#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 testsIn 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 parent0fc58c6 commit38dc3f2

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

‎Lib/test/datetimetester.py

Lines changed: 20 additions & 15 deletions
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-
importio
6-
importitertools
75
importbisect
86
importcopy
97
importdecimal
10-
importsys
8+
importio
9+
importitertools
1110
importos
1211
importpickle
1312
importrandom
1413
importre
1514
importstruct
15+
importsys
1616
importunittest
17+
importwarnings
1718

1819
fromarrayimportarray
1920

@@ -51,11 +52,12 @@
5152
forprotoinrange(pickle.HIGHEST_PROTOCOL+1)]
5253
assertlen(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
fortest_name,tsintest_cases:
26272629
withself.subTest(test_name,ts=ts):
26282630
withself.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+
withself.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
deftest_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
forinsanein-1e200,1e200:
2648-
self.assertRaises(OverflowError,self.theclass.utcfromtimestamp,
2649-
insane)
2651+
withself.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
deftest_negative_float_fromtimestamp(self):
@@ -3005,7 +3009,7 @@ def __new__(cls, *args, **kwargs):
30053009
forname,meth_name,kwargsintest_cases:
30063010
withself.subTest(name):
30073011
constr=getattr(DateTimeSubclass,meth_name)
3008-
ifconstr=="utcnow":
3012+
ifmeth_name=="utcnow":
30093013
withself.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+
withwarnings.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
deftest_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
deftransitions(self):
61036109
for (_,prev_ti), (t,ti)inpairs(zip(self.ut,self.ti)):
61046110
shift=ti[0]-prev_ti[0]
6105-
# TODO: Remove this use of utcfromtimestamp
6106-
yielddatetime.utcfromtimestamp(t),shift
6111+
yield (EPOCH_NAIVE+timedelta(seconds=t)),shift
61076112

61086113
defnondst_folds(self):
61096114
"""Find all folds with the same value of isdst on both sides of the transition."""
61106115
for (_,prev_ti), (t,ti)inpairs(zip(self.ut,self.ti)):
61116116
shift=ti[0]-prev_ti[0]
61126117
ifshift<ZEROandti[1]==prev_ti[1]:
6113-
yielddatetime.utcfromtimestamp(t),-shift,prev_ti[2],ti[2]
6118+
yield_utcfromtimestamp(datetime,t,),-shift,prev_ti[2],ti[2]
61146119

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp