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
0 commit comments