diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-11-02 14:28:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-02 14:28:27 (GMT) |
commit | c29b585fd4b5a91d17fc5dd41d86edff28a30da3 (patch) | |
tree | aadcf238ccc1867d7adefc079081781424e3e62c /Modules/_testcapimodule.c | |
parent | e314853d57450b2b9523157eebd405289a795a0e (diff) | |
download | cpython-c29b585fd4b5a91d17fc5dd41d86edff28a30da3.zip cpython-c29b585fd4b5a91d17fc5dd41d86edff28a30da3.tar.gz cpython-c29b585fd4b5a91d17fc5dd41d86edff28a30da3.tar.bz2 |
bpo-31784: Implement PEP 564: add time.time_ns() (#3989)
Add new time functions:
* time.clock_gettime_ns()
* time.clock_settime_ns()
* time.monotonic_ns()
* time.perf_counter_ns()
* time.process_time_ns()
* time.time_ns()
Add new _PyTime functions:
* _PyTime_FromTimespec()
* _PyTime_FromNanosecondsObject()
* _PyTime_FromTimeval()
Other changes:
* Add also os.times() tests to test_os.
* pytime_fromtimeval() and pytime_fromtimeval() now return
_PyTime_MAX or _PyTime_MIN on overflow, rather than undefined
behaviour
* _PyTime_FromNanoseconds() parameter type changes from long long to
_PyTime_t
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 1f71a09..5210809 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3946,13 +3946,16 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args) static PyObject * test_pytime_assecondsdouble(PyObject *self, PyObject *args) { - long long ns; + PyObject *obj; _PyTime_t ts; double d; - if (!PyArg_ParseTuple(args, "L", &ns)) + if (!PyArg_ParseTuple(args, "O", &obj)) { + return NULL; + } + if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) { return NULL; - ts = _PyTime_FromNanoseconds(ns); + } d = _PyTime_AsSecondsDouble(ts); return PyFloat_FromDouble(d); } @@ -3960,23 +3963,28 @@ test_pytime_assecondsdouble(PyObject *self, PyObject *args) static PyObject * test_PyTime_AsTimeval(PyObject *self, PyObject *args) { - long long ns; + PyObject *obj; int round; _PyTime_t t; struct timeval tv; PyObject *seconds; - if (!PyArg_ParseTuple(args, "Li", &ns, &round)) + if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) return NULL; - if (check_time_rounding(round) < 0) + if (check_time_rounding(round) < 0) { return NULL; - t = _PyTime_FromNanoseconds(ns); - if (_PyTime_AsTimeval(t, &tv, round) < 0) + } + if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { return NULL; + } + if (_PyTime_AsTimeval(t, &tv, round) < 0) { + return NULL; + } seconds = PyLong_FromLongLong(tv.tv_sec); - if (seconds == NULL) + if (seconds == NULL) { return NULL; + } return Py_BuildValue("Nl", seconds, tv.tv_usec); } @@ -3984,15 +3992,19 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args) static PyObject * test_PyTime_AsTimespec(PyObject *self, PyObject *args) { - long long ns; + PyObject *obj; _PyTime_t t; struct timespec ts; - if (!PyArg_ParseTuple(args, "L", &ns)) + if (!PyArg_ParseTuple(args, "O", &obj)) { return NULL; - t = _PyTime_FromNanoseconds(ns); - if (_PyTime_AsTimespec(t, &ts) == -1) + } + if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { return NULL; + } + if (_PyTime_AsTimespec(t, &ts) == -1) { + return NULL; + } return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec); } #endif @@ -4000,15 +4012,19 @@ test_PyTime_AsTimespec(PyObject *self, PyObject *args) static PyObject * test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) { - long long ns; + PyObject *obj; int round; _PyTime_t t, ms; - if (!PyArg_ParseTuple(args, "Li", &ns, &round)) + if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) { return NULL; - if (check_time_rounding(round) < 0) + } + if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { + return NULL; + } + if (check_time_rounding(round) < 0) { return NULL; - t = _PyTime_FromNanoseconds(ns); + } ms = _PyTime_AsMilliseconds(t, round); /* This conversion rely on the fact that _PyTime_t is a number of nanoseconds */ @@ -4018,15 +4034,18 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args) static PyObject * test_PyTime_AsMicroseconds(PyObject *self, PyObject *args) { - long long ns; + PyObject *obj; int round; _PyTime_t t, ms; - if (!PyArg_ParseTuple(args, "Li", &ns, &round)) + if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) return NULL; - if (check_time_rounding(round) < 0) + if (_PyTime_FromNanosecondsObject(&t, obj) < 0) { return NULL; - t = _PyTime_FromNanoseconds(ns); + } + if (check_time_rounding(round) < 0) { + return NULL; + } ms = _PyTime_AsMicroseconds(t, round); /* This conversion rely on the fact that _PyTime_t is a number of nanoseconds */ |