|
1 | 1 | from test import support |
| 2 | +from test.support import warnings_helper |
2 | 3 | import decimal |
3 | 4 | import enum |
4 | 5 | import locale |
|
14 | 15 | except ImportError: |
15 | 16 | _testcapi = None |
16 | 17 |
|
17 | | -from test.support import skip_if_buggy_ucrt_strfptime, warnings_helper |
| 18 | +from test.support import skip_if_buggy_ucrt_strfptime |
18 | 19 |
|
19 | 20 | # Max year is only limited by the size of C int. |
20 | 21 | SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4 |
@@ -114,12 +115,13 @@ def test_pthread_getcpuclockid(self): |
114 | 115 | clk_id = time.pthread_getcpuclockid(threading.get_ident()) |
115 | 116 | self.assertTrue(type(clk_id) is int) |
116 | 117 | # when in 32-bit mode AIX only returns the predefined constant |
117 | | - if not platform.system() == "AIX": |
118 | | - self.assertNotEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID) |
119 | | - elif (sys.maxsize.bit_length() > 32): |
120 | | - self.assertNotEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID) |
121 | | - else: |
| 118 | + if platform.system() == "AIX" and (sys.maxsize.bit_length() <= 32): |
| 119 | + self.assertEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID) |
| 120 | + # Solaris returns CLOCK_THREAD_CPUTIME_ID when current thread is given |
| 121 | + elif sys.platform.startswith("sunos"): |
122 | 122 | self.assertEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID) |
| 123 | + else: |
| 124 | + self.assertNotEqual(clk_id, time.CLOCK_THREAD_CPUTIME_ID) |
123 | 125 | t1 = time.clock_gettime(clk_id) |
124 | 126 | t2 = time.clock_gettime(clk_id) |
125 | 127 | self.assertLessEqual(t1, t2) |
@@ -460,8 +462,8 @@ def test_mktime(self): |
460 | 462 | @unittest.skipUnless(platform.libc_ver()[0] != 'glibc', |
461 | 463 | "disabled because of a bug in glibc. Issue #13309") |
462 | 464 | def test_mktime_error(self): |
463 | | - # It may not be possible to reliably make mktime return error |
464 | | - # on all platfom. This will make sure that no other exception |
| 465 | + # It may not be possible to reliably make mktime return an error |
| 466 | + # on all platforms. This will make sure that no other exception |
465 | 467 | # than OverflowError is raised for an extreme value. |
466 | 468 | tt = time.gmtime(self.t) |
467 | 469 | tzname = time.strftime('%Z', tt) |
@@ -570,20 +572,26 @@ def test_localtime_failure(self): |
570 | 572 | self.assertRaises(ValueError, time.ctime, float("nan")) |
571 | 573 |
|
572 | 574 | def test_get_clock_info(self): |
573 | | - clocks = ['monotonic', 'perf_counter', 'process_time', 'time'] |
| 575 | + clocks = [ |
| 576 | + 'monotonic', |
| 577 | + 'perf_counter', |
| 578 | + 'process_time', |
| 579 | + 'time', |
| 580 | + 'thread_time', |
| 581 | + ] |
574 | 582 |
|
575 | 583 | for name in clocks: |
576 | | - info = time.get_clock_info(name) |
577 | | - |
578 | | - #self.assertIsInstance(info, dict) |
579 | | - self.assertIsInstance(info.implementation, str) |
580 | | - self.assertNotEqual(info.implementation, '') |
581 | | - self.assertIsInstance(info.monotonic, bool) |
582 | | - self.assertIsInstance(info.resolution, float) |
583 | | - # 0.0 < resolution <= 1.0 |
584 | | - self.assertGreater(info.resolution, 0.0) |
585 | | - self.assertLessEqual(info.resolution, 1.0) |
586 | | - self.assertIsInstance(info.adjustable, bool) |
| 584 | + with self.subTest(name=name): |
| 585 | + info = time.get_clock_info(name) |
| 586 | + |
| 587 | + self.assertIsInstance(info.implementation, str) |
| 588 | + self.assertNotEqual(info.implementation, '') |
| 589 | + self.assertIsInstance(info.monotonic, bool) |
| 590 | + self.assertIsInstance(info.resolution, float) |
| 591 | + # 0.0 < resolution <= 1.0 |
| 592 | + self.assertGreater(info.resolution, 0.0) |
| 593 | + self.assertLessEqual(info.resolution, 1.0) |
| 594 | + self.assertIsInstance(info.adjustable, bool) |
587 | 595 |
|
588 | 596 | self.assertRaises(ValueError, time.get_clock_info, 'xxx') |
589 | 597 |
|
@@ -863,7 +871,7 @@ def convert_values(ns_timestamps): |
863 | 871 | try: |
864 | 872 | result = pytime_converter(value, time_rnd) |
865 | 873 | expected = expected_func(value) |
866 | | - except Exception as exc: |
| 874 | + except Exception: |
867 | 875 | self.fail("Error on timestamp conversion: %s" % debug_info) |
868 | 876 | self.assertEqual(result, |
869 | 877 | expected, |
@@ -1078,6 +1086,36 @@ def test_object_to_timespec(self): |
1078 | 1086 | with self.assertRaises(ValueError): |
1079 | 1087 | pytime_object_to_timespec(float('nan'), time_rnd) |
1080 | 1088 |
|
| 1089 | +@unittest.skipUnless(sys.platform == "darwin", "test weak linking on macOS") |
| 1090 | +class TestTimeWeaklinking(unittest.TestCase): |
| 1091 | + # These test cases verify that weak linking support on macOS works |
| 1092 | + # as expected. These cases only test new behaviour introduced by weak linking, |
| 1093 | + # regular behaviour is tested by the normal test cases. |
| 1094 | + # |
| 1095 | + # See the section on Weak Linking in Mac/README.txt for more information. |
| 1096 | + def test_clock_functions(self): |
| 1097 | + import sysconfig |
| 1098 | + import platform |
| 1099 | + |
| 1100 | + config_vars = sysconfig.get_config_vars() |
| 1101 | + var_name = "HAVE_CLOCK_GETTIME" |
| 1102 | + if var_name not in config_vars or not config_vars[var_name]: |
| 1103 | + raise unittest.SkipTest(f"{var_name} is not available") |
| 1104 | + |
| 1105 | + mac_ver = tuple(int(x) for x in platform.mac_ver()[0].split(".")) |
| 1106 | + |
| 1107 | + clock_names = [ |
| 1108 | + "CLOCK_MONOTONIC", "clock_gettime", "clock_gettime_ns", "clock_settime", |
| 1109 | + "clock_settime_ns", "clock_getres"] |
| 1110 | + |
| 1111 | + if mac_ver >= (10, 12): |
| 1112 | + for name in clock_names: |
| 1113 | + self.assertTrue(hasattr(time, name), f"time.{name} is not available") |
| 1114 | + |
| 1115 | + else: |
| 1116 | + for name in clock_names: |
| 1117 | + self.assertFalse(hasattr(time, name), f"time.{name} is available") |
| 1118 | + |
1081 | 1119 |
|
1082 | 1120 | if __name__ == "__main__": |
1083 | 1121 | unittest.main() |
0 commit comments