| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5909a49 commit ae6cd7c
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,22 +164,6 @@ PyAPI_FUNC(_PyTime_t) _PyTime_MulDiv(_PyTime_t ticks, | |||
| 164 | 164 | _PyTime_t mul, | |
| 165 | 165 | _PyTime_t div); | |
| 166 | 166 | ||
| 167 | - /* Get the current time from the system clock. | ||
| 168 | - | ||
| 169 | - The function cannot fail. _PyTime_Init() ensures that the system clock | ||
| 170 | - works. */ | ||
| 171 | - PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void); | ||
| 172 | - | ||
| 173 | - /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. | ||
| 174 | - The clock is not affected by system clock updates. The reference point of | ||
| 175 | - the returned value is undefined, so that only the difference between the | ||
| 176 | - results of consecutive calls is valid. | ||
| 177 | - | ||
| 178 | - The function cannot fail. _PyTime_Init() ensures that a monotonic clock | ||
| 179 | - is available and works. */ | ||
| 180 | - PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void); | ||
| 181 | - | ||
| 182 | - | ||
| 183 | 167 | /* Structure used by time.get_clock_info() */ | |
| 184 | 168 | typedef struct { | |
| 185 | 169 | const char *implementation; | |
@@ -189,13 +173,34 @@ typedef struct { | |||
| 189 | 173 | } _Py_clock_info_t; | |
| 190 | 174 | ||
| 191 | 175 | /* Get the current time from the system clock. | |
| 192 | - * Fill clock information if info is not NULL. | ||
| 193 | - * Raise an exception and return -1 on error, return 0 on success. | ||
| 176 | + | ||
| 177 | + If the internal clock fails, silently ignore the error and return 0. | ||
| 178 | + On integer overflow, silently ignore the overflow and truncated the clock to | ||
| 179 | + _PyTime_MIN or _PyTime_MAX. | ||
| 180 | + | ||
| 181 | + Use _PyTime_GetSystemClockWithInfo() to check for failure. */ | ||
| 182 | + PyAPI_FUNC(_PyTime_t) _PyTime_GetSystemClock(void); | ||
| 183 | + | ||
| 184 | + /* Get the current time from the system clock. | ||
| 185 | + * On success, set *t and *info (if not NULL), and return 0. | ||
| 186 | + * On error, raise an exception and return -1. | ||
| 194 | 187 | */ | |
| 195 | 188 | PyAPI_FUNC(int) _PyTime_GetSystemClockWithInfo( | |
| 196 | 189 | _PyTime_t *t, | |
| 197 | 190 | _Py_clock_info_t *info); | |
| 198 | 191 | ||
| 192 | + /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. | ||
| 193 | + The clock is not affected by system clock updates. The reference point of | ||
| 194 | + the returned value is undefined, so that only the difference between the | ||
| 195 | + results of consecutive calls is valid. | ||
| 196 | + | ||
| 197 | + If the internal clock fails, silently ignore the error and return 0. | ||
| 198 | + On integer overflow, silently ignore the overflow and truncated the clock to | ||
| 199 | + _PyTime_MIN or _PyTime_MAX. | ||
| 200 | + | ||
| 201 | + Use _PyTime_GetMonotonicClockWithInfo() to check for failure. */ | ||
| 202 | + PyAPI_FUNC(_PyTime_t) _PyTime_GetMonotonicClock(void); | ||
| 203 | + | ||
| 199 | 204 | /* Get the time of a monotonic clock, i.e. a clock that cannot go backwards. | |
| 200 | 205 | The clock is not affected by system clock updates. The reference point of | |
| 201 | 206 | the returned value is undefined, so that only the difference between the | |
@@ -209,10 +214,6 @@ PyAPI_FUNC(int) _PyTime_GetMonotonicClockWithInfo( | |||
| 209 | 214 | _Py_clock_info_t *info); | |
| 210 | 215 | ||
| 211 | 216 | ||
| 212 | - /* Initialize time. | ||
| 213 | - Return 0 on success, raise an exception and return -1 on error. */ | ||
| 214 | - PyAPI_FUNC(int) _PyTime_Init(void); | ||
| 215 | - | ||
| 216 | 217 | /* Converts a timestamp to the Gregorian time, using the local time zone. | |
| 217 | 218 | Return 0 on success, raise an exception and return -1 on error. */ | |
| 218 | 219 | PyAPI_FUNC(int) _PyTime_localtime(time_t t, struct tm *tm); | |
@@ -224,8 +225,11 @@ PyAPI_FUNC(int) _PyTime_gmtime(time_t t, struct tm *tm); | |||
| 224 | 225 | /* Get the performance counter: clock with the highest available resolution to | |
| 225 | 226 | measure a short duration. | |
| 226 | 227 | ||
| 227 | - The function cannot fail. _PyTime_Init() ensures that the system clock | ||
| 228 | - works. */ | ||
| 228 | + If the internal clock fails, silently ignore the error and return 0. | ||
| 229 | + On integer overflow, silently ignore the overflow and truncated the clock to | ||
| 230 | + _PyTime_MIN or _PyTime_MAX. | ||
| 231 | + | ||
| 232 | + Use _PyTime_GetPerfCounterWithInfo() to check for failure. */ | ||
| 229 | 233 | PyAPI_FUNC(_PyTime_t) _PyTime_GetPerfCounter(void); | |
| 230 | 234 | ||
| 231 | 235 | /* Get the performance counter: clock with the highest available resolution to | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + :func:`time.time()`, :func:`time.perf_counter()` and | ||
| 2 | + :func:`time.monotonic()` functions can no longer fail with a Python fatal | ||
| 3 | + error, instead raise a regular Python exception on failure. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,7 +51,7 @@ | |||
| 51 | 51 | #define _Py_tzname tzname | |
| 52 | 52 | #endif | |
| 53 | 53 | ||
| 54 | - #if defined(__APPLE__ ) && defined(__has_builtin) | ||
| 54 | + #if defined(__APPLE__ ) && defined(__has_builtin) | ||
| 55 | 55 | # if __has_builtin(__builtin_available) | |
| 56 | 56 | # define HAVE_CLOCK_GETTIME_RUNTIME __builtin_available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) | |
| 57 | 57 | # endif | |
@@ -74,10 +74,21 @@ _PyFloat_FromPyTime(_PyTime_t t) | |||
| 74 | 74 | } | |
| 75 | 75 | ||
| 76 | 76 | ||
| 77 | + static int | ||
| 78 | + get_system_time(_PyTime_t *t) | ||
| 79 | + { | ||
| 80 | + // Avoid _PyTime_GetSystemClock() which silently ignores errors. | ||
| 81 | + return _PyTime_GetSystemClockWithInfo(t, NULL); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + | ||
| 77 | 85 | static PyObject * | |
| 78 | 86 | time_time(PyObject *self, PyObject *unused) | |
| 79 | 87 | { | |
| 80 | - _PyTime_t t = _PyTime_GetSystemClock(); | ||
| 88 | + _PyTime_t t; | ||
| 89 | + if (get_system_time(&t) < 0) { | ||
| 90 | + return NULL; | ||
| 91 | + } | ||
| 81 | 92 | return _PyFloat_FromPyTime(t); | |
| 82 | 93 | } | |
| 83 | 94 | ||
@@ -91,7 +102,10 @@ Fractions of a second may be present if the system clock provides them."); | |||
| 91 | 102 | static PyObject * | |
| 92 | 103 | time_time_ns(PyObject *self, PyObject *unused) | |
| 93 | 104 | { | |
| 94 | - _PyTime_t t = _PyTime_GetSystemClock(); | ||
| 105 | + _PyTime_t t; | ||
| 106 | + if (get_system_time(&t) < 0) { | ||
| 107 | + return NULL; | ||
| 108 | + } | ||
| 95 | 109 | return _PyTime_AsNanosecondsObject(t); | |
| 96 | 110 | } | |
| 97 | 111 | ||
@@ -147,20 +161,11 @@ _PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) | |||
| 147 | 161 | } | |
| 148 | 162 | #endif /* HAVE_CLOCK */ | |
| 149 | 163 | ||
| 150 | - static PyObject* | ||
| 151 | - perf_counter(_Py_clock_info_t *info) | ||
| 152 | - { | ||
| 153 | - _PyTime_t t; | ||
| 154 | - if (_PyTime_GetPerfCounterWithInfo(&t, info) < 0) { | ||
| 155 | - return NULL; | ||
| 156 | - } | ||
| 157 | - return _PyFloat_FromPyTime(t); | ||
| 158 | - } | ||
| 159 | 164 | ||
| 160 | 165 | #ifdef HAVE_CLOCK_GETTIME | |
| 161 | 166 | ||
| 162 | 167 | #ifdef __APPLE__ | |
| 163 | - /* | ||
| 168 | + /* | ||
| 164 | 169 | * The clock_* functions will be removed from the module | |
| 165 | 170 | * dict entirely when the C API is not available. | |
| 166 | 171 | */ | |
@@ -1096,10 +1101,22 @@ the local timezone used by methods such as localtime, but this behaviour\n\ | |||
| 1096 | 1101 | should not be relied on."); | |
| 1097 | 1102 | #endif /* HAVE_WORKING_TZSET */ | |
| 1098 | 1103 | ||
| 1104 | + | ||
| 1105 | + static int | ||
| 1106 | + get_monotonic(_PyTime_t *t) | ||
| 1107 | + { | ||
| 1108 | + // Avoid _PyTime_GetMonotonicClock() which silently ignores errors. | ||
| 1109 | + return _PyTime_GetMonotonicClockWithInfo(t, NULL); | ||
| 1110 | + } | ||
| 1111 | + | ||
| 1112 | + | ||
| 1099 | 1113 | static PyObject * | |
| 1100 | 1114 | time_monotonic(PyObject *self, PyObject *unused) | |
| 1101 | 1115 | { | |
| 1102 | - _PyTime_t t = _PyTime_GetMonotonicClock(); | ||
| 1116 | + _PyTime_t t; | ||
| 1117 | + if (get_monotonic(&t) < 0) { | ||
| 1118 | + return NULL; | ||
| 1119 | + } | ||
| 1103 | 1120 | return _PyFloat_FromPyTime(t); | |
| 1104 | 1121 | } | |
| 1105 | 1122 | ||
@@ -1111,7 +1128,10 @@ Monotonic clock, cannot go backward."); | |||
| 1111 | 1128 | static PyObject * | |
| 1112 | 1129 | time_monotonic_ns(PyObject *self, PyObject *unused) | |
| 1113 | 1130 | { | |
| 1114 | - _PyTime_t t = _PyTime_GetMonotonicClock(); | ||
| 1131 | + _PyTime_t t; | ||
| 1132 | + if (get_monotonic(&t) < 0) { | ||
| 1133 | + return NULL; | ||
| 1134 | + } | ||
| 1115 | 1135 | return _PyTime_AsNanosecondsObject(t); | |
| 1116 | 1136 | } | |
| 1117 | 1137 | ||
@@ -1120,21 +1140,38 @@ PyDoc_STRVAR(monotonic_ns_doc, | |||
| 1120 | 1140 | \n\ | |
| 1121 | 1141 | Monotonic clock, cannot go backward, as nanoseconds."); | |
| 1122 | 1142 | ||
| 1143 | + | ||
| 1144 | + static int | ||
| 1145 | + get_perf_counter(_PyTime_t *t) | ||
| 1146 | + { | ||
| 1147 | + // Avoid _PyTime_GetPerfCounter() which silently ignores errors. | ||
| 1148 | + return _PyTime_GetPerfCounterWithInfo(t, NULL); | ||
| 1149 | + } | ||
| 1150 | + | ||
| 1151 | + | ||
| 1123 | 1152 | static PyObject * | |
| 1124 | 1153 | time_perf_counter(PyObject *self, PyObject *unused) | |
| 1125 | 1154 | { | |
| 1126 | - return perf_counter(NULL); | ||
| 1155 | + _PyTime_t t; | ||
| 1156 | + if (get_perf_counter(&t) < 0) { | ||
| 1157 | + return NULL; | ||
| 1158 | + } | ||
| 1159 | + return _PyFloat_FromPyTime(t); | ||
| 1127 | 1160 | } | |
| 1128 | 1161 | ||
| 1129 | 1162 | PyDoc_STRVAR(perf_counter_doc, | |
| 1130 | 1163 | "perf_counter() -> float\n\ | |
| 1131 | 1164 | \n\ | |
| 1132 | 1165 | Performance counter for benchmarking."); | |
| 1133 | 1166 | ||
| 1167 | + | ||
| 1134 | 1168 | static PyObject * | |
| 1135 | 1169 | time_perf_counter_ns(PyObject *self, PyObject *unused) | |
| 1136 | 1170 | { | |
| 1137 | - _PyTime_t t = _PyTime_GetPerfCounter(); | ||
| 1171 | + _PyTime_t t; | ||
| 1172 | + if (get_perf_counter(&t) < 0) { | ||
| 1173 | + return NULL; | ||
| 1174 | + } | ||
| 1138 | 1175 | return _PyTime_AsNanosecondsObject(t); | |
| 1139 | 1176 | } | |
| 1140 | 1177 | ||
@@ -1421,7 +1458,7 @@ _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) | |||
| 1421 | 1458 | ||
| 1422 | 1459 | #if defined(__APPLE__) && defined(__has_attribute) && __has_attribute(availability) | |
| 1423 | 1460 | static int | |
| 1424 | - _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) | ||
| 1461 | + _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) | ||
| 1425 | 1462 | __attribute__((availability(macos, introduced=10.12))) | |
| 1426 | 1463 | __attribute__((availability(ios, introduced=10.0))) | |
| 1427 | 1464 | __attribute__((availability(tvos, introduced=10.0))) | |
@@ -1460,7 +1497,7 @@ _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) | |||
| 1460 | 1497 | ||
| 1461 | 1498 | #ifdef HAVE_THREAD_TIME | |
| 1462 | 1499 | #ifdef __APPLE__ | |
| 1463 | - /* | ||
| 1500 | + /* | ||
| 1464 | 1501 | * The clock_* functions will be removed from the module | |
| 1465 | 1502 | * dict entirely when the C API is not available. | |
| 1466 | 1503 | */ | |
@@ -2025,7 +2062,10 @@ pysleep(_PyTime_t secs) | |||
| 2025 | 2062 | HANDLE hInterruptEvent; | |
| 2026 | 2063 | #endif | |
| 2027 | 2064 | ||
| 2028 | - deadline = _PyTime_GetMonotonicClock() + secs; | ||
| 2065 | + if (get_monotonic(&monotonic) < 0) { | ||
| 2066 | + return -1; | ||
| 2067 | + } | ||
| 2068 | + deadline = monotonic + secs; | ||
| 2029 | 2069 | ||
| 2030 | 2070 | do { | |
| 2031 | 2071 | #ifndef MS_WINDOWS | |
@@ -2077,10 +2117,13 @@ pysleep(_PyTime_t secs) | |||
| 2077 | 2117 | if (PyErr_CheckSignals()) | |
| 2078 | 2118 | return -1; | |
| 2079 | 2119 | ||
| 2080 | - monotonic = _PyTime_GetMonotonicClock(); | ||
| 2120 | + if (get_monotonic(&monotonic) < 0) { | ||
| 2121 | + return -1; | ||
| 2122 | + } | ||
| 2081 | 2123 | secs = deadline - monotonic; | |
| 2082 | - if (secs < 0) | ||
| 2124 | + if (secs < 0) { | ||
| 2083 | 2125 | break; | |
| 2126 | + } | ||
| 2084 | 2127 | /* retry with the recomputed delay */ | |
| 2085 | 2128 | } while (1); | |
| 2086 | 2129 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -763,12 +763,6 @@ pycore_interp_init(PyThreadState *tstate) | |||
| 763 | 763 | goto done; | |
| 764 | 764 | } | |
| 765 | 765 | ||
| 766 | - if (_Py_IsMainInterpreter(tstate)) { | ||
| 767 | - if (_PyTime_Init() < 0) { | ||
| 768 | - return _PyStatus_ERR("can't initialize time"); | ||
| 769 | - } | ||
| 770 | - } | ||
| 771 | - | ||
| 772 | 766 | status = _PySys_Create(tstate, &sysmod); | |
| 773 | 767 | if (_PyStatus_EXCEPTION(status)) { | |
| 774 | 768 | goto done; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments