Issue 4579: .read() and .readline() differ in failing (original) (raw)

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 "", line 1, in IOError: [Errno 9] Bad file descriptor f.write("spamspamhihi") f.read() '' f.seek(0) f.read() Traceback (most recent call last): File "", line 1, in 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 "", line 1, in 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 "", line 1, in 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

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.