Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79785

Re: Status of side-effecting functions in python

From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: Status of side-effecting functions in python
Date 2014-10-27 14:45 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <m2llqj$oju$1@reader1.panix.com> (permalink)
References (2 earlier) <438d510f-05ae-47dd-bcda-42a8b777c99b@googlegroups.com> <m2itm2$l5r$1@dont-email.me> <683c84d8-d916-4b63-b4b2-92cd2763e260@googlegroups.com> <roy-8A6FEF.17562126102014@news.panix.com> <544e2cf2$0$13009$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


On 2014-10-27, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
> Roy Smith wrote:
>
>>> Yes and no. If something goes wrong in a .write() method,
>>> is not Python supposed to raise an error? (!)
>> 
>> Define "wrong".  It is not an error for a write() call to consume fewer
>> bytes than were requested.  
>
> It's not? I'm asking a genuine question here, not a rhetorical one.

No.  

Under Unix/Posix a write() call may _always_ write fewer bytes than
requested.

It may be that the device/pipe/file whatever has filled and blocking
is disabled.  It may be that the device has decided that, at the
moment, it can only handle a certain amount of data for some other
reason.  For example: Let's say you're writing to a network connection
that must segment data, and you try to write more than will fit in the
current segment. The write() call may fill the segment, send the
segment, and "refuse" the rest of the data -- requiring that you make
a subsequent write() with the remaining data (at which point it will
start a new segment).

Or, it may be because the system call was interrupted by something
completely unrelated to your program, the write() call, or the "thing"
to which you're writing [and it was more convenient for whoever wrote
the OS to do a partial write than it was to try to resume the write].

If you really want to make sure that all bytes get written, you _must_
put all write() calls in a loop that checks the return value and keeps
re-writing any unwritten data.

And to answer your next question: yes, Unix application programmers
have been complaining about that (perhaps justifiably) since 1970.

-- 
Grant Edwards               grant.b.edwards        Yow! I have a TINY BOWL in
                                  at               my HEAD
                              gmail.com            

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Status of side-effecting functions in python Rustom Mody <rustompmody@gmail.com> - 2014-10-25 10:27 -0700
  Re: Status of side-effecting functions in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-25 23:41 +0200
    Re: Status of side-effecting functions in python Dan Sommers <dan@tombstonezero.net> - 2014-10-25 22:22 +0000
      Re: Status of side-effecting functions in python Terry Reedy <tjreedy@udel.edu> - 2014-10-25 18:53 -0400
    Re: Status of side-effecting functions in python wxjmfauth@gmail.com - 2014-10-26 00:45 -0700
      Re: Status of side-effecting functions in python Dan Sommers <dan@tombstonezero.net> - 2014-10-26 13:41 +0000
        Re: Status of side-effecting functions in python Rustom Mody <rustompmody@gmail.com> - 2014-10-26 07:53 -0700
        Re: Status of side-effecting functions in python wxjmfauth@gmail.com - 2014-10-26 09:26 -0700
          Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-26 18:50 +0200
          Re: Status of side-effecting functions in python Roy Smith <roy@panix.com> - 2014-10-26 17:56 -0400
            Re: Status of side-effecting functions in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-27 22:30 +1100
              Re: Status of side-effecting functions in python Chris Angelico <rosuav@gmail.com> - 2014-10-27 22:44 +1100
              Re: Status of side-effecting functions in python Roy Smith <roy@panix.com> - 2014-10-27 09:28 -0400
              Re: Status of side-effecting functions in python Grant Edwards <invalid@invalid.invalid> - 2014-10-27 14:45 +0000
                Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-27 17:14 +0200
                Re: Status of side-effecting functions in python Nobody <nobody@nowhere.invalid> - 2014-10-28 03:52 +0000
                Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-28 11:07 +0200
                Re: Status of side-effecting functions in python Marko Rauhamaa <marko@pacujo.net> - 2014-10-28 11:10 +0200
                .write() behavior Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2014-10-29 01:06 -0400
                Re: .write() behavior Marko Rauhamaa <marko@pacujo.net> - 2014-10-29 08:00 +0200
                Re: .write() behavior Alan Bawden <alan@scooby-doo.csail.mit.edu> - 2014-10-31 16:32 -0400
    Re: Status of side-effecting functions in python Grant Edwards <invalid@invalid.invalid> - 2014-10-27 14:22 +0000

csiph-web