Debian Codegraph @

Modules/_testcapi/time.c

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