]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #12459: time.sleep() now raises a ValueError if the sleep length is
authorVictor Stinner <victor.stinner@haypocalc.com>
Tue, 5 Jul 2011 20:00:25 +0000 (22:00 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Tue, 5 Jul 2011 20:00:25 +0000 (22:00 +0200)
negative, instead of an infinite sleep on Windows or raising an IOError on
Linux for example, to have the same behaviour on all platforms.

Lib/test/test_time.py
Misc/NEWS
Modules/timemodule.c

index 9d7dbc87c0d63a4ad2510de8eaae28d0e685f5bb..94de098911ac99128982c329d54d64ea448b3fcf 100644 (file)
@@ -27,6 +27,8 @@ class TimeTestCase(unittest.TestCase):
                          int(self.t))
 
     def test_sleep(self):
+        self.assertRaises(ValueError, time.sleep, -2)
+        self.assertRaises(ValueError, time.sleep, -1)
         time.sleep(1.2)
 
     def test_strftime(self):
index 4f41fb17be4b2e7aecfa606cd4a472b2ab940481..b96ca50df01071ef6df031932e3e790b937ea431 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -219,6 +219,10 @@ Core and Builtins
 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
   Python scripts using a encoding different than UTF-8 (read the coding cookie
   of the script).
index 636d4adcf0b02de16a70f1e7241e846b370dd5c9..4dc82a0310eca539ac7a9a52f7af7a2447c3243f 100644 (file)
@@ -141,6 +141,11 @@ time_sleep(PyObject *self, PyObject *args)
     double secs;
     if (!PyArg_ParseTuple(args, "d:sleep", &secs))
         return NULL;
+    if (secs < 0) {
+        PyErr_SetString(PyExc_ValueError,
+                        "sleep length must be non-negative");
+        return NULL;
+    }
     if (floatsleep(secs) != 0)
         return NULL;
     Py_INCREF(Py_None);