Issue #12459: time.sleep() now raises a ValueError if the sleep length is

negative, instead of an infinite sleep on Windows or raising an IOError on
Linux for example, to have the same behaviour on all platforms.
This commit is contained in:
Victor Stinner 2011-07-05 22:00:25 +02:00
parent 5351a1f956
commit 7f53a5027d
3 changed files with 11 additions and 0 deletions

View File

@ -27,6 +27,8 @@ class TimeTestCase(unittest.TestCase):
int(self.t)) int(self.t))
def test_sleep(self): def test_sleep(self):
self.assertRaises(ValueError, time.sleep, -2)
self.assertRaises(ValueError, time.sleep, -1)
time.sleep(1.2) time.sleep(1.2)
def test_strftime(self): def test_strftime(self):

View File

@ -219,6 +219,10 @@ Core and Builtins
Library Library
------- -------
- Issue #12459: time.sleep() now raises a ValueError if the sleep length is
negative, instead of an infinite sleep on Windows or raising an IOError on
Linux for example, to have the same behaviour on all platforms.
- Issue #12451: pydoc: html_getfile() now uses tokenize.open() to support - Issue #12451: pydoc: html_getfile() now uses tokenize.open() to support
Python scripts using a encoding different than UTF-8 (read the coding cookie Python scripts using a encoding different than UTF-8 (read the coding cookie
of the script). of the script).

View File

@ -141,6 +141,11 @@ time_sleep(PyObject *self, PyObject *args)
double secs; double secs;
if (!PyArg_ParseTuple(args, "d:sleep", &secs)) if (!PyArg_ParseTuple(args, "d:sleep", &secs))
return NULL; return NULL;
if (secs < 0) {
PyErr_SetString(PyExc_ValueError,
"sleep length must be non-negative");
return NULL;
}
if (floatsleep(secs) != 0) if (floatsleep(secs) != 0)
return NULL; return NULL;
Py_INCREF(Py_None); Py_INCREF(Py_None);