By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

Behaviour of time.sleep with negative arg

P: n/a
Python 2.3.3
Under Windows NT: Negative Int or Real -> sleeps for a long time
Under Linux: Returns IOError: [Errno 22] Invalid argument

I consider both of these unfriendly results and expected the preferable
silent time.sleep(0) in much the same way that slicing a string beyond the
ends returns an empty string.

Colin Brown
PyNZ

Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma***********************************@python. org...
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 am 100% with you that time.sleep() should perform the same on all
platforms but would rather the sleep(0) for negative args because:
1. 2**31 seconds is about 68 years so interpreting a negative integer
as unsigned is practically useless
2. A common usage of sleep is with the difference of two values. If
it raises an exception on negative numbers then when one takes the
difference of say a timestamp and current-time (real-values), to be safe you
would need to put the difference in a temporary variable, check that for
negative and only sleep for positive values - not very elegant.
3. I only came across this in some long-running code that because of
variation in thread code execution times aborted on Linux when a tiny
negative number was produced, hence my concern.
4. Other Python behaviour takes the least surprise option, eg
"abc"[-9:] does not raise an exception.

Whatever is decided, the behaviour needs to be documented under time.sleep!

Colin Brown
PyNZ

Jul 18 '05 #3

P: n/a
On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:
"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma***********************************@python. org...
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 am 100% with you that time.sleep() should perform the same on all
platforms but would rather the sleep(0) for negative args because:
1. 2**31 seconds is about 68 years so interpreting a negative integer
as unsigned is practically useless


I was not arguing that it was too useful to be able to sleep for 68
years (and certainly not that the argument -1 should be interpreted to
mean 68 years), but rather that sleep(-1) is a TypeError or ValueError
because it's not in the range of valid arguments to sleep.
2. A common usage of sleep is with the difference of two values. If
it raises an exception on negative numbers then when one takes the
difference of say a timestamp and current-time (real-values), to be safe you
would need to put the difference in a temporary variable, check that for
negative and only sleep for positive values - not very elegant.
def unsurprising_sleep(d):
if d < 0: return
time.sleep(d)
Whatever is decided, the behaviour needs to be documented under time.sleep!


sleep(secs)
Suspend execution for the given number of seconds. The argument
may be a floating point number to indicate a more precise
sleep time. The actual suspension time may be less than that
requested because any caught signal will terminate the sleep()
following execution of that signal's catching routine. Also,
the suspension time may be longer than requested by an arbitrary
amount because of the scheduling of other activity in the system.

+ The behavior when secs is less than zero is undefined. In particular,
+ a future version of Python may interpret 'time.sleep(-1); other code'
+ to cause 'other code' to execute beginning one second before the call
+ to time.sleep.

You can find my proposed enhancement to the documentation above.

Jeff

Jul 18 '05 #4

P: n/a

"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma***********************************@python. org...
On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:
"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma***********************************@python. org... + The behavior when secs is less than zero is undefined. In

particular, + a future version of Python may interpret 'time.sleep(-1); other code' + to cause 'other code' to execute beginning one second before the call + to time.sleep.


I do not think this is taking it far enough, why not support a complex
number argument, you should be able to do something interesting with that!

Thanks
Colin
Jul 18 '05 #5

P: n/a
Colin Brown wrote:
"Jeff Epler" <je****@unpythonic.net> wrote in message
news:ma***********************************@python. org...
... time.sleep() should perform the same on all platforms but would
rather the sleep(0) for negative args because ...

Surely not __all__ platforms. Certainly the BDFL's machine needs full
access to time machine controls. I can see restricting the use of
negative arguments to authorized members of the PSU (if it actually
existed)....

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #6

P: n/a
On Sun, Mar 14, 2004 at 03:19:53PM -0600, Jeff Epler wrote:
On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:

[...]
2. A common usage of sleep is with the difference of two values. If
it raises an exception on negative numbers then when one takes the
difference of say a timestamp and current-time (real-values), to be safe you
would need to put the difference in a temporary variable, check that for
negative and only sleep for positive values - not very elegant.


def unsurprising_sleep(d):
if d < 0: return
time.sleep(d)


Or even just:

time.sleep(max(d, 0))

-Andrew.
Jul 18 '05 #7

P: n/a
"Andrew Bennetts" <an***************@puzzling.org> wrote in message
news:ma************************************@python .org...
On Sun, Mar 14, 2004 at 03:19:53PM -0600, Jeff Epler wrote:
On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:

[...]
2. A common usage of sleep is with the difference of two values. If it raises an exception on negative numbers then when one takes the
difference of say a timestamp and current-time (real-values), to be safe you would need to put the difference in a temporary variable, check that for negative and only sleep for positive values - not very elegant.


def unsurprising_sleep(d):
if d < 0: return
time.sleep(d)


Or even just:

time.sleep(max(d, 0))

-Andrew.


Agreed, This is what had I ended up coding, almost acceptable ;-)

Colin

Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.