[Python-bugs-list] [ python-Bugs-416906 ] readlines() corrupts file opened for 'a'

noreply@sourceforge.net noreply@sourceforge.net
Fri, 12 Oct 2001 13:56:50 -0700


Bugs item #416906, was opened at 2001-04-17 18:10
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=416906&group_id=5470

Category: None
Group: None
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Guido van Rossum (gvanrossum)
Summary: readlines() corrupts file opened for 'a'

Initial Comment:
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.

----------------------------------------------------------------------

Comment By: Martin D Katz, Ph.D. (drbits)
Date: 2001-10-12 13:56

Message:
Logged In: YES 
user_id=276840

Under MS Windows 2K, opening the file for 'w' and then 
reading (with read()) also causes this.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-04-18 11:24

Message:
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).

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-04-18 07:00

Message:
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.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-04-18 02:39

Message:
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".

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-04-17 21:17

Message:
Logged In: YES 
user_id=6380

Assigned to Tim, because he believes that this is a
Microsoft bug.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=416906&group_id=5470