mirror of
https://github.com/python/cpython.git
synced 2025-08-03 08:34:29 +00:00
bpo-21302: Add _PyTime_AsNanoseconds() (GH-28350)
Refactor pytime.c: * Add pytime_from_nanoseconds() and pytime_as_nanoseconds(), and use explicitly these functions * Add two empty lines between functions * PEP 7: add braces { ... } * C99: declare variables where they are set * Rename private functions to lowercase * Rename error_time_t_overflow() to pytime_time_t_overflow() * Rename win_perf_counter_frequency() to py_win_perf_counter_frequency() * py_get_monotonic_clock(): add an assertion to detect overflow when mach_absolute_time() unsigned uint64_t is casted to _PyTime_t (signed int64_t). _testcapi: use _PyTime_FromNanoseconds().
This commit is contained in:
parent
40d2ac92f9
commit
b49263b698
4 changed files with 233 additions and 171 deletions
|
@ -4623,11 +4623,10 @@ static PyObject *
|
|||
test_pytime_fromseconds(PyObject *self, PyObject *args)
|
||||
{
|
||||
int seconds;
|
||||
_PyTime_t ts;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "i", &seconds))
|
||||
if (!PyArg_ParseTuple(args, "i", &seconds)) {
|
||||
return NULL;
|
||||
ts = _PyTime_FromSeconds(seconds);
|
||||
}
|
||||
_PyTime_t ts = _PyTime_FromSeconds(seconds);
|
||||
return _PyTime_AsNanosecondsObject(ts);
|
||||
}
|
||||
|
||||
|
@ -4636,14 +4635,16 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
|
|||
{
|
||||
PyObject *obj;
|
||||
int round;
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_time_rounding(round) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
_PyTime_t ts;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
|
||||
return NULL;
|
||||
if (check_time_rounding(round) < 0)
|
||||
return NULL;
|
||||
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1)
|
||||
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
|
||||
return NULL;
|
||||
}
|
||||
return _PyTime_AsNanosecondsObject(ts);
|
||||
}
|
||||
|
||||
|
@ -4651,16 +4652,14 @@ static PyObject *
|
|||
test_pytime_assecondsdouble(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *obj;
|
||||
_PyTime_t ts;
|
||||
double d;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &obj)) {
|
||||
return NULL;
|
||||
}
|
||||
_PyTime_t ts;
|
||||
if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
d = _PyTime_AsSecondsDouble(ts);
|
||||
double d = _PyTime_AsSecondsDouble(ts);
|
||||
return PyFloat_FromDouble(d);
|
||||
}
|
||||
|
||||
|
@ -4669,23 +4668,22 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
|
|||
{
|
||||
PyObject *obj;
|
||||
int round;
|
||||
_PyTime_t t;
|
||||
struct timeval tv;
|
||||
PyObject *seconds;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_time_rounding(round) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
_PyTime_t t;
|
||||
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
struct timeval tv;
|
||||
if (_PyTime_AsTimeval(t, &tv, round) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
seconds = PyLong_FromLongLong(tv.tv_sec);
|
||||
PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
|
||||
if (seconds == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -4697,15 +4695,14 @@ static PyObject *
|
|||
test_PyTime_AsTimespec(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *obj;
|
||||
_PyTime_t t;
|
||||
struct timespec ts;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "O", &obj)) {
|
||||
return NULL;
|
||||
}
|
||||
_PyTime_t t;
|
||||
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
struct timespec ts;
|
||||
if (_PyTime_AsTimespec(t, &ts) == -1) {
|
||||
return NULL;
|
||||
}
|
||||
|
@ -4718,21 +4715,19 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
|
|||
{
|
||||
PyObject *obj;
|
||||
int round;
|
||||
_PyTime_t t, ms;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
|
||||
return NULL;
|
||||
}
|
||||
_PyTime_t t;
|
||||
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (check_time_rounding(round) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
ms = _PyTime_AsMilliseconds(t, round);
|
||||
/* This conversion rely on the fact that _PyTime_t is a number of
|
||||
nanoseconds */
|
||||
return _PyTime_AsNanosecondsObject(ms);
|
||||
_PyTime_t ms = _PyTime_AsMilliseconds(t, round);
|
||||
_PyTime_t ns = _PyTime_FromNanoseconds(ms);
|
||||
return _PyTime_AsNanosecondsObject(ns);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
|
@ -4740,20 +4735,19 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
|
|||
{
|
||||
PyObject *obj;
|
||||
int round;
|
||||
_PyTime_t t, ms;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round))
|
||||
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
|
||||
return NULL;
|
||||
}
|
||||
_PyTime_t t;
|
||||
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
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 */
|
||||
return _PyTime_AsNanosecondsObject(ms);
|
||||
_PyTime_t us = _PyTime_AsMicroseconds(t, round);
|
||||
_PyTime_t ns = _PyTime_FromNanoseconds(us);
|
||||
return _PyTime_AsNanosecondsObject(ns);
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue