Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bceb528

Browse files
committed
Test the year range supported by time.strftime() and time.asctime().
1 parent 629a2c3 commit bceb528

1 file changed

Lines changed: 59 additions & 37 deletions

File tree

Lib/test/test_time.py

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
import sys
77
import warnings
88

9+
# Max year is only limited by the size of C int.
10+
SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
11+
TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
12+
TIME_MINYEAR = -TIME_MAXYEAR - 1
13+
14+
915
class TimeTestCase(unittest.TestCase):
1016

1117
def setUp(self):
@@ -91,7 +97,6 @@ def _bounds_checking(self, func):
9197
self.assertRaises(ValueError, func,
9298
(1900, 1, 32, 0, 0, 0, 0, 1, -1))
9399
# Check hour [0, 23]
94-
func((1900, 1, 1, 0, 0, 0, 0, 1, -1))
95100
func((1900, 1, 1, 23, 0, 0, 0, 1, -1))
96101
self.assertRaises(ValueError, func,
97102
(1900, 1, 1, -1, 0, 0, 0, 1, -1))
@@ -165,11 +170,13 @@ def test_asctime(self):
165170
time.asctime(time.gmtime(self.t))
166171

167172
# Max year is only limited by the size of C int.
168-
sizeof_int = sysconfig.get_config_var('SIZEOF_INT') or 4
169-
bigyear = (1 << 8 * sizeof_int - 1) - 1
170-
asc = time.asctime((bigyear, 6, 1) + (0,)*6)
171-
self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
172-
self.assertRaises(OverflowError, time.asctime, (bigyear + 1,) + (0,)*8)
173+
for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
174+
asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
175+
self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
176+
self.assertRaises(OverflowError, time.asctime,
177+
(TIME_MAXYEAR + 1,) + (0,) * 8)
178+
self.assertRaises(OverflowError, time.asctime,
179+
(TIME_MINYEAR - 1,) + (0,) * 8)
173180
self.assertRaises(TypeError, time.asctime, 0)
174181
self.assertRaises(TypeError, time.asctime, ())
175182
self.assertRaises(TypeError, time.asctime, (0,) * 10)
@@ -290,6 +297,24 @@ def test_localtime_without_arg(self):
290297
t1 = time.mktime(lt1)
291298
self.assertAlmostEqual(t1, t0, delta=0.2)
292299

300+
def test_mktime(self):
301+
# Issue #1726687
302+
for t in (-2, -1, 0, 1):
303+
try:
304+
tt = time.localtime(t)
305+
except (OverflowError, ValueError):
306+
pass
307+
else:
308+
self.assertEqual(time.mktime(tt), t)
309+
# It may not be possible to reliably make mktime return error
310+
# on all platfom. This will make sure that no other exception
311+
# than OverflowError is raised for an extreme value.
312+
try:
313+
time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
314+
except OverflowError:
315+
pass
316+
317+
293318
class TestLocale(unittest.TestCase):
294319
def setUp(self):
295320
self.oldloc = locale.setlocale(locale.LC_ALL)
@@ -346,6 +371,28 @@ def year4d(y):
346371
return time.strftime('%4Y', (y,) + (0,) * 8)
347372
self.test_year('%04d', func=year4d)
348373

374+
def skip_if_not_supported(y):
375+
msg = "strftime() is limited to [1; 9999] with Visual Studio"
376+
# Check that it doesn't crash for year > 9999
377+
try:
378+
time.strftime('%Y', (y,) + (0,) * 8)
379+
except ValueError:
380+
cond = False
381+
else:
382+
cond = True
383+
return unittest.skipUnless(cond, msg)
384+
385+
@skip_if_not_supported(10000)
386+
def test_large_year(self):
387+
return super().test_large_year()
388+
389+
@skip_if_not_supported(0)
390+
def test_negative(self):
391+
return super().test_negative()
392+
393+
del skip_if_not_supported
394+
395+
349396
class _Test4dYear(_BaseYearTest):
350397
_format = '%d'
351398

@@ -360,44 +407,19 @@ def test_year(self, fmt=None, func=None):
360407
self.assertEqual(func(9999), fmt % 9999)
361408

362409
def test_large_year(self):
363-
# Check that it doesn't crash for year > 9999
364-
try:
365-
text = self.yearstr(12345)
366-
except ValueError:
367-
# strftime() is limited to [1; 9999] with Visual Studio
368-
return
369-
self.assertEqual(text, '12345')
410+
self.assertEqual(self.yearstr(12345), '12345')
370411
self.assertEqual(self.yearstr(123456789), '123456789')
412+
self.assertEqual(self.yearstr(TIME_MAXYEAR), str(TIME_MAXYEAR))
413+
self.assertRaises(OverflowError, self.yearstr, TIME_MAXYEAR + 1)
371414

372415
def test_negative(self):
373-
try:
374-
text = self.yearstr(-1)
375-
except ValueError:
376-
# strftime() is limited to [1; 9999] with Visual Studio
377-
return
378-
self.assertEqual(text, self._format % -1)
379-
416+
self.assertEqual(self.yearstr(-1), self._format % -1)
380417
self.assertEqual(self.yearstr(-1234), '-1234')
381418
self.assertEqual(self.yearstr(-123456), '-123456')
419+
self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
420+
self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
382421

383422

384-
def test_mktime(self):
385-
# Issue #1726687
386-
for t in (-2, -1, 0, 1):
387-
try:
388-
tt = time.localtime(t)
389-
except (OverflowError, ValueError):
390-
pass
391-
else:
392-
self.assertEqual(time.mktime(tt), t)
393-
# It may not be possible to reliably make mktime return error
394-
# on all platfom. This will make sure that no other exception
395-
# than OverflowError is raised for an extreme value.
396-
try:
397-
time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
398-
except OverflowError:
399-
pass
400-
401423
class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear):
402424
pass
403425

0 commit comments

Comments
 (0)