diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-03-28 00:26:47 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-03-28 00:26:47 (GMT) |
commit | 95e9cef6f023a1cf365f2f02775badb3a6ac0d82 (patch) | |
tree | 721cb2ab9fbba707888bc7e90b49758ffd5156ed /Modules | |
parent | b7df3144ef14ec50650dfd47da4ba09ee0bc674c (diff) | |
download | cpython-95e9cef6f023a1cf365f2f02775badb3a6ac0d82.zip cpython-95e9cef6f023a1cf365f2f02775badb3a6ac0d82.tar.gz cpython-95e9cef6f023a1cf365f2f02775badb3a6ac0d82.tar.bz2 |
Issue #22117: Write unit tests for _PyTime_AsTimeval()
* _PyTime_AsTimeval() now ensures that tv_usec is always positive
* _PyTime_AsTimespec() now ensures that tv_nsec is always positive
* _PyTime_AsTimeval() now returns an integer on overflow instead of raising an
exception
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapimodule.c | 31 | ||||
-rw-r--r-- | Modules/timemodule.c | 5 |
2 files changed, 35 insertions, 1 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 5029105..4503dc3 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -14,6 +14,10 @@ #include "marshal.h" #include <signal.h> +#ifdef MS_WINDOWS +# include <winsock2.h> +#endif + #ifdef WITH_THREAD #include "pythread.h" #endif /* WITH_THREAD */ @@ -3408,6 +3412,32 @@ test_pytime_assecondsdouble(PyObject *self, PyObject *args) return PyFloat_FromDouble(d); } +static PyObject * +test_PyTime_AsTimeval(PyObject *self, PyObject *args) +{ + PY_LONG_LONG ns; + int round; + _PyTime_t t; + struct timeval tv; + PyObject *seconds; + + if (!PyArg_ParseTuple(args, "Li", &ns, &round)) + return NULL; + if (check_time_rounding(round) < 0) + return NULL; + t = _PyTime_FromNanoseconds(ns); + if (_PyTime_AsTimeval(t, &tv, round) < 0) { + PyErr_SetString(PyExc_OverflowError, + "timeout doesn't fit into C timeval"); + return NULL; + } + + seconds = PyLong_FromLong((PY_LONG_LONG)tv.tv_sec); + if (seconds == NULL) + return NULL; + return Py_BuildValue("Nl", seconds, tv.tv_usec); +} + #ifdef HAVE_CLOCK_GETTIME static PyObject * test_PyTime_AsTimespec(PyObject *self, PyObject *args) @@ -3590,6 +3620,7 @@ static PyMethodDef TestMethods[] = { return_result_with_error, METH_NOARGS}, {"PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS}, {"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS}, + {"PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS}, #ifdef HAVE_CLOCK_GETTIME {"PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS}, #endif diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 880f3d2..21e6f43 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1405,8 +1405,11 @@ pysleep(_PyTime_t secs) do { #ifndef MS_WINDOWS - if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_UP) < 0) + if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_UP) < 0) { + PyErr_SetString(PyExc_OverflowError, + "delay doesn't fit into C timeval"); return -1; + } Py_BEGIN_ALLOW_THREADS err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout); |