Behaviour of time.sleep with negative arg

Colin Brown cbrown at metservice.com
Sun Mar 14 14:43:51 EST 2004


"Jeff Epler" <jepler at unpythonic.net> wrote in message
news:mailman.15.1079236344.745.python-list at 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






More information about the Python-list mailing list