This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: .read() and .readline() differ in failing
Type: behavior Stage:
Components: Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, benjamin.peterson, eggy, habnabit, ironfroggy
Priority: normal Keywords: needs review, patch

Created on 2008-12-07 19:06 by eggy, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bad_readline.patch amaury.forgeotdarc, 2008-12-08 10:19
Messages (8)
msg77242 - (view) Author: Mark Florisson (eggy) * Date: 2008-12-07 19:06
>>> f = os.fdopen(os.open('spam!', os.O_TRUNC|os.O_CREAT|os.O_RDWR), 'w')
>>> f.read()
''
>>> f.readline()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor
>>> f.write("spamspamhihi")
>>> f.read()
''
>>> f.seek(0)
>>> f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor

This is very strange behaviour. First, .read() succeeds, and .readline()
fails, but after writing and seeking, .read() also fails.

In python3, both read and readline fail, but with different exceptions:

>>> f = os.fdopen(os.open('spam!', os.O_TRUNC|os.O_CREAT|os.O_RDWR), 'w')
>>> f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mark/source/code/_python-3.0/Lib/io.py", line 1718, in read
    decoder.decode(self.buffer.read(), final=True))
  File "/home/mark/source/code/_python-3.0/Lib/io.py", line 668, in read
    self._unsupported("read")
  File "/home/mark/source/code/_python-3.0/Lib/io.py", line 327, in
_unsupported
    (self.__class__.__name__, name))
io.UnsupportedOperation: BufferedWriter.read() not supported
>>> f.readline()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mark/source/code/_python-3.0/Lib/io.py", line 1807, in
readline
    while self._read_chunk():
  File "/home/mark/source/code/_python-3.0/Lib/io.py", line 1554, in
_read_chunk
    input_chunk = self.buffer.read1(self._CHUNK_SIZE)
AttributeError: 'BufferedWriter' object has no attribute 'read1'

In my opinion, all operations, in all python versions, should fail like
readline in the first example: IOError: [Errno 9] Bad file descriptor
msg77244 - (view) Author: Mark Florisson (eggy) * Date: 2008-12-07 19:23
Actually, I wouldn't expect it to fail like that, because it's not a bad
file descriptor, it's an actual that doesn't agree with the (userspace)
file mode. What I'd expect is probably a TypeError. Although an IOError,
with a different message, wouldn't be totally inappropriate either.
msg77245 - (view) Author: Mark Florisson (eggy) * Date: 2008-12-07 19:24
s/actual/operation/
msg77250 - (view) Author: Aaron Gallagher (habnabit) Date: 2008-12-07 20:33
I can't reproduce this on python 2.5.1, 2.5.2, or 2.6.0 on Mac OS 10.5.4. 
Both .read() and .readline() raise an EBADF IOError. 3.0.0 fails in the 
same way.
msg77265 - (view) Author: Mark Florisson (eggy) * Date: 2008-12-07 21:44
Perhaps it's linux specific then. I'm on debian lenny (2.6.26-1-amd64).
msg77277 - (view) Author: Calvin Spealman (ironfroggy) Date: 2008-12-07 23:48
Confirmed this behavior on my ubuntu installations but it fails properly 
on Windows.
msg77294 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-08 10:19
Python 2 is very platform-dependent: fdopen simply call the C function
fdopen() and returns a FILE*.
Since the sample code is a mistake (read on a file open in write mode),
how and when the error shows up really depends on the platform's
implementation of the FILE* object.

On the other hand Python 3 re-implemented all this, no FILE is used. The
io.UnsupportedOperation("BufferedWriter.read() not supported") is much
better IMO. And note that io.UnsupportedOperation is a IOError as well.

However the AttributeError is inconsistent. The attached patch turns it
into a UnsupportedOperation as above.
msg82925 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-02-28 17:17
I'm going to close this as a duplicate of #4996 (same fix), which will
be fixed when we merge the io-c branch.
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48829
2009-02-28 17:17:44benjamin.petersonsetstatus: open -> closed
resolution: duplicate
messages: + msg82925
nosy: + benjamin.peterson
2008-12-08 10:19:56amaury.forgeotdarcsetfiles: + bad_readline.patch
versions: - Python 2.5, Python 2.4
nosy: + amaury.forgeotdarc
messages: + msg77294
priority: normal
keywords: + needs review, patch
2008-12-07 23:48:51ironfroggysetmessages: + msg77277
2008-12-07 23:45:58ironfroggysetnosy: + ironfroggy
2008-12-07 21:44:10eggysetmessages: + msg77265
2008-12-07 20:33:13habnabitsetnosy: + habnabit
messages: + msg77250
2008-12-07 19:24:02eggysetmessages: + msg77245
2008-12-07 19:23:21eggysetmessages: + msg77244
2008-12-07 19:06:30eggycreate