classification
Title: filehandle.write() does not return None (Python 3.0a2)
Type: behavior Stage:
Components: Documentation Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: aroberge, christian.heimes, georg.brandl, gvanrossum, mark_t_russell
Priority: low Keywords:

Created on 2008-01-09 12:52 by aroberge, last changed 2008-03-21 19:42 by georg.brandl. This issue is now closed.

Messages (6)
msg59592 - (view) Author: Andre Roberge (aroberge) * Date: 2008-01-09 12:52
According to the docs, and consistent with the Python 2.x behavior,
filehandle.write() should return None.  However, under 3.0a2 (and
3.0a1), it returns the number of characters written.

Either the documentation
http://docs.python.org/dev/3.0/tutorial/inputoutput.html#reading-and-writing-files
is wrong, or it is a bug.  I would *much* prefer if the behavior would
stay the same as before.

Below is a sample session illustrating the behavior.  Note that I also
get an error message when exiting the session using exit() - this is an
unrelated problem.

Python 3.0a2 (r30a2:59382, Dec 27 2007, 15:48:14) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> handle = open('test_file.txt.', 'w')
>>> handle.write('spam')
4
>>> r = handle.write('more spam')
>>> print(r)
9
>>> exit()
/usr/local/py3k/lib/python3.0/io.py:1210: RuntimeWarning: Trying to
close unclosable fd!
  self.buffer.close()
msg59604 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-09 14:33
Guido, Mike and Mark have implemented most of the new IO library. I'm
sure they can shed some light on the design decision.

I've fixed the other bug a while ago. I don't use exit() and I didn't
noticed the problem until Joseph pointed me in the right direction.
msg59605 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-09 15:09
The intention was that the lowest-level (unbuffered) stream object can
write fewer bytes than given to it, as it is a direct interface to the
underlying system call, which has this property (especially when the
file is in non-blocking mode).

I think it's fine to return None from the higher-level classes' write()
method (buffered and text).  Though this makes it a bit harder to switch
between buffered and unbuffered binary output.  Perhaps text I/O should
return None and bytes I/O should return a byte count?
msg59625 - (view) Author: Andre Roberge (aroberge) * Date: 2008-01-09 21:12
After doing some more reading, I realize that the current behaviour is
totally consistent with
http://www.python.org/dev/peps/pep-3116/
which I should have consulted first.
So perhaps it should be considered to be simply a case of a
documentation error, as I alluded to when I open this issue. (sorry for
the noise)
msg63889 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-03-18 04:57
Georg, can you whip up a bit of documentation for this?  (Of course you
may find that the docs for io.py are lacking more than just this nit...)
msg64262 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-03-21 19:42
The docs for "io" are practically nonexistent :)

I'll leave that for a separate issue, and fixed this one by adding a
paragraph to the current file object docs (better than nothing) in r61710.
History
Date User Action Args
2008-03-21 19:42:37georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg64262
2008-03-18 04:57:45gvanrossumsetpriority: normal -> low
assignee: gvanrossum -> georg.brandl
messages: + msg63889
components: + Documentation, - Library (Lib)
2008-01-09 21:12:36arobergesetmessages: + msg59625
2008-01-09 15:09:59gvanrossumsetmessages: + msg59605
2008-01-09 14:33:47christian.heimessetpriority: normal
assignee: gvanrossum
messages: + msg59604
components: + Library (Lib), - Interpreter Core
nosy: + gvanrossum, georg.brandl, christian.heimes, mark_t_russell
2008-01-09 12:52:28arobergecreate