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

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

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 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 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 195
196 def test_strptime_exception_context(self): 196 def test_strptime_exception_context(self):
197 # check that this doesn't chain exceptions needlessly (see #17572) 197 # check that this doesn't chain exceptions needlessly (see #17572)
198 with self.assertRaises(ValueError) as e: 198 with self.assertRaises(ValueError) as e:
199 time.strptime('', '%D') 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 %')
200 self.assertIs(e.exception.__suppress_context__, True) 204 self.assertIs(e.exception.__suppress_context__, True)
201 205
202 def test_asctime(self): 206 def test_asctime(self):
203 time.asctime(time.gmtime(self.t)) 207 time.asctime(time.gmtime(self.t))
204 208
205 # Max year is only limited by the size of C int. 209 # Max year is only limited by the size of C int.
206 for bigyear in TIME_MAXYEAR, TIME_MINYEAR: 210 for bigyear in TIME_MAXYEAR, TIME_MINYEAR:
207 asc = time.asctime((bigyear, 6, 1) + (0,) * 6) 211 asc = time.asctime((bigyear, 6, 1) + (0,) * 6)
208 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear)) 212 self.assertEqual(asc[-len(str(bigyear)):], str(bigyear))
209 self.assertRaises(OverflowError, time.asctime, 213 self.assertRaises(OverflowError, time.asctime,
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 self.assertNotEqual(tzname, 'LMT') 364 self.assertNotEqual(tzname, 'LMT')
361 try: 365 try:
362 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1)) 366 time.mktime((-1, 1, 1, 0, 0, 0, -1, -1, -1))
363 except OverflowError: 367 except OverflowError:
364 pass 368 pass
365 self.assertEqual(time.strftime('%Z', tt), tzname) 369 self.assertEqual(time.strftime('%Z', tt), tzname)
366 370
367 @unittest.skipUnless(hasattr(time, 'monotonic'), 371 @unittest.skipUnless(hasattr(time, 'monotonic'),
368 'need time.monotonic') 372 'need time.monotonic')
369 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
370 t1 = time.monotonic() 382 t1 = time.monotonic()
371 time.sleep(0.5) 383 time.sleep(0.5)
372 t2 = time.monotonic() 384 t2 = time.monotonic()
373 dt = t2 - t1 385 dt = t2 - t1
374 self.assertGreater(t2, t1) 386 self.assertGreater(t2, t1)
375 self.assertAlmostEqual(dt, 0.5, delta=0.2) 387 # Issue #20101: On some Windows machines, dt may be slightly low
376 388 self.assertTrue(0.45 <= dt <= 1.0, dt)
389
390 # monotonic() is a monotonic but non adjustable clock
377 info = time.get_clock_info('monotonic') 391 info = time.get_clock_info('monotonic')
378 self.assertTrue(info.monotonic) 392 self.assertTrue(info.monotonic)
379 self.assertFalse(info.adjustable) 393 self.assertFalse(info.adjustable)
380 394
381 def test_perf_counter(self): 395 def test_perf_counter(self):
382 time.perf_counter() 396 time.perf_counter()
383 397
384 def test_process_time(self): 398 def test_process_time(self):
385 # process_time() should not include time spend during a sleep 399 # process_time() should not include time spend during a sleep
386 start = time.process_time() 400 start = time.process_time()
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 def setUp(self): 466 def setUp(self):
453 self.oldloc = locale.setlocale(locale.LC_ALL) 467 self.oldloc = locale.setlocale(locale.LC_ALL)
454 468
455 def tearDown(self): 469 def tearDown(self):
456 locale.setlocale(locale.LC_ALL, self.oldloc) 470 locale.setlocale(locale.LC_ALL, self.oldloc)
457 471
458 def test_bug_3061(self): 472 def test_bug_3061(self):
459 try: 473 try:
460 tmp = locale.setlocale(locale.LC_ALL, "fr_FR") 474 tmp = locale.setlocale(locale.LC_ALL, "fr_FR")
461 except locale.Error: 475 except locale.Error:
462 # skip this test 476 self.skipTest('could not set locale.LC_ALL to fr_FR')
463 return
464 # This should not cause an exception 477 # This should not cause an exception
465 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))
466 479
467 480
468 class _TestAsctimeYear: 481 class _TestAsctimeYear:
469 _format = '%d' 482 _format = '%d'
470 483
471 def yearstr(self, y): 484 def yearstr(self, y):
472 return time.asctime((y,) + (0,) * 8).split()[-1] 485 return time.asctime((y,) + (0,) * 8).split()[-1]
473 486
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 pass 575 pass
563 576
564 577
565 class TestPytime(unittest.TestCase): 578 class TestPytime(unittest.TestCase):
566 def setUp(self): 579 def setUp(self):
567 self.invalid_values = ( 580 self.invalid_values = (
568 -(2 ** 100), 2 ** 100, 581 -(2 ** 100), 2 ** 100,
569 -(2.0 ** 100.0), 2.0 ** 100.0, 582 -(2.0 ** 100.0), 2.0 ** 100.0,
570 ) 583 )
571 584
585 @support.cpython_only
572 def test_time_t(self): 586 def test_time_t(self):
573 from _testcapi import pytime_object_to_time_t 587 from _testcapi import pytime_object_to_time_t
574 for obj, time_t in ( 588 for obj, time_t in (
575 (0, 0), 589 (0, 0),
576 (-1, -1), 590 (-1, -1),
577 (-1.0, -1), 591 (-1.0, -1),
578 (-1.9, -1), 592 (-1.9, -1),
579 (1.0, 1), 593 (1.0, 1),
580 (1.9, 1), 594 (1.9, 1),
581 ): 595 ):
582 self.assertEqual(pytime_object_to_time_t(obj), time_t) 596 self.assertEqual(pytime_object_to_time_t(obj), time_t)
583 597
584 for invalid in self.invalid_values: 598 for invalid in self.invalid_values:
585 self.assertRaises(OverflowError, pytime_object_to_time_t, invalid) 599 self.assertRaises(OverflowError, pytime_object_to_time_t, invalid)
586 600
601 @support.cpython_only
587 def test_timeval(self): 602 def test_timeval(self):
588 from _testcapi import pytime_object_to_timeval 603 from _testcapi import pytime_object_to_timeval
589 for obj, timeval in ( 604 for obj, timeval in (
590 (0, (0, 0)), 605 (0, (0, 0)),
591 (-1, (-1, 0)), 606 (-1, (-1, 0)),
592 (-1.0, (-1, 0)), 607 (-1.0, (-1, 0)),
593 (1e-6, (0, 1)), 608 (1e-6, (0, 1)),
594 (-1e-6, (-1, 999999)), 609 (-1e-6, (-1, 999999)),
595 (-1.2, (-2, 800000)), 610 (-1.2, (-2, 800000)),
596 (1.1234560, (1, 123456)), 611 (1.1234560, (1, 123456)),
597 (1.1234569, (1, 123456)), 612 (1.1234569, (1, 123456)),
598 (-1.1234560, (-2, 876544)), 613 (-1.1234560, (-2, 876544)),
599 (-1.1234561, (-2, 876543)), 614 (-1.1234561, (-2, 876543)),
600 ): 615 ):
601 self.assertEqual(pytime_object_to_timeval(obj), timeval) 616 self.assertEqual(pytime_object_to_timeval(obj), timeval)
602 617
603 for invalid in self.invalid_values: 618 for invalid in self.invalid_values:
604 self.assertRaises(OverflowError, pytime_object_to_timeval, invalid) 619 self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)
605 620
621 @support.cpython_only
606 def test_timespec(self): 622 def test_timespec(self):
607 from _testcapi import pytime_object_to_timespec 623 from _testcapi import pytime_object_to_timespec
608 for obj, timespec in ( 624 for obj, timespec in (
609 (0, (0, 0)), 625 (0, (0, 0)),
610 (-1, (-1, 0)), 626 (-1, (-1, 0)),
611 (-1.0, (-1, 0)), 627 (-1.0, (-1, 0)),
612 (1e-9, (0, 1)), 628 (1e-9, (0, 1)),
613 (-1e-9, (-1, 999999999)), 629 (-1e-9, (-1, 999999999)),
614 (-1.2, (-2, 800000000)), 630 (-1.2, (-2, 800000000)),
615 (1.1234567890, (1, 123456789)), 631 (1.1234567890, (1, 123456789)),
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 687
672 # Load a short time structure using pickle. 688 # Load a short time structure using pickle.
673 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."
674 lt = pickle.loads(st) 690 lt = pickle.loads(st)
675 self.assertIsNone(lt.tm_gmtoff) 691 self.assertIsNone(lt.tm_gmtoff)
676 self.assertIsNone(lt.tm_zone) 692 self.assertIsNone(lt.tm_zone)
677 693
678 694
679 if __name__ == "__main__": 695 if __name__ == "__main__":
680 unittest.main() 696 unittest.main()
LEFTRIGHT

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