Behaviour of time.sleep with negative arg

Jeff Epler jepler at unpythonic.net
Sun Mar 14 04:51:54 CET 2004


I'd agree that time.sleep(interval) should do the same thing on all
platforms when interval<0, but I'd rather see it always raise an
exception than sleep for 0 seconds.

I think this patch does what I suggest, but it's hard for me to tell
since my platform already raised an exception for sleep(-1).

Index: Modules/timemodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/timemodule.c,v
retrieving revision 2.139
diff -u -r2.139 timemodule.c
--- Modules/timemodule.c	20 Nov 2003 01:44:59 -0000	2.139
+++ Modules/timemodule.c	14 Mar 2004 03:23:00 -0000
@@ -169,6 +169,10 @@
 	double secs;
 	if (!PyArg_ParseTuple(args, "d:sleep", &secs))
 		return NULL;
+	if (secs < 0) {
+		errno = EINVAL;
+		return PyErr_SetFromErrno(PyExc_ValueError);
+	}
 	if (floatsleep(secs) != 0)
 		return NULL;
 	Py_INCREF(Py_None);

Jeff
PS POSIX sleep(3) takes an unsigned argument, so there is no possible
out-of-range value




More information about the Python-list mailing list