Skip to content

Commit 9750570

Browse files
committed
fix multiply_int_timedelta
1 parent 77c4865 commit 9750570

File tree

2 files changed

+9
-44
lines changed

2 files changed

+9
-44
lines changed

Lib/test/datetimetester.py

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ def test_cannot_subclass(self):
310310
class MyTimezone(timezone): pass
311311

312312
def test_utcoffset(self):
313-
if 'Pure' not in self.__class__.__name__: return # BUG
314313
dummy = self.DT
315314
for h in [0, 1.5, 12]:
316315
offset = h * HOUR
@@ -352,7 +351,6 @@ def test_tzname(self):
352351
with self.assertRaises(TypeError): self.EST.tzname(5)
353352

354353
def test_fromutc(self):
355-
if 'Pure' not in self.__class__.__name__: return # BUG
356354
with self.assertRaises(ValueError):
357355
timezone.utc.fromutc(self.DT)
358356
with self.assertRaises(TypeError):
@@ -365,7 +363,6 @@ def test_fromutc(self):
365363
self.DT.replace(tzinfo=timezone.utc))
366364

367365
def test_comparison(self):
368-
if 'Pure' not in self.__class__.__name__: return # BUG
369366
self.assertNotEqual(timezone(ZERO), timezone(HOUR))
370367
self.assertEqual(timezone(HOUR), timezone(HOUR))
371368
self.assertEqual(timezone(-5 * HOUR), timezone(-5 * HOUR, 'EST'))
@@ -552,7 +549,6 @@ def test_constructor(self):
552549
ra(TypeError, lambda: td(microseconds='1'))
553550

554551
def test_computations(self):
555-
if 'Pure' not in self.__class__.__name__: return # BUG
556552
eq = self.assertEqual
557553
td = timedelta
558554

@@ -867,7 +863,6 @@ def _test_overflow_special(self):
867863
self.assertRaises(OverflowError, day.__mul__, -INF)
868864

869865
def test_microsecond_rounding(self):
870-
if 'Pure' not in self.__class__.__name__: return # BUG
871866
td = timedelta
872867
eq = self.assertEqual
873868

@@ -878,8 +873,8 @@ def test_microsecond_rounding(self):
878873
eq(td(milliseconds=-0.5/1000), td(nanoseconds=-500))
879874
eq(td(milliseconds=0.6/1000), td(nanoseconds=600))
880875
eq(td(milliseconds=-0.6/1000), td(nanoseconds=-600))
881-
eq(td(milliseconds=1.5/1000), td(nanoseconds=1500))
882-
eq(td(milliseconds=-1.5/1000), td(nanoseconds=-1500))
876+
eq(td(milliseconds=1.5/1000), td(microseconds=1, nanoseconds=500))
877+
eq(td(milliseconds=-1.5/1000), td(microseconds=-1, nanoseconds=-500))
883878
eq(td(seconds=0.5/10**6), td(nanoseconds=500))
884879
eq(td(seconds=-0.5/10**6), td(nanoseconds=-500))
885880
eq(td(seconds=1/2**7), td(microseconds=7812, nanoseconds=500))
@@ -1143,7 +1138,6 @@ def test_delta_non_days_ignored(self):
11431138
self.assertEqual(dt2, dt - days)
11441139

11451140
def test_strptime(self):
1146-
if 'Pure' not in self.__class__.__name__: return # BUG
11471141
inputs = [
11481142
# Basic valid cases
11491143
(date(1998, 2, 3), '1998-02-03', '%Y-%m-%d'),
@@ -1380,7 +1374,6 @@ def test_hash_equality(self):
13801374
self.assertEqual(dic[e], 2)
13811375

13821376
def test_computations(self):
1383-
if 'Pure' not in self.__class__.__name__: return # BUG
13841377
a = self.theclass(2002, 1, 31)
13851378
b = self.theclass(1956, 1, 31)
13861379
c = self.theclass(2001,2,1)
@@ -1431,7 +1424,6 @@ def test_computations(self):
14311424
self.assertRaises(TypeError, lambda: a + a)
14321425

14331426
def test_overflow(self):
1434-
if 'Pure' not in self.__class__.__name__: return # BUG
14351427
tiny = self.theclass.resolution
14361428

14371429
for delta in [tiny, timedelta(1), timedelta(2)]:
@@ -2269,7 +2261,6 @@ def test_isoformat(self):
22692261
self.assertEqual(t.isoformat(), "0002-03-02T00:00:00+00:00:16")
22702262

22712263
def test_isoformat_timezone(self):
2272-
if 'Pure' not in self.__class__.__name__: return # BUG
22732264
tzoffsets = [
22742265
('05:00', timedelta(hours=5)),
22752266
('02:00', timedelta(hours=2)),
@@ -2297,7 +2288,7 @@ def test_isoformat_timezone(self):
22972288
dt = dt_base.replace(tzinfo=tzi)
22982289
exp = exp_base + exp_tz
22992290
with self.subTest(tzi=tzi):
2300-
assert dt.isoformat() == exp
2291+
self.assertEqual(dt.isoformat(), exp)
23012292

23022293
def test_format(self):
23032294
dt = self.theclass(2007, 9, 10, 4, 5, 1, 123)
@@ -2450,7 +2441,6 @@ def test_hash_equality(self):
24502441
self.assertEqual(dic[e], 2)
24512442

24522443
def test_computations(self):
2453-
if 'Pure' not in self.__class__.__name__: return # BUG
24542444
a = self.theclass(2002, 1, 31)
24552445
b = self.theclass(1956, 1, 31)
24562446
diff = a-b
@@ -2862,7 +2852,6 @@ def test_utcnow(self):
28622852
self.assertLessEqual(abs(from_timestamp - from_now), tolerance)
28632853

28642854
def test_strptime(self):
2865-
if 'Pure' not in self.__class__.__name__: return # BUG
28662855
string = '2004-12-01 13:02:47.197'
28672856
format = '%Y-%m-%d %H:%M:%S.%f'
28682857
expected = _strptime._strptime_datetime_datetime(self.theclass, string,
@@ -3314,7 +3303,6 @@ def test_fromisoformat_datetime(self):
33143303
self.assertEqual(dt, dt_rt)
33153304

33163305
def test_fromisoformat_timezone(self):
3317-
if 'Pure' not in self.__class__.__name__: return # BUG
33183306
base_dt = self.theclass(2014, 12, 30, 12, 30, 45, 217456)
33193307

33203308
tzoffsets = [
@@ -3823,7 +3811,6 @@ def test_isoformat(self):
38233811
self.assertEqual(t.isoformat(timespec='auto'), "12:34:56")
38243812

38253813
def test_isoformat_timezone(self):
3826-
if 'Pure' not in self.__class__.__name__: return # BUG
38273814
tzoffsets = [
38283815
('05:00', timedelta(hours=5)),
38293816
('02:00', timedelta(hours=2)),
@@ -4007,7 +3994,6 @@ def test_compat_unpickle(self):
40073994
self.assertEqual(derived, expected)
40083995

40093996
def test_strptime(self):
4010-
if 'Pure' not in self.__class__.__name__: return # BUG
40113997
# bpo-34482: Check that surrogates are handled properly.
40123998
inputs = [
40133999
(self.theclass(13, 2, 47, 197000), '13:02:47.197', '%H:%M:%S.%f'),
@@ -4021,7 +4007,6 @@ def test_strptime(self):
40214007
self.assertIs(type(got), self.theclass)
40224008

40234009
def test_strptime_tz(self):
4024-
if 'Pure' not in self.__class__.__name__: return # BUG
40254010
strptime = self.theclass.strptime
40264011
self.assertEqual(strptime("+0002", "%z").utcoffset(), 2 * MINUTE)
40274012
self.assertEqual(strptime("-0002", "%z").utcoffset(), -2 * MINUTE)
@@ -4346,7 +4331,6 @@ def test_empty(self):
43464331
self.assertIsNone(t.tzinfo)
43474332

43484333
def test_zones(self):
4349-
if 'Pure' not in self.__class__.__name__: return # BUG
43504334
est = FixedOffset(-300, "EST", 1)
43514335
utc = FixedOffset(0, "UTC", -2)
43524336
met = FixedOffset(60, "MET", 3)
@@ -4635,7 +4619,6 @@ def test_fromisoformat(self):
46354619
self.assertEqual(t, t_rt)
46364620

46374621
def test_fromisoformat_timezone(self):
4638-
if 'Pure' not in self.__class__.__name__: return # BUG
46394622
base_time = self.theclass(12, 30, 45, 217456)
46404623

46414624
tzoffsets = [
@@ -5718,7 +5701,6 @@ def convert_between_tz_and_utc(self, tz, utc):
57185701
self.checkoutside(outside, tz, utc)
57195702

57205703
def test_easy(self):
5721-
if 'Pure' not in self.__class__.__name__: return # BUG
57225704
# Despite the name of this test, the endcases are excruciating.
57235705
self.convert_between_tz_and_utc(Eastern, utc_real)
57245706
self.convert_between_tz_and_utc(Pacific, utc_real)
@@ -5743,7 +5725,6 @@ def test_easy(self):
57435725
# self.convert_between_tz_and_utc(Central, Eastern) # can't work
57445726

57455727
def test_tricky(self):
5746-
if 'Pure' not in self.__class__.__name__: return # BUG
57475728
# 22:00 on day before daylight starts.
57485729
fourback = self.dston - timedelta(hours=4)
57495730
ninewest = FixedOffset(-9*60, "-0900", 0)
@@ -5795,7 +5776,6 @@ def test_tricky(self):
57955776

57965777

57975778
def test_bogus_dst(self):
5798-
if 'Pure' not in self.__class__.__name__: return # BUG
57995779
class ok(tzinfo):
58005780
def utcoffset(self, dt): return HOUR
58015781
def dst(self, dt): return HOUR
@@ -5823,7 +5803,6 @@ def dst(self, dt):
58235803
self.assertRaises(ValueError, dt.astimezone, tricky_notok())
58245804

58255805
def test_fromutc(self):
5826-
if 'Pure' not in self.__class__.__name__: return # BUG
58275806
self.assertRaises(TypeError, Eastern.fromutc) # not enough args
58285807
now = datetime.now(tz=utc_real)
58295808
self.assertRaises(ValueError, Eastern.fromutc, now) # wrong tzinfo
@@ -6129,7 +6108,6 @@ def fromutc(self, dt):
61296108
class TestLocalTimeDisambiguation(unittest.TestCase):
61306109

61316110
def test_vilnius_1941_fromutc(self):
6132-
if 'Pure' not in self.__class__.__name__: return # BUG
61336111
Vilnius = Europe_Vilnius_1941()
61346112

61356113
gdt = datetime(1941, 6, 23, 20, 59, 59, tzinfo=timezone.utc)
@@ -6154,7 +6132,6 @@ def test_vilnius_1941_fromutc(self):
61546132
self.assertTrue(ldt.dst())
61556133

61566134
def test_vilnius_1941_toutc(self):
6157-
if 'Pure' not in self.__class__.__name__: return # BUG
61586135
Vilnius = Europe_Vilnius_1941()
61596136

61606137
ldt = datetime(1941, 6, 23, 22, 59, 59, tzinfo=Vilnius)
@@ -6333,7 +6310,6 @@ def test_dst(self):
63336310

63346311

63356312
def test_utcoffset(self):
6336-
if 'Pure' not in self.__class__.__name__: return # BUG
63376313
# Let's first establish that things work in regular times.
63386314
dt_summer = datetime(2002, 10, 27, 1, tzinfo=Eastern2) - timedelta.resolution
63396315
dt_winter = datetime(2002, 10, 27, 2, tzinfo=Eastern2)
@@ -6344,7 +6320,6 @@ def test_utcoffset(self):
63446320
self.assertEqual(dt_winter.replace(fold=1).utcoffset(), -5 * HOUR)
63456321

63466322
def test_fromutc(self):
6347-
if 'Pure' not in self.__class__.__name__: return # BUG
63486323
# Let's first establish that things work in regular times.
63496324
u_summer = datetime(2002, 10, 27, 6, tzinfo=Eastern2) - timedelta.resolution
63506325
u_winter = datetime(2002, 10, 27, 7, tzinfo=Eastern2)

Modules/_datetimemodule.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,22 +2354,12 @@ nanoseconds_to_delta(PyObject *pyns)
23542354
static PyObject *
23552355
multiply_int_timedelta(PyObject *intobj, PyDateTime_Delta *delta)
23562356
{
2357-
PyObject *pyus_in;
2358-
PyObject *pyus_out;
2359-
PyObject *result;
2360-
2361-
pyus_in = delta_to_nanoseconds(delta);
2362-
if (pyus_in == NULL)
2363-
return NULL;
2364-
2365-
pyus_out = PyNumber_Multiply(intobj, pyus_in);
2366-
Py_DECREF(pyus_in);
2367-
if (pyus_out == NULL)
2368-
return NULL;
2369-
2370-
result = nanoseconds_to_delta(pyus_out);
2371-
Py_DECREF(pyus_out);
2372-
return result;
2357+
return new_delta_ex(GET_TD_DAYS(delta) * PyLong_AsLong(intobj),
2358+
GET_TD_SECONDS(delta) * PyLong_AsLong(intobj),
2359+
GET_TD_MICROSECONDS(delta) * PyLong_AsLong(intobj),
2360+
GET_TD_NANOSECONDS(delta) * PyLong_AsLong(intobj),
2361+
1,
2362+
DELTA_TYPE(NO_STATE));
23732363
}
23742364

23752365
static PyObject *

0 commit comments

Comments
 (0)