Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(264841)

Delta Between Two Patch Sets: Lib/test/test_time.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_tempfile.py ('k') | Lib/test/test_traceback.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from test import support 1 from test import support
2 import time 2 import time
3 import unittest 3 import unittest
4 import locale 4 import locale
5 import sysconfig 5 import sysconfig
6 import sys 6 import sys
7 import platform 7 import platform
8 try: 8 try:
9 import threading 9 import threading
10 except ImportError: 10 except ImportError:
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 # values. No test for daylight savings since strftime() does 168 # values. No test for daylight savings since strftime() does
169 # not change output based on its value and no test for year 169 # not change output based on its value and no test for year
170 # because systems vary in their support for year 0. 170 # because systems vary in their support for year 0.
171 expected = "2000 01 01 00 00 00 1 001" 171 expected = "2000 01 01 00 00 00 1 001"
172 with support.check_warnings(): 172 with support.check_warnings():
173 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8) 173 result = time.strftime("%Y %m %d %H %M %S %w %j", (2000,)+(0,)*8)
174 self.assertEqual(expected, result) 174 self.assertEqual(expected, result)
175 175
176 def test_strptime(self): 176 def test_strptime(self):
177 # Should be able to go round-trip from strftime to strptime without 177 # Should be able to go round-trip from strftime to strptime without
178 # throwing an exception. 178 # raising an exception.
179 tt = time.gmtime(self.t) 179 tt = time.gmtime(self.t)
180 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I', 180 for directive in ('a', 'A', 'b', 'B', 'c', 'd', 'H', 'I',
181 'j', 'm', 'M', 'p', 'S', 181 'j', 'm', 'M', 'p', 'S',
182 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'): 182 'U', 'w', 'W', 'x', 'X', 'y', 'Y', 'Z', '%'):
183 format = '%' + directive 183 format = '%' + directive
184 strf_output = time.strftime(format, tt) 184 strf_output = time.strftime(format, tt)
185 try: 185 try:
186 time.strptime(strf_output, format) 186 time.strptime(strf_output, format)
187 except ValueError: 187 except ValueError:
188 self.fail("conversion specifier %r failed with '%s' input." % 188 self.fail("conversion specifier %r failed with '%s' input." %
189 (format, strf_output)) 189 (format, strf_output))
190 190
191 def test_strptime_bytes(self): 191 def test_strptime_bytes(self):
192 # Make sure only strings are accepted as arguments to strptime. 192 # Make sure only strings are accepted as arguments to strptime.
193 self.assertRaises(TypeError, time.strptime, b'2009', "%Y") 193 self.assertRaises(TypeError, time.strptime, b'2009', "%Y")
194 self.assertRaises(TypeError, time.strptime, '2009', b'%Y') 194 self.assertRaises(TypeError, time.strptime, '2009', b'%Y')
195
196 def test_strptime_exception_context(self):
197 # check that this doesn't chain exceptions needlessly (see #17572)
198 with self.assertRaises(ValueError) as e:
199 time.strptime('', '%D')
200 self.assertIs(e.exception.__suppress_context__, True)
201 # additional check for IndexError branch (issue #19545)
202 with self.assertRaises(ValueError) as e:
203 time.strptime('19', '%Y %')
204 self.assertIs(e.exception.__suppress_context__, True)
195 205
196 def test_asctime(self): 206 def test_asctime(self):
197 time.asctime(time.gmtime(self.t)) 207 time.asctime(time.gmtime(self.t))
198 208
199 # Max year is only limited by the size of C int. 209 # Max year is only limited by the size of C int.
200 for bigyear in TIME_MAXYEAR, TIME_MINYEAR: 210 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
201 asc = time.asctime((bigyear, 6, 1) + (0,) * 6) 211 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
202 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) 212 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
203 self.assertRaises(OverflowError, time.asctime, 213 self.assertRaises(OverflowError, time.asctime,
204 (TIME_MAXYEAR + 1,) + (0,) * 8) 214 (TIME_MAXYEAR + 1,) + (0,) * 8)
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 environ['TZ'] = victoria 287 environ['TZ'] = victoria
278 time.tzset() 288 time.tzset()
279 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002)) 289 self.assertNotEqual(time.gmtime(xmas2002), time.localtime(xmas2002))
280 290
281 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called 291 # Issue #11886: Australian Eastern Standard Time (UTC+10) is called
282 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST" 292 # "EST" (as Eastern Standard Time, UTC-5) instead of "AEST"
283 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone), 293 # (non-DST timezone), and "EDT" instead of "AEDT" (DST timezone),
284 # on some operating systems (e.g. FreeBSD), which is wrong. See for 294 # on some operating systems (e.g. FreeBSD), which is wrong. See for
285 # example this bug: 295 # example this bug:
286 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810 296 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=93810
287 self.assertIn(time.tzname[0], ('AEST' 'EST'), time.tzname[0]) 297 self.assertIn(time.tzname[0], ('AEST' 'EST'))
288 self.assertIn(time.tzname[1], ('AEDT', 'EDT'), str(time.tzname[1])) 298 self.assertIn(time.tzname[1], ('AEDT', 'EDT'))
289 self.assertEqual(len(time.tzname), 2) 299 self.assertEqual(len(time.tzname), 2)
290 self.assertEqual(time.daylight, 1) 300 self.assertEqual(time.daylight, 1)
291 self.assertEqual(time.timezone, -36000) 301 self.assertEqual(time.timezone, -36000)
292 self.assertEqual(time.altzone, -39600) 302 self.assertEqual(time.altzone, -39600)
293 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1) 303 self.assertEqual(time.localtime(xmas2002).tm_isdst, 1)
294 304
295 finally: 305 finally:
296 # Repair TZ environment variable in case any other tests 306 # Repair TZ environment variable in case any other tests
297 # rely on it. 307 # rely on it.
298 if org_TZ is not None: 308 if org_TZ is not None:
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 self.assertNotEqual(tzname, 'LMT') 364 self.assertNotEqual(tzname, 'LMT')
355 try: 365 try:
356 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1)) 366 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
357 except OverflowError: 367 except OverflowError:
358 pass 368 pass
359 self.assertEqual(time.strftime('%Z', tt), tzname) 369 self.assertEqual(time.strftime('%Z', tt), tzname)
360 370
361 @unittest.skipUnless(hasattr(time, 'monotonic'), 371 @unittest.skipUnless(hasattr(time, 'monotonic'),
362 'need time.monotonic') 372 'need time.monotonic')
363 def test_monotonic(self): 373 def test_monotonic(self):
374 # monotonic() should not go backward
375 times = [time.monotonic() for n in range(100)]
376 t1 = times[0]
377 for t2 in times[1:]:
378 self.assertGreaterEqual(t2, t1, "times=%s" % times)
379 t1 = t2
380
381 # monotonic() includes time elapsed during a sleep
364 t1 = time.monotonic() 382 t1 = time.monotonic()
365 time.sleep(0.1) 383 time.sleep(0.5)
366 t2 = time.monotonic() 384 t2 = time.monotonic()
367 dt = t2 - t1 385 dt = t2 - t1
368 self.assertGreater(t2, t1) 386 self.assertGreater(t2, t1)
369 self.assertAlmostEqual(dt, 0.1, delta=0.2) 387 # Issue #20101: On some Windows machines, dt may be slightly low
370 388 self.assertTrue(0.45 <= dt <= 1.0, dt)
389
390 # monotonic() is a monotonic but non adjustable clock
371 info = time.get_clock_info('monotonic') 391 info = time.get_clock_info('monotonic')
372 self.assertTrue(info.monotonic) 392 self.assertTrue(info.monotonic)
373 self.assertFalse(info.adjustable) 393 self.assertFalse(info.adjustable)
374 394
375 def test_perf_counter(self): 395 def test_perf_counter(self):
376 time.perf_counter() 396 time.perf_counter()
377 397
378 def test_process_time(self): 398 def test_process_time(self):
379 # process_time() should not include time spend during a sleep 399 # process_time() should not include time spend during a sleep
380 start = time.process_time() 400 start = time.process_time()
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 def setUp(self): 466 def setUp(self):
447 self.oldloc = locale.setlocale(locale.LC_ALL) 467 self.oldloc = locale.setlocale(locale.LC_ALL)
448 468
449 def tearDown(self): 469 def tearDown(self):
450 locale.setlocale(locale.LC_ALL, self.oldloc) 470 locale.setlocale(locale.LC_ALL, self.oldloc)
451 471
452 def test_bug_3061(self): 472 def test_bug_3061(self):
453 try: 473 try:
454 tmp = locale.setlocale(locale.LC_ALL, "fr_FR") 474 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
455 except locale.Error: 475 except locale.Error:
456 # skip this test 476 self.skipTest('could not set locale.LC_ALL to fr_FR')
457 return
458 # This should not cause an exception 477 # This should not cause an exception
459 time.strftime("%B", (2009,2,1,0,0,0,0,0,0)) 478 time.strftime("%B", (2009,2,1,0,0,0,0,0,0))
460 479
461
462 class _BaseYearTest(unittest.TestCase):
463 def yearstr(self, y):
464 raise NotImplementedError()
465 480
466 class _TestAsctimeYear: 481 class _TestAsctimeYear:
467 _format = '%d' 482 _format = '%d'
468 483
469 def yearstr(self, y): 484 def yearstr(self, y):
470 return time.asctime((y,) + (0,) * 8).split()[-1] 485 return time.asctime((y,) + (0,) * 8).split()[-1]
471 486
472 def test_large_year(self): 487 def test_large_year(self):
473 # Check that it doesn't crash for year > 9999 488 # Check that it doesn't crash for year > 9999
474 self.assertEqual(self.yearstr(12345), '12345') 489 self.assertEqual(self.yearstr(12345), '12345')
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 def test_large_year(self): 528 def test_large_year(self):
514 return super().test_large_year() 529 return super().test_large_year()
515 530
516 @skip_if_not_supported(0) 531 @skip_if_not_supported(0)
517 def test_negative(self): 532 def test_negative(self):
518 return super().test_negative() 533 return super().test_negative()
519 534
520 del skip_if_not_supported 535 del skip_if_not_supported
521 536
522 537
523 class _Test4dYear(_BaseYearTest): 538 class _Test4dYear:
524 _format = '%d' 539 _format = '%d'
525 540
526 def test_year(self, fmt=None, func=None): 541 def test_year(self, fmt=None, func=None):
527 fmt = fmt or self._format 542 fmt = fmt or self._format
528 func = func or self.yearstr 543 func = func or self.yearstr
529 self.assertEqual(func(1), fmt % 1) 544 self.assertEqual(func(1), fmt % 1)
530 self.assertEqual(func(68), fmt % 68) 545 self.assertEqual(func(68), fmt % 68)
531 self.assertEqual(func(69), fmt % 69) 546 self.assertEqual(func(69), fmt % 69)
532 self.assertEqual(func(99), fmt % 99) 547 self.assertEqual(func(99), fmt % 99)
533 self.assertEqual(func(999), fmt % 999) 548 self.assertEqual(func(999), fmt % 999)
(...skipping 12 matching lines...) Expand all
546 self.assertEqual(self.yearstr(-123456789), str(-123456789)) 561 self.assertEqual(self.yearstr(-123456789), str(-123456789))
547 self.assertEqual(self.yearstr(-1234567890), str(-1234567890)) 562 self.assertEqual(self.yearstr(-1234567890), str(-1234567890))
548 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1 900)) 563 self.assertEqual(self.yearstr(TIME_MINYEAR + 1900), str(TIME_MINYEAR + 1 900))
549 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900 564 # Issue #13312: it may return wrong value for year < TIME_MINYEAR + 1900
550 # Skip the value test, but check that no error is raised 565 # Skip the value test, but check that no error is raised
551 self.yearstr(TIME_MINYEAR) 566 self.yearstr(TIME_MINYEAR)
552 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR)) 567 # self.assertEqual(self.yearstr(TIME_MINYEAR), str(TIME_MINYEAR))
553 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1) 568 self.assertRaises(OverflowError, self.yearstr, TIME_MINYEAR - 1)
554 569
555 570
556 class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear): 571 class TestAsctime4dyear(_TestAsctimeYear, _Test4dYear, unittest.TestCase):
557 pass 572 pass
558 573
559 class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear): 574 class TestStrftime4dyear(_TestStrftimeYear, _Test4dYear, unittest.TestCase):
560 pass 575 pass
561 576
562 577
563 class TestPytime(unittest.TestCase): 578 class TestPytime(unittest.TestCase):
564 def setUp(self): 579 def setUp(self):
565 self.invalid_values = ( 580 self.invalid_values = (
566 -(2 ** 100), 2 ** 100, 581 -(2 ** 100), 2 ** 100,
567 -(2.0 ** 100.0), 2.0 ** 100.0, 582 -(2.0 ** 100.0), 2.0 ** 100.0,
568 ) 583 )
569 584
585 @support.cpython_only
570 def test_time_t(self): 586 def test_time_t(self):
571 from _testcapi import pytime_object_to_time_t 587 from _testcapi import pytime_object_to_time_t
572 for obj, time_t in ( 588 for obj, time_t in (
573 (0, 0), 589 (0, 0),
574 (-1, -1), 590 (-1, -1),
575 (-1.0, -1), 591 (-1.0, -1),
576 (-1.9, -1), 592 (-1.9, -1),
577 (1.0, 1), 593 (1.0, 1),
578 (1.9, 1), 594 (1.9, 1),
579 ): 595 ):
580 self.assertEqual(pytime_object_to_time_t(obj), time_t) 596 self.assertEqual(pytime_object_to_time_t(obj), time_t)
581 597
582 for invalid in self.invalid_values: 598 for invalid in self.invalid_values:
583 self.assertRaises(OverflowError, pytime_object_to_time_t, invalid) 599 self.assertRaises(OverflowError, pytime_object_to_time_t, invalid)
584 600
601 @support.cpython_only
585 def test_timeval(self): 602 def test_timeval(self):
586 from _testcapi import pytime_object_to_timeval 603 from _testcapi import pytime_object_to_timeval
587 for obj, timeval in ( 604 for obj, timeval in (
588 (0, (0, 0)), 605 (0, (0, 0)),
589 (-1, (-1, 0)), 606 (-1, (-1, 0)),
590 (-1.0, (-1, 0)), 607 (-1.0, (-1, 0)),
591 (1e-6, (0, 1)), 608 (1e-6, (0, 1)),
592 (-1e-6, (-1, 999999)), 609 (-1e-6, (-1, 999999)),
593 (-1.2, (-2, 800000)), 610 (-1.2, (-2, 800000)),
594 (1.1234560, (1, 123456)), 611 (1.1234560, (1, 123456)),
595 (1.1234569, (1, 123456)), 612 (1.1234569, (1, 123456)),
596 (-1.1234560, (-2, 876544)), 613 (-1.1234560, (-2, 876544)),
597 (-1.1234561, (-2, 876543)), 614 (-1.1234561, (-2, 876543)),
598 ): 615 ):
599 self.assertEqual(pytime_object_to_timeval(obj), timeval) 616 self.assertEqual(pytime_object_to_timeval(obj), timeval)
600 617
601 for invalid in self.invalid_values: 618 for invalid in self.invalid_values:
602 self.assertRaises(OverflowError, pytime_object_to_timeval, invalid) 619 self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)
603 620
621 @support.cpython_only
604 def test_timespec(self): 622 def test_timespec(self):
605 from _testcapi import pytime_object_to_timespec 623 from _testcapi import pytime_object_to_timespec
606 for obj, timespec in ( 624 for obj, timespec in (
607 (0, (0, 0)), 625 (0, (0, 0)),
608 (-1, (-1, 0)), 626 (-1, (-1, 0)),
609 (-1.0, (-1, 0)), 627 (-1.0, (-1, 0)),
610 (1e-9, (0, 1)), 628 (1e-9, (0, 1)),
611 (-1e-9, (-1, 999999999)), 629 (-1e-9, (-1, 999999999)),
612 (-1.2, (-2, 800000000)), 630 (-1.2, (-2, 800000000)),
613 (1.1234567890, (1, 123456789)), 631 (1.1234567890, (1, 123456789)),
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 def test_short_times(self): 684 def test_short_times(self):
667 685
668 import pickle 686 import pickle
669 687
670 # Load a short time structure using pickle. 688 # Load a short time structure using pickle.
671 st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\ nI1\ntp1\n(dp2\ntp3\nRp4\n." 689 st = b"ctime\nstruct_time\np0\n((I2007\nI8\nI11\nI1\nI24\nI49\nI5\nI223\ nI1\ntp1\n(dp2\ntp3\nRp4\n."
672 lt = pickle.loads(st) 690 lt = pickle.loads(st)
673 self.assertIsNone(lt.tm_gmtoff) 691 self.assertIsNone(lt.tm_gmtoff)
674 self.assertIsNone(lt.tm_zone) 692 self.assertIsNone(lt.tm_zone)
675 693
676 def test_main():
677 support.run_unittest(
678 TimeTestCase,
679 TestLocale,
680 TestAsctime4dyear,
681 TestStrftime4dyear,
682 TestPytime)
683 694
684 if __name__ == "__main__": 695 if __name__ == "__main__":
685 test_main() 696 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+