classification
Title: Reading while writing-only permissions
Type: Stage:
Components: Windows Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: nnorwitz, quevedo, tim.peters
Priority: normal Keywords:

Created on 2003-02-08 23:50 by quevedo, last changed 2003-02-09 03:20 by tim.peters. This issue is now closed.

Messages (5)
msg14523 - (view) Author: Pablo de la Rosa (quevedo) Date: 2003-02-08 23:50
Hello world.




Well, let's see. When you open a file with "write-only" 
permission, then you write some in and after that you 
print the text contained in the file, you get your code (or 
almost all) and some other characters by output (See 1 
). 




- 1:




a) Code:


#!/usr/bin/python


file = open("test.txt", "w")


file.write("We love Python")


text = file.read()


print text




b) Output:


ÿÿ    


pen("test.txt", "w")


file.write("We love Python")


text = file.read()


print text


ext




ufferTypes   tuples	   TupleTypes   lists   
ListTypes   dicts   DictTypes   DictionaryTypes   
_fs   FunctionTypes


   LambdaTypes	   func_codes   CodeTypes   
RuntimeErrors   gs


   GeneratorTypes   _Cs	   ClassTypes   _ms   
UnboundMethodTypes   _xs   InstanceTypes


   MethodTypes   lens   BuiltinFunctionTypes   
appends   BuiltinMethodTypes


   ModuleTypes   files   FileTypes   xranges


   XRangeTypes	   TypeErrors   exc_infos   tbs


   TracebackTypes   tb_frames	   FrameTypes   
AttributeErrors   slices	   SliceTypes   Ellipsiss   
EllipsisTypes   __dict__s


   DictProxyType((   s   IntTypes   tbs   
BuiltinFunctionTypes   BooleanTypes   _Cs   
UnboundMethodTypes


   StringTypes   BuiltinMethodTypes	   FloatTypes   
DictionaryTypes   TypeTypes


   DictProxyTypes   _fs


   GeneratorTypes   InstanceTypes


   ObjectTypes   DictTypes   FileTypes   syss   
EllipsisTypes   Strý%  Lÿh HNh  ListTypes


   MethodTypes	   TupleTypes


   ModuleTypes	   FrameTypes   LongTypes


   BufferTypes


   TracebackTypes   gs   CodeTypes	   
ClassTypes   _xs   UnicodeTypes	   SliceTypes   
ComplexTypes


   LambdaTypes   FunctionTypes


   XRangeTypes   NoneType(    (    s   
C:\PYTHON23\lib\types.pys   ?   sr   
	





			



    s   |  
i i ?  Sd  S(   N(   s   selfs   datas


   itervalues(   s   self(    (    s   C




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




But if you try this without writing to the file you get just 
an error (See 2).




- 2:




a) Code:


#!/usr/bin/python


file = open("test.txt", "w")


text = file.read()


print text




b) Output:


Traceback (most recent call last):


  File "tralara.py", line 3, in ?


    text = file.read()


IOError: [Errno 9] Bad file descriptor




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




It's stupid, I know. Why somenone would want to read a 
file after writing to it ? Don't know, I've discovered this 
by error ;)




Tested in Python 2.3 Beta (Win32).




Happy coding friends.


msg14524 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2003-02-09 00:31
Logged In: YES 
user_id=33168

I cannot reproduce on Linux, I'm guessing this is a windows bug.
msg14525 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-02-09 00:47
Logged In: YES 
user_id=31435

Python doesn't implement files, the operating system and 
platform C libraries do that.  If the same thing happens in a 
C program, there's no hope.  Which version of Windows 
were you running (their file implementations aren't the 
same, especially not 95/98/ME vs NT/2000/XP)?
msg14526 - (view) Author: Pablo de la Rosa (quevedo) Date: 2003-02-09 01:06
Logged In: YES 
user_id=707875

Windows 98 Second Edition ;)
msg14527 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2003-02-09 03:20
Logged In: YES 
user_id=31435

Figures <wink>.  I tried this C program on Win98SE, using 
Microsoft's MSVC 6:

#include <stdio.h>
#include <stdlib.h>

void main()
{
    char buf[1000];
    size_t n;
    FILE *f = fopen("temp.txt", "w");

    fprintf(f, "%s\n", "Hello, world!");
    memset(buf, ' ', sizeof(buf));
    n = fread(buf, 1, sizeof(buf), f);
    printf("read %d chars, ferror is %d, feof is %d\n",
           n, ferror(f), feof(f));
}

It didn't complain.  The output was

read 1000 chars, ferror is 0, feof is 0

Since the OS doesn't complain, there's really nothing 
Python can do about it short of writing our own file 
implementation, and that's a huge project.

When I boosted the buf size in the above to 10000, it said 
it read 4082 characters, suggesting it's just reading 
whatever bits were left on the disk and sucked into its 
internal buffer.  File temp.txt was 4097 bytes when the 
program ended.

Since Python isn't doing any of this, I'm closing this as 
Windows, 3rdParty, and WontFix.  BTW, I use Win98SE 
too at home, but I don't expect it to act like a real 
operating system <wink>.
History
Date User Action Args
2003-02-08 23:50:22quevedocreate