<div dir="ltr"><div><div><div><div>File objects historically have a pretty vague spec. E.g. it's not defined which methods are supported beyond read() and readline() (for readers) or write() (for writers).<br><br></div>

Short writes shouldn't be allowed (a regular file object's write() doesn't even return a value for this reason). This means that a raw (unbuffered) <br><br></div>Short reads *should* be expected, since behavior around these varies widely. If you see code that doesn't expect them please file bugs.<br>

<br></div>The glossary item doesn't provide much guidance for would-be implementers of compliant file file objects, and the interface defined in the io module is too large (e.g. nobody cares to implement readinto()).<br>

<br></div>I think we should clarify that raw (unbuffered) file objects are not safe. I don't care about preventing this explicitly though -- when you see "file object" you should think "duck typing" and program accordingly. (Reading and writing are already distinct interfaces; ditto for text vs. bytes.)<br>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Apr 28, 2014 at 10:54 AM, Charles-François Natali <span dir="ltr"><<a href="mailto:cf.natali@gmail.com" target="_blank">cf.natali@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
What's meant exactly by a "file object"?<br>
<br>
Let me be more specific: for example, pickle.dump() accepts a "file object".<br>
<br>
Looking at the code, it doesn't check the return value of its write() method.<br>
<br>
So it assumes that write() should always write the whole data (not<br>
partial write).<br>
<br>
Same thing for read, it assumes there won't be short reads.<br>
<br>
A sample use case would be passing a socket.makefile() to pickle: it<br>
works, because makefile() returns a BufferedReader/Writer which takes<br>
care of short read/write.<br>
<br>
But the documentation just says "file object". And if you have a look<br>
the file object definition in the glossary:<br>
<a href="https://docs.python.org/3.5/glossary.html#term-file-object" target="_blank">https://docs.python.org/3.5/glossary.html#term-file-object</a><br>
<br>
"""<br>
There are actually three categories of file objects: raw binary files,<br>
buffered binary files and text files. Their interfaces are defined in<br>
the io module. The canonical way to create a file object is by using<br>
the open() function.<br>
"""<br>
<br>
So someone passing e.g. a raw binary file - which doesn't handle short<br>
reads/writes - would run into trouble.<br>
<br>
It's the same thing for e.g. GzipFile, and probably many others.<br>
<br>
Would it make sense to add a note somewhere?<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" target="_blank">https://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)
</div>