|
63 | 63 | #define SEC_TO_NS (1000 * 1000 * 1000)
|
64 | 64 |
|
65 | 65 |
|
| 66 | +/*[clinic input] |
| 67 | +module time |
| 68 | +[clinic start generated code]*/ |
| 69 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a668a08771581f36]*/ |
| 70 | + |
| 71 | + |
66 | 72 | #if defined(HAVE_TIMES) || defined(HAVE_CLOCK)
|
67 | 73 | static int
|
68 | 74 | check_ticks_per_second(long tps, const char *context)
|
@@ -227,62 +233,85 @@ _PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
|
227 | 233 | #pragma clang diagnostic ignored "-Wunguarded-availability"
|
228 | 234 | #endif
|
229 | 235 |
|
230 |
| -static PyObject * |
231 |
| -time_clock_gettime(PyObject *self, PyObject *args) |
| 236 | +static int |
| 237 | +time_clockid_converter(PyObject *obj, clockid_t *p) |
232 | 238 | {
|
233 |
| - int ret; |
234 |
| - struct timespec tp; |
235 |
| - |
236 | 239 | #if defined(_AIX) && (SIZEOF_LONG == 8)
|
237 |
| - long clk_id; |
238 |
| - if (!PyArg_ParseTuple(args, "l:clock_gettime", &clk_id)) { |
| 240 | + long clk_id = PyLong_AsLong(obj); |
239 | 241 | #else
|
240 |
| - int clk_id; |
241 |
| - if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) { |
| 242 | + int clk_id = PyLong_AsInt(obj); |
242 | 243 | #endif
|
243 |
| - return NULL; |
| 244 | + if (clk_id == -1 && PyErr_Occurred()) { |
| 245 | + PyErr_Format(PyExc_TypeError, |
| 246 | + "clk_id should be integer, not %s", |
| 247 | + _PyType_Name(Py_TYPE(obj))); |
| 248 | + return 0; |
244 | 249 | }
|
245 | 250 |
|
246 |
| - ret = clock_gettime((clockid_t)clk_id, &tp); |
| 251 | + // Make sure that we picked the right type (check sizes type) |
| 252 | + Py_BUILD_ASSERT(sizeof(clk_id) == sizeof(*p)); |
| 253 | + *p = (clockid_t)clk_id; |
| 254 | + return 1; |
| 255 | +} |
| 256 | + |
| 257 | +/*[python input] |
| 258 | +
|
| 259 | +class clockid_t_converter(CConverter): |
| 260 | + type = "clockid_t" |
| 261 | + converter = 'time_clockid_converter' |
| 262 | +
|
| 263 | +[python start generated code]*/ |
| 264 | +/*[python end generated code: output=da39a3ee5e6b4b0d input=53867111501f46c8]*/ |
| 265 | + |
| 266 | + |
| 267 | +/*[clinic input] |
| 268 | +time.clock_gettime |
| 269 | +
|
| 270 | + clk_id: clockid_t |
| 271 | + / |
| 272 | +
|
| 273 | +Return the time of the specified clock clk_id as a float. |
| 274 | +[clinic start generated code]*/ |
| 275 | + |
| 276 | +static PyObject * |
| 277 | +time_clock_gettime_impl(PyObject *module, clockid_t clk_id) |
| 278 | +/*[clinic end generated code: output=832b9ebc03328020 input=7e89fcc42ca15e5d]*/ |
| 279 | +{ |
| 280 | + struct timespec tp; |
| 281 | + int ret = clock_gettime(clk_id, &tp); |
247 | 282 | if (ret != 0) {
|
248 | 283 | PyErr_SetFromErrno(PyExc_OSError);
|
249 | 284 | return NULL;
|
250 | 285 | }
|
251 | 286 | return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
|
252 | 287 | }
|
253 | 288 |
|
254 |
| -PyDoc_STRVAR(clock_gettime_doc, |
255 |
| -"clock_gettime(clk_id) -> float\n\ |
256 |
| -\n\ |
257 |
| -Return the time of the specified clock clk_id."); |
| 289 | +/*[clinic input] |
| 290 | +time.clock_gettime_ns |
| 291 | +
|
| 292 | + clk_id: clockid_t |
| 293 | + / |
| 294 | +
|
| 295 | +Return the time of the specified clock clk_id as nanoseconds (int). |
| 296 | +[clinic start generated code]*/ |
258 | 297 |
|
259 | 298 | static PyObject *
|
260 |
| -time_clock_gettime_ns(PyObject *self, PyObject *args) |
| 299 | +time_clock_gettime_ns_impl(PyObject *module, clockid_t clk_id) |
| 300 | +/*[clinic end generated code: output=4a045c3a36e60044 input=aabc248db8c8e3e5]*/ |
261 | 301 | {
|
262 |
| - int ret; |
263 |
| - int clk_id; |
264 | 302 | struct timespec ts;
|
265 |
| - _PyTime_t t; |
266 |
| - |
267 |
| - if (!PyArg_ParseTuple(args, "i:clock_gettime", &clk_id)) { |
268 |
| - return NULL; |
269 |
| - } |
270 |
| - |
271 |
| - ret = clock_gettime((clockid_t)clk_id, &ts); |
| 303 | + int ret = clock_gettime(clk_id, &ts); |
272 | 304 | if (ret != 0) {
|
273 | 305 | PyErr_SetFromErrno(PyExc_OSError);
|
274 | 306 | return NULL;
|
275 | 307 | }
|
| 308 | + |
| 309 | + _PyTime_t t; |
276 | 310 | if (_PyTime_FromTimespec(&t, &ts) < 0) {
|
277 | 311 | return NULL;
|
278 | 312 | }
|
279 | 313 | return _PyTime_AsNanosecondsObject(t);
|
280 | 314 | }
|
281 |
| - |
282 |
| -PyDoc_STRVAR(clock_gettime_ns_doc, |
283 |
| -"clock_gettime_ns(clk_id) -> int\n\ |
284 |
| -\n\ |
285 |
| -Return the time of the specified clock clk_id as nanoseconds."); |
286 | 315 | #endif /* HAVE_CLOCK_GETTIME */
|
287 | 316 |
|
288 | 317 | #ifdef HAVE_CLOCK_SETTIME
|
@@ -1857,12 +1886,16 @@ init_timezone(PyObject *m)
|
1857 | 1886 | }
|
1858 | 1887 |
|
1859 | 1888 |
|
| 1889 | +// Include Argument Clinic code after defining converters such as |
| 1890 | +// time_clockid_converter(). |
| 1891 | +#include "clinic/timemodule.c.h" |
| 1892 | + |
1860 | 1893 | static PyMethodDef time_methods[] = {
|
1861 | 1894 | {"time", time_time, METH_NOARGS, time_doc},
|
1862 | 1895 | {"time_ns", time_time_ns, METH_NOARGS, time_ns_doc},
|
1863 | 1896 | #ifdef HAVE_CLOCK_GETTIME
|
1864 |
| - {"clock_gettime", time_clock_gettime, METH_VARARGS, clock_gettime_doc}, |
1865 |
| - {"clock_gettime_ns",time_clock_gettime_ns, METH_VARARGS, clock_gettime_ns_doc}, |
| 1897 | + TIME_CLOCK_GETTIME_METHODDEF |
| 1898 | + TIME_CLOCK_GETTIME_NS_METHODDEF |
1866 | 1899 | #endif
|
1867 | 1900 | #ifdef HAVE_CLOCK_SETTIME
|
1868 | 1901 | {"clock_settime", time_clock_settime, METH_VARARGS, clock_settime_doc},
|
|
0 commit comments