Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove PyTime_AsNanoseconds
  • Loading branch information
encukou committed Feb 12, 2024
commit ac94566de27c941df9dac9a435ab3adc068652b1
13 changes: 0 additions & 13 deletions Include/cpython/pytime.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,10 @@ typedef PY_INT64_T PyTime_t;
#define PyTime_MAX INT64_MAX

PyAPI_FUNC(double) PyTime_AsSecondsDouble(PyTime_t t);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: i suggest to first declare functions "creating" PyTime_t values, and then in second just (just add an empty line) functions to "convert" PyTime_t values.

PyAPI_FUNC(int) PyTime_AsNanoseconds(PyTime_t t, PY_INT64_T *result);
PyAPI_FUNC(int) PyTime_Monotonic(PyTime_t *result);
PyAPI_FUNC(int) PyTime_PerfCounter(PyTime_t *result);
PyAPI_FUNC(int) PyTime_Time(PyTime_t *result);

static inline int
_PyTime_AsNanoseconds_impl(PyTime_t t, PY_INT64_T *result)
{
// Implementation detail: PyTime_t is already a int64_t in nanoseconds.
// This may change. We might e.g. extend PyTime_t to 128 bits, then
// this function can start raising OverflowError.
*result = t;
return 0;
}

#define PyTime_AsNanoseconds _PyTime_AsNanoseconds_impl

#ifdef __cplusplus
}
#endif
Expand Down
26 changes: 7 additions & 19 deletions Lib/test/test_capi/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
DAY_TO_SEC = (24 * 60 * 60)
# Worst clock resolution: maximum delta between two clock reads.
CLOCK_RES = 0.050
CLOCK_RES_NS = int(CLOCK_RES * SEC_TO_NS)


class CAPITest(unittest.TestCase):
Expand All @@ -19,15 +18,10 @@ def test_min_max(self):
self.assertEqual(PyTime_MIN, -2**63)
self.assertEqual(PyTime_MAX, 2**63 - 1)

def check_clock(self, c_func, py_func, c_func_ns, py_func_ns):
t_c = c_func()
t_py = py_func()
t_c_ns = c_func_ns()
t_py_ns = py_func_ns()
self.assertAlmostEqual(t_c, t_py, delta=CLOCK_RES)
self.assertAlmostEqual(t_c_ns, t_py_ns, delta=CLOCK_RES_NS)
self.assertAlmostEqual(t_c * SEC_TO_NS, t_c_ns, delta=CLOCK_RES_NS)
self.assertAlmostEqual(t_py * SEC_TO_NS, t_py_ns, delta=CLOCK_RES_NS)
def check_clock(self, c_func, py_func):
t1 = c_func()
t2 = py_func()
self.assertAlmostEqual(t1, t2, delta=CLOCK_RES)

def test_assecondsdouble(self):
# Test PyTime_AsSecondsDouble()
Expand Down Expand Up @@ -66,18 +60,12 @@ def ns_to_sec(ns):

def test_monotonic(self):
# Test PyTime_Monotonic()
self.check_clock(
_testcapi.PyTime_Monotonic, time.monotonic,
_testcapi.PyTime_Monotonic_ns, time.monotonic_ns)
self.check_clock(_testcapi.PyTime_Monotonic, time.monotonic)

def test_perf_counter(self):
# Test PyTime_PerfCounter()
self.check_clock(
_testcapi.PyTime_PerfCounter, time.perf_counter,
_testcapi.PyTime_PerfCounter_ns, time.perf_counter_ns)
self.check_clock(_testcapi.PyTime_PerfCounter, time.perf_counter)

def test_time(self):
# Test PyTime_time()
self.check_clock(
_testcapi.PyTime_Time, time.time,
_testcapi.PyTime_Time_ns, time.time_ns)
self.check_clock(_testcapi.PyTime_Time, time.time)
49 changes: 0 additions & 49 deletions Modules/_testcapi/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,61 +79,12 @@ test_pytime_time(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
return pytime_as_float(t);
}

static PyObject*
pytime_as_ns(PyTime_t t)
{
assert(sizeof(long long) >= sizeof(PyTime_t));
long long result;
if (PyTime_AsNanoseconds(t, &result) < 0) {
return NULL;
}
return PyLong_FromLongLong(result);
}


static PyObject*
test_pytime_monotonic_ns(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
if (PyTime_Monotonic(&t) < 0) {
return NULL;
}
return pytime_as_ns(t);
}


static PyObject*
test_pytime_perf_counter_ns(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
if (PyTime_PerfCounter(&t) < 0) {
return NULL;
}
return pytime_as_ns(t);
}


static PyObject*
test_pytime_time_ns(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
{
PyTime_t t;
if (PyTime_Time(&t) < 0) {
printf("ERR! %d\n", (int)t);
return NULL;
}
printf("... %d\n", (int)t);
return pytime_as_ns(t);
}


static PyMethodDef test_methods[] = {
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
{"PyTime_Monotonic", test_pytime_monotonic, METH_NOARGS},
{"PyTime_PerfCounter", test_pytime_perf_counter, METH_NOARGS},
{"PyTime_Time", test_pytime_time, METH_NOARGS},
Comment thread
encukou marked this conversation as resolved.
Resolved
{"PyTime_Monotonic_ns", test_pytime_monotonic_ns, METH_NOARGS},
{"PyTime_PerfCounter_ns", test_pytime_perf_counter_ns, METH_NOARGS},
{"PyTime_Time_ns", test_pytime_time_ns, METH_NOARGS},
{NULL},
};

Expand Down
8 changes: 0 additions & 8 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1433,11 +1433,3 @@ _PyDeadline_Get(_PyTime_t deadline)
NOEXC_WRAPPER(_PyTime_GetPerfCounter, PyTime_PerfCounter)
NOEXC_WRAPPER(_PyTime_GetMonotonicClock, PyTime_Monotonic)
NOEXC_WRAPPER(_PyTime_GetSystemClock, PyTime_Time)

// Export PyTime_AsNanoseconds from libpython
#undef PyTime_AsNanoseconds
int
PyTime_AsNanoseconds(PyTime_t t, int64_t *result)
{
return _PyTime_AsNanoseconds_impl(t, result);
}