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: readlines() corrupts file opened for 'a'
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: drbits, gvanrossum, tim.peters
Priority: normal Keywords:

Created on 2001-04-18 01:10 by anonymous, last changed 2022-04-10 16:03 by admin. This issue is now closed.

Messages (6)
msg4323 - (view) Author: Nobody/Anonymous (nobody) Date: 2001-04-18 01:10
Version 2.1 final release for Windows (using Idle)
Windows 98 2nd edition

f=open('c:\\My Documents\\textfile.txt', 'a')
f.write('hello file\n')
f.readlines()

raises the following error - it probably should.

Traceback (most recent call last):
  File "<pyshell#6>", line 1, in ?
    f.readlines()
IOError: [Errno 9] Bad file descriptor

However, the file gets a bunch of garbage appended to 
it.  In some cases, the entire contents of the GUI 
window gets appended.   

In Redhat 6.2 (also with Version 2.1 Final)the same 
code just erases the file.
msg4324 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-04-18 04:17
Logged In: YES 
user_id=6380

Assigned to Tim, because he believes that this is a
Microsoft bug.
msg4325 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-04-18 09:39
Logged In: YES 
user_id=31435

Back to you, Guido:  you're the one fond of arguing that MS 
is within its rights to do anything whatsoever when the 
rules for C stdio are broken <wink>.

Seriously, MS won't "fix" this (they'll deny it's broken).  
Same kind of thing happens in straight C under MS.

BTW, note that Anonymous did not claim it's an MS bug:  two 
different bugs were claimed (Win98SE appends crap, RH 6.2 
destroys the whole file).

I don't see a realistic choice here but to say "hmm -- 
tough luck, don't do that".
msg4326 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2001-04-18 14:00
Logged In: YES 
user_id=6380

Writing after reading without an intervening seek() is
illegal in C's stdio, but the Python file object (a thin
wrapper around stdio) doesn't know how to check for this
error condition.  So, we have to say "then don't do this".

Possible point of light: there's discussion on writing a new
I/O library for Python that avoids relying on stdio.  It
could fix this issue, too.

Closing the bug now.
msg4327 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2001-04-18 18:24
Logged In: YES 
user_id=31435

Note that the problem isn't *just* output followed by input 
without an intervening positioning operation:  the file was 
opened in append mode ("a"), not append-update mode ("a+"), 
so input isn't "legal" period.  MS can mangle part of the 
file even if there is a seek() in between (but doesn't if 
opened in append-update mode instead).
msg4328 - (view) Author: Martin D Katz, Ph.D. (drbits) Date: 2001-10-12 20:56
Logged In: YES 
user_id=276840

Under MS Windows 2K, opening the file for 'w' and then 
reading (with read()) also causes this.
History
Date User Action Args
2022-04-10 16:03:58adminsetgithub: 34358
2001-04-18 01:10:22anonymouscreate