1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include "parts.h"
static int
pytime_from_nanoseconds(PyTime_t *tp, PyObject *obj)
{
if (!PyLong_Check(obj)) {
PyErr_Format(PyExc_TypeError, "expect int, got %s",
Py_TYPE(obj)->tp_name);
return -1;
}
long long nsec = PyLong_AsLongLong(obj);
if (nsec == -1 && PyErr_Occurred()) {
return -1;
}
Py_BUILD_ASSERT(sizeof(long long) == sizeof(PyTime_t));
*tp = (PyTime_t)nsec;
return 0;
}
static PyObject *
test_pytime_assecondsdouble(PyObject *Py_UNUSED(self), PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
PyTime_t ts;
if (pytime_from_nanoseconds(&ts, obj) < 0) {
return NULL;
}
double d = PyTime_AsSecondsDouble(ts);
return PyFloat_FromDouble(d);
}
static PyObject*
pytime_as_float(PyTime_t t)
{
return PyFloat_FromDouble(PyTime_AsSecondsDouble(t));
}
static PyObject*
test_pytime_monotonic(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
int res = PyTime_Monotonic(&t);
if (res < 0) {
assert(t == 0);
return NULL;
}
assert(res == 0);
return pytime_as_float(t);
}
static PyObject*
test_pytime_monotonic_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
int res;
Py_BEGIN_ALLOW_THREADS
res = PyTime_MonotonicRaw(&t);
Py_END_ALLOW_THREADS
if (res < 0) {
assert(t == 0);
PyErr_SetString(PyExc_RuntimeError, "PyTime_MonotonicRaw() failed");
return NULL;
}
assert(res == 0);
return pytime_as_float(t);
}
static PyObject*
test_pytime_perf_counter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
int res = PyTime_PerfCounter(&t);
if (res < 0) {
assert(t == 0);
return NULL;
}
assert(res == 0);
return pytime_as_float(t);
}
static PyObject*
test_pytime_perf_counter_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
int res;
Py_BEGIN_ALLOW_THREADS
res = PyTime_PerfCounterRaw(&t);
Py_END_ALLOW_THREADS
if (res < 0) {
assert(t == 0);
PyErr_SetString(PyExc_RuntimeError, "PyTime_PerfCounterRaw() failed");
return NULL;
}
assert(res == 0);
return pytime_as_float(t);
}
static PyObject*
test_pytime_time(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
int res = PyTime_Time(&t);
if (res < 0) {
assert(t == 0);
return NULL;
}
assert(res == 0);
return pytime_as_float(t);
}
static PyObject*
test_pytime_time_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
int res;
Py_BEGIN_ALLOW_THREADS
res = PyTime_TimeRaw(&t);
Py_END_ALLOW_THREADS
if (res < 0) {
assert(t == 0);
PyErr_SetString(PyExc_RuntimeError, "PyTime_TimeRaw() failed");
return NULL;
}
assert(res == 0);
return pytime_as_float(t);
}
static PyMethodDef test_methods[] = {
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
{"PyTime_Monotonic", test_pytime_monotonic, METH_NOARGS},
{"PyTime_MonotonicRaw", test_pytime_monotonic_raw, METH_NOARGS},
{"PyTime_PerfCounter", test_pytime_perf_counter, METH_NOARGS},
{"PyTime_PerfCounterRaw", test_pytime_perf_counter_raw, METH_NOARGS},
{"PyTime_Time", test_pytime_time, METH_NOARGS},
{"PyTime_TimeRaw", test_pytime_time_raw, METH_NOARGS},
{NULL},
};
int
_PyTestCapi_Init_Time(PyObject *m)
{
if (PyModule_AddFunctions(m, test_methods) < 0) {
return -1;
}
Py_BUILD_ASSERT(sizeof(long long) == sizeof(PyTime_t));
if (PyModule_AddObject(m, "PyTime_MIN", PyLong_FromLongLong(PyTime_MIN)) < 0) {
return 1;
}
if (PyModule_AddObject(m, "PyTime_MAX", PyLong_FromLongLong(PyTime_MAX)) < 0) {
return 1;
}
return 0;
}
|