@@ -69,25 +69,6 @@ module time
69
69
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a668a08771581f36]*/
70
70
71
71
72
- #if defined(HAVE_TIMES ) || defined(HAVE_CLOCK )
73
- static int
74
- check_ticks_per_second (long tps , const char * context )
75
- {
76
- /* Effectively, check that _PyTime_MulDiv(t, SEC_TO_NS, tps)
77
- cannot overflow. */
78
- if (tps >= 0 && (_PyTime_t )tps > _PyTime_MAX / SEC_TO_NS ) {
79
- PyErr_Format (PyExc_OverflowError , "%s is too large" , context );
80
- return -1 ;
81
- }
82
- if (tps < 1 ) {
83
- PyErr_Format (PyExc_RuntimeError , "invalid %s" , context );
84
- return -1 ;
85
- }
86
- return 0 ;
87
- }
88
- #endif /* HAVE_TIMES || HAVE_CLOCK */
89
-
90
-
91
72
/* Forward declarations */
92
73
static int pysleep (_PyTime_t timeout );
93
74
@@ -96,11 +77,11 @@ typedef struct {
96
77
PyTypeObject * struct_time_type ;
97
78
#ifdef HAVE_TIMES
98
79
// times() clock frequency in hertz
99
- long ticks_per_second ;
80
+ _PyTimeFraction times_base ;
100
81
#endif
101
82
#ifdef HAVE_CLOCK
102
83
// clock() frequency in hertz
103
- long clocks_per_second ;
84
+ _PyTimeFraction clock_base ;
104
85
#endif
105
86
} time_module_state ;
106
87
@@ -174,10 +155,11 @@ Return the current time in nanoseconds since the Epoch.");
174
155
static int
175
156
py_clock (time_module_state * state , _PyTime_t * tp , _Py_clock_info_t * info )
176
157
{
177
- long clocks_per_second = state -> clocks_per_second ;
158
+ _PyTimeFraction * base = & state -> clock_base ;
159
+
178
160
if (info ) {
179
161
info -> implementation = "clock()" ;
180
- info -> resolution = 1.0 / ( double ) clocks_per_second ;
162
+ info -> resolution = _PyTimeFraction_Resolution ( base ) ;
181
163
info -> monotonic = 1 ;
182
164
info -> adjustable = 0 ;
183
165
}
@@ -189,7 +171,7 @@ py_clock(time_module_state *state, _PyTime_t *tp, _Py_clock_info_t *info)
189
171
"or its value cannot be represented" );
190
172
return -1 ;
191
173
}
192
- _PyTime_t ns = _PyTime_MulDiv (ticks , SEC_TO_NS , clocks_per_second );
174
+ _PyTime_t ns = _PyTimeFraction_Mul (ticks , base );
193
175
* tp = _PyTime_FromNanoseconds (ns );
194
176
return 0 ;
195
177
}
@@ -1257,7 +1239,7 @@ static int
1257
1239
process_time_times (time_module_state * state , _PyTime_t * tp ,
1258
1240
_Py_clock_info_t * info )
1259
1241
{
1260
- long ticks_per_second = state -> ticks_per_second ;
1242
+ _PyTimeFraction * base = & state -> times_base ;
1261
1243
1262
1244
struct tms process ;
1263
1245
if (times (& process ) == (clock_t )- 1 ) {
@@ -1266,14 +1248,14 @@ process_time_times(time_module_state *state, _PyTime_t *tp,
1266
1248
1267
1249
if (info ) {
1268
1250
info -> implementation = "times()" ;
1251
+ info -> resolution = _PyTimeFraction_Resolution (base );
1269
1252
info -> monotonic = 1 ;
1270
1253
info -> adjustable = 0 ;
1271
- info -> resolution = 1.0 / (double )ticks_per_second ;
1272
1254
}
1273
1255
1274
1256
_PyTime_t ns ;
1275
- ns = _PyTime_MulDiv (process .tms_utime , SEC_TO_NS , ticks_per_second );
1276
- ns += _PyTime_MulDiv (process .tms_stime , SEC_TO_NS , ticks_per_second );
1257
+ ns = _PyTimeFraction_Mul (process .tms_utime , base );
1258
+ ns += _PyTimeFraction_Mul (process .tms_stime , base );
1277
1259
* tp = _PyTime_FromNanoseconds (ns );
1278
1260
return 1 ;
1279
1261
}
@@ -1395,8 +1377,7 @@ py_process_time(time_module_state *state, _PyTime_t *tp,
1395
1377
// times() failed, ignore failure
1396
1378
#endif
1397
1379
1398
- /* clock */
1399
- /* Currently, Python 3 requires clock() to build: see issue #22624 */
1380
+ /* clock(). Python 3 requires clock() to build (see gh-66814) */
1400
1381
return py_clock (state , tp , info );
1401
1382
#endif
1402
1383
}
@@ -2110,20 +2091,23 @@ time_exec(PyObject *module)
2110
2091
#endif
2111
2092
2112
2093
#ifdef HAVE_TIMES
2113
- if (_Py_GetTicksPerSecond (& state -> ticks_per_second ) < 0 ) {
2094
+ long ticks_per_second ;
2095
+ if (_Py_GetTicksPerSecond (& ticks_per_second ) < 0 ) {
2114
2096
PyErr_SetString (PyExc_RuntimeError ,
2115
2097
"cannot read ticks_per_second" );
2116
2098
return -1 ;
2117
2099
}
2118
-
2119
- if (check_ticks_per_second (state -> ticks_per_second , "_SC_CLK_TCK" ) < 0 ) {
2100
+ if (_PyTimeFraction_Set (& state -> times_base , SEC_TO_NS ,
2101
+ ticks_per_second ) < 0 ) {
2102
+ PyErr_Format (PyExc_OverflowError , "ticks_per_second is too large" );
2120
2103
return -1 ;
2121
2104
}
2122
2105
#endif
2123
2106
2124
2107
#ifdef HAVE_CLOCK
2125
- state -> clocks_per_second = CLOCKS_PER_SEC ;
2126
- if (check_ticks_per_second (state -> clocks_per_second , "CLOCKS_PER_SEC" ) < 0 ) {
2108
+ if (_PyTimeFraction_Set (& state -> clock_base , SEC_TO_NS ,
2109
+ CLOCKS_PER_SEC ) < 0 ) {
2110
+ PyErr_Format (PyExc_OverflowError , "CLOCKS_PER_SEC is too large" );
2127
2111
return -1 ;
2128
2112
}
2129
2113
#endif
0 commit comments