Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!feed.news.qwest.net!mpls-nntp-03.inet.qwest.net!132.250.1.121.MISMATCH!ra.nrl.navy.mil!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!roy From: Roy Smith Newsgroups: comp.lang.python Subject: Re: Status of side-effecting functions in python Date: Mon, 27 Oct 2014 09:28:37 -0400 Organization: PANIX Public Access Internet and UNIX, NYC Lines: 30 Message-ID: References: <438d510f-05ae-47dd-bcda-42a8b777c99b@googlegroups.com> <683c84d8-d916-4b63-b4b2-92cd2763e260@googlegroups.com> <544e2cf2$0$13009$c3e8da3$5496439d@news.astraweb.com> NNTP-Posting-Host: localhost X-Trace: reader1.panix.com 1414416517 2508 127.0.0.1 (27 Oct 2014 13:28:37 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 27 Oct 2014 13:28:37 +0000 (UTC) User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) Xref: csiph.com comp.lang.python:79782 In article <544e2cf2$0$13009$c3e8da3$5496439d@news.astraweb.com>, Steven D'Aprano 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. I would > expect that if I ask to write 2 bytes, and only 1 byte is written, that > absolutely is an error. Under what circumstances is it okay for write() to > throw data away? It's not throwing away data. The write() call returns a count of how many bytes is consumed, so you can present the rest of them again in a later write() call (assuming that makes sense to do for your application). In some cases, the underlying hardware (or network protocol) may be unable to handle the number of bytes you requested, or may fail in mid-transmission. Imagine a serial link. You tell it to write 100 bytes. It starts sending them down the line and after 20 bytes, the connection fails. What should write() do in that case? It hasn't written all the data, so it needs to let you know that. It also has written *some* of the data, so it needs to let you know that too. What you do with that information is up to you, but it clearly needs to return a richer status indication than just success/failure.