Behaviour of time.sleep with negative arg

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Colin Brown

    Behaviour of time.sleep with negative arg

    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



  • Jeff Epler

    #2
    Re: Behaviour of time.sleep with negative arg

    I'd agree that time.sleep(inte rval) 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_ParseTu ple(args, "d:sleep", &secs))
    return NULL;
    + if (secs < 0) {
    + errno = EINVAL;
    + return PyErr_SetFromEr rno(PyExc_Value Error);
    + }
    if (floatsleep(sec s) != 0)
    return NULL;
    Py_INCREF(Py_No ne);

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

    Comment

    • Colin Brown

      #3
      Re: Behaviour of time.sleep with negative arg

      "Jeff Epler" <jepler@unpytho nic.net> wrote in message
      news:mailman.15 .1079236344.745 .python-list@python.org ...[color=blue]
      > I'd agree that time.sleep(inte rval) 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.[/color]

      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



      Comment

      • Jeff Epler

        #4
        Re: Behaviour of time.sleep with negative arg

        On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:[color=blue]
        > "Jeff Epler" <jepler@unpytho nic.net> wrote in message
        > news:mailman.15 .1079236344.745 .python-list@python.org ...[color=green]
        > > I'd agree that time.sleep(inte rval) 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.[/color]
        >
        > 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[/color]

        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.
        [color=blue]
        > 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.[/color]

        def unsurprising_sl eep(d):
        if d < 0: return
        time.sleep(d)
        [color=blue]
        > Whatever is decided, the behaviour needs to be documented under time.sleep![/color]

        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

        Comment

        • Colin Brown

          #5
          Re: Behaviour of time.sleep with negative arg


          "Jeff Epler" <jepler@unpytho nic.net> wrote in message
          news:mailman.10 .1079299930.682 .python-list@python.org ...[color=blue]
          > On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:[color=green]
          > > "Jeff Epler" <jepler@unpytho nic.net> wrote in message
          > > news:mailman.15 .1079236344.745 .python-list@python.org ...[/color]
          > + The behavior when secs is less than zero is undefined. In[/color]
          particular,[color=blue]
          > + a future version of Python may interpret 'time.sleep(-1); other[/color]
          code'[color=blue]
          > + to cause 'other code' to execute beginning one second before the[/color]
          call[color=blue]
          > + to time.sleep.[/color]

          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


          Comment

          • Scott David Daniels

            #6
            Re: Behaviour of time.sleep with negative arg

            Colin Brown wrote:
            [color=blue]
            > "Jeff Epler" <jepler@unpytho nic.net> wrote in message
            > news:mailman.15 .1079236344.745 .python-list@python.org ...
            > ... time.sleep() should perform the same on all platforms but would
            > rather the sleep(0) for negative args because ...[/color]
            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
            Scott.Daniels@A cm.Org

            Comment

            • Andrew Bennetts

              #7
              Re: Behaviour of time.sleep with negative arg

              On Sun, Mar 14, 2004 at 03:19:53PM -0600, Jeff Epler wrote:[color=blue]
              > On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:[/color]
              [...][color=blue][color=green]
              > > 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.[/color]
              >
              > def unsurprising_sl eep(d):
              > if d < 0: return
              > time.sleep(d)[/color]

              Or even just:

              time.sleep(max( d, 0))

              -Andrew.


              Comment

              • Colin Brown

                #8
                Re: Behaviour of time.sleep with negative arg

                "Andrew Bennetts" <andrew-pythonlist@puzz ling.org> wrote in message
                news:mailman.0. 1079317970.1224 1.python-list@python.org ...[color=blue]
                > On Sun, Mar 14, 2004 at 03:19:53PM -0600, Jeff Epler wrote:[color=green]
                > > On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:[/color]
                > [...][color=green][color=darkred]
                > > > 2. A common usage of sleep is with the difference of two[/color][/color][/color]
                values. If[color=blue][color=green][color=darkred]
                > > > it raises an exception on negative numbers then when one takes the
                > > > difference of say a timestamp and current-time (real-values), to be[/color][/color][/color]
                safe you[color=blue][color=green][color=darkred]
                > > > would need to put the difference in a temporary variable, check that[/color][/color][/color]
                for[color=blue][color=green][color=darkred]
                > > > negative and only sleep for positive values - not very elegant.[/color]
                > >
                > > def unsurprising_sl eep(d):
                > > if d < 0: return
                > > time.sleep(d)[/color]
                >
                > Or even just:
                >
                > time.sleep(max( d, 0))
                >
                > -Andrew.
                >
                >[/color]

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

                Colin



                Comment

                Working...