Re: problem with read() write()

2009-11-01 Thread Gertjan Klein
Alf P. Steinbach wrote:

So with 'w+' the only way to get garbage is if 'read' reads beyond the end of 
file, or 'open' doesn't conform to the documentation.

It does read beyond the end of file. This is perhaps the way the
underlying C library works, but it looks like an unexpected feature
(read: bug) to me.

I reproduced (with Python 2.5.2 on WinXP) the code the OP wrote after
creating an empty (0-byte) test file; after the write() the read()
returns random garbage. I can't imagine why anyone would want that
behaviour. The file grew to be 4099 bytes after f.close(). I wrote
'hello' to it, so the length of garbage added was 4094 bytes, which I
find a strange number also.

I would have expected the read to return nothing. Can anyone explain or
even defend this behaviour?

Gertjan.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-11-01 Thread Alf P. Steinbach

* Gertjan Klein:

Alf P. Steinbach wrote:

So with 'w+' the only way to get garbage is if 'read' reads beyond the end of 
file, or 'open' doesn't conform to the documentation.


It does read beyond the end of file. This is perhaps the way the
underlying C library works, but it looks like an unexpected feature
(read: bug) to me.

I reproduced (with Python 2.5.2 on WinXP) the code the OP wrote after
creating an empty (0-byte) test file; after the write() the read()
returns random garbage. I can't imagine why anyone would want that
behaviour. The file grew to be 4099 bytes after f.close(). I wrote
'hello' to it, so the length of garbage added was 4094 bytes, which I
find a strange number also.


Could you post (copy and paste) the code, and description of results?



I would have expected the read to return nothing. Can anyone explain or
even defend this behaviour?


I'm just a Python newbie, but in C and C++ such things are usually down to 
undefined behavior, that is, the program doing something that is implicitly or 
explicitly defined as undefined behavior by the language standard.


With UB the effect may then be something or nothing or anything or just what you 
expected; appearance of the infamous nasal demons is one possibility...


Quoting n869, which is the January 18th 1999 draft of the C99 standard:

  §7.19.5.3/6
  When a file is opened with update mode (’+’ as the second or third
  character in the above list of mode argument values), both input and
  output may be performed on the associated stream. However, output shall
  not be directly followed by input without an intervening call to the
  fflush function or to a file positioning function (fseek, fsetpos, or
  rewind), and input shall not be directly followed by output without an
  intervening call to a file positioning function, unless the input
  operation encounters end-of-file. Opening( or creating) a text file with
  update mode may instead open (or create) a binary stream in some
  implementations.

Shall not means UB. This applies to C FILE* handling.

AFAICS nothing except efficiency prevents the Python wrapper, if FILE* is what 
it uses, from automatically inserting an appropriate fflush or fseek.


And for a language used by so many newbies (this is positive!) I agree that it 
should ideally get rid of that UB (assuming that's what the problem is), or, if 
it doesn't already, mention that in the Python documentation.



Cheers,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-11-01 Thread Gertjan Klein
Alf P. Steinbach wrote:

* Gertjan Klein:
 I reproduced (with Python 2.5.2 on WinXP) the code the OP wrote after
 creating an empty (0-byte) test file; after the write() the read()
 returns random garbage. I can't imagine why anyone would want that
 behaviour. The file grew to be 4099 bytes after f.close(). I wrote
 'hello' to it, so the length of garbage added was 4094 bytes, which I
 find a strange number also.

Could you post (copy and paste) the code, and description of results?

The code is exactly the OP's code, with an f.close() after f.read(). The
results are described above.

Shall not means UB. This applies to C FILE* handling.

Yes. But Python is not C. I would have expected that Python shields me
from such bizarre results. The fact that, apparently, the Python file
object is a thin wrapper over a C library function is an explanation of
the behaviour, but not a justification.

And for a language used by so many newbies (this is positive!) I agree that it 
should ideally get rid of that UB (assuming that's what the problem is), or, 
if 
it doesn't already, mention that in the Python documentation.

I'm not sure I can defend this statement, but I think Python should not
have undefined behaviour at all.

Gertjan.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-11-01 Thread Matt Nordhoff
Gertjan Klein wrote:
 Alf P. Steinbach wrote:
 
 So with 'w+' the only way to get garbage is if 'read' reads beyond the end 
 of 
 file, or 'open' doesn't conform to the documentation.
 
 It does read beyond the end of file. This is perhaps the way the
 underlying C library works, but it looks like an unexpected feature
 (read: bug) to me.
 
 I reproduced (with Python 2.5.2 on WinXP) the code the OP wrote after
 creating an empty (0-byte) test file; after the write() the read()
 returns random garbage. I can't imagine why anyone would want that
 behaviour. The file grew to be 4099 bytes after f.close(). I wrote
 'hello' to it, so the length of garbage added was 4094 bytes, which I
 find a strange number also.
 
 I would have expected the read to return nothing. Can anyone explain or
 even defend this behaviour?
 
 Gertjan.

I wonder, does it still happen if you open the file in binary mode?
(Stick a b in the file mode.) It could be some Windows text mode
craziness.
-- 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-11-01 Thread Steven D'Aprano
On Sun, 01 Nov 2009 10:44:45 +0100, Alf P. Steinbach wrote:

 Could you post (copy and paste) the code, and description of results?

Using Python 2.6 under Linux (Fedora 7):

 f = open('garbage', 'r')  # prove the file doesn't exist
Traceback (most recent call last):
  File stdin, line 1, in module
IOError: [Errno 2] No such file or directory: 'garbage'
 f = open('garbage', 'wb+')
 f.read(4)
''
 f.write('hello world\n')
 f.seek(0)
 f.read(100)
'hello world\n'


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach

* Zeynel:

Hello,

I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:

f = open(pw, r+)
f.write(hello)
f.read()

But read() returns a bunch of what looks like meta code:

ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'QUEUE'\np1\n
(S'exec' 

What am I doing wrong? Thank you.


After the 'write' the current position in the file is after the hello, so 
reading will read further content from there.


The following works (disclaimer: I'm utter newbie in Python, and didn't consult 
the documentation, and it's the first time I've seen the Python 'open'):



f = open(pw, r+)
f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()


Cheers  hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
 * Zeynel:





  Hello,

  I've been studying the official tutorial, so far it's been fun, but
  today I ran into a problem with the write(). So, I open the file pw
  and write hello and read:

  f = open(pw, r+)
  f.write(hello)
  f.read()

  But read() returns a bunch of what looks like meta code:

  ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
  \xa5\x02\x0b
  \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
  0'QUEUE'\np1\n
  (S'exec' 

  What am I doing wrong? Thank you.

 After the 'write' the current position in the file is after the hello, so
 reading will read further content from there.

 The following works (disclaimer: I'm utter newbie in Python, and didn't 
 consult
 the documentation, and it's the first time I've seen the Python 'open'):

 f = open(pw, r+)
 f.write( hello )
 f.seek( 0 )  # Go back to start of file
 f.read()
 f.close()

 Cheers  hth.,

 - Alf

Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach

* Zeynel:

On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:

* Zeynel:






Hello,
I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:
f = open(pw, r+)
f.write(hello)
f.read()
But read() returns a bunch of what looks like meta code:
ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
0'QUEUE'\np1\n
(S'exec' 
What am I doing wrong? Thank you.

After the 'write' the current position in the file is after the hello, so
reading will read further content from there.

The following works (disclaimer: I'm utter newbie in Python, and didn't consult
the documentation, and it's the first time I've seen the Python 'open'):

f = open(pw, r+)
f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()

Cheers  hth.,

- Alf


Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.


Just a thought: try w+ instead of r+.

Because if you do

  print( open.__doc__ )

as I recall it said something about w truncating the file?


Cheers  hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:
 * Zeynel:





  On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
  * Zeynel:

  Hello,
  I've been studying the official tutorial, so far it's been fun, but
  today I ran into a problem with the write(). So, I open the file pw
  and write hello and read:
  f = open(pw, r+)
  f.write(hello)
  f.read()
  But read() returns a bunch of what looks like meta code:
  ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
  \xa5\x02\x0b
  \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
   0'QUEUE'\np1\n
  (S'exec' 
  What am I doing wrong? Thank you.
  After the 'write' the current position in the file is after the hello, so
  reading will read further content from there.

  The following works (disclaimer: I'm utter newbie in Python, and didn't 
  consult
  the documentation, and it's the first time I've seen the Python 'open'):

  f = open(pw, r+)
  f.write( hello )
  f.seek( 0 )  # Go back to start of file
  f.read()
  f.close()

  Cheers  hth.,

  - Alf

  Thanks, but it didn't work for me. I still get the meta file. Although
  I see that hello is there.

 Just a thought: try w+ instead of r+.

 Because if you do

    print( open.__doc__ )

 as I recall it said something about w truncating the file?

 Cheers  hth.,

 - Alf

No :) I still got the same thing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Alf P. Steinbach

* Zeynel:

On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:

* Zeynel:






On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:

* Zeynel:

Hello,
I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:
f = open(pw, r+)
f.write(hello)
f.read()
But read() returns a bunch of what looks like meta code:
ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
0'QUEUE'\np1\n
(S'exec' 
What am I doing wrong? Thank you.

After the 'write' the current position in the file is after the hello, so
reading will read further content from there.
The following works (disclaimer: I'm utter newbie in Python, and didn't consult
the documentation, and it's the first time I've seen the Python 'open'):
f = open(pw, r+)
f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()
Cheers  hth.,
- Alf

Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.

Just a thought: try w+ instead of r+.

Because if you do

   print( open.__doc__ )

as I recall it said something about w truncating the file?

Cheers  hth.,

- Alf


No :) I still got the same thing.


Hm. Now I had to look in the docs because I thought I'd given bad advice.

Doc of 'open' says:

  The mode 'w+' opens and truncates the file to 0 bytes, while 'r+' opens the
   file without truncation.

So with 'w+' the only way to get garbage is if 'read' reads beyond the end of 
file, or 'open' doesn't conform to the documentation.


Testing with Python 3.1.1 under Windows XP Pro:

example
 f = open( zilly, w+ )
 f.write( garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage )
63
 f.close()
 f = open( zilly, r )
 f.read()
'garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
 f.close()
 f.open( zilly, r+ )
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
 open( zilly, r+ )
_io.TextIOWrapper name='zilly' encoding='cp1252'
 f = open( zilly, r+ )
 f.write( hello )
5
 f.seek( 0 )
0
 f.read()
'hellogegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
 f.close()
 f = open( zilly, w+ )
 f.write( hello )
5
 f.seek( 0 )
0
 f.read()
'hello'
 f.close()

/example

The w+ works here. Even if I made a typing mistake and apparently left the 
file open in the middle there.



Cheers  hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Dave Angel

Zeynel wrote:

On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:
  

* Zeynel:







On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
  

* Zeynel:


Hello,
I've been studying the official tutorial, so far it's been fun, but
today I ran into a problem with the write(). So, I open the file pw
and write hello and read:
f =pen(pw, r+)
f.write(hello)
f.read()
But read() returns a bunch of what looks like meta code:
ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
\xa5\x02\x0b
\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0 
0'QUEUE'\np1\n
(S'exec' 
What am I doing wrong? Thank you.
  

After the 'write' the current position in the file is after the hello, so
reading will read further content from there.

The following works (disclaimer: I'm utter newbie in Python, and didn't consult

the documentation, and it's the first time I've seen the Python 'open'):

f =pen(pw, r+)

f.write( hello )
f.seek( 0 )  # Go back to start of file
f.read()
f.close()

Cheers  hth.,

- Alf


Thanks, but it didn't work for me. I still get the meta file. Although
I see that hello is there.
  

Just a thought: try w+ instead of r+.

Because if you do

   print( open.__doc__ )

as I recall it said something about w truncating the file?

Cheers  hth.,

- Alf



No :) I still got the same thing.

  
When you ran the program, the file already existed.  So you're mixing 
the old content and the new.  If you don't want that, then don't use r+. 

When testing, you should start with the system in a known state.  Try 
initializing (with a text editor for example) that file before trying 
the program.


And if it still gives you problems, show us the data in the file before 
running your code, your new code, and the results you get.   One thing 
that'd be useful is to actually assign the results of the read() 
function to a variable and print it.


DaveA

--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 10:40 am, Alf P. Steinbach al...@start.no wrote:
Thanks! This works. But I need to close the file before read and open
it again with r, otherwise I get the garbage again. Can you give me
the link where you got this in documentation:

The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
opens the
 file without truncation.

Only place i could find it was in this bug report: 
http://bugs.python.org/issue5061

 * Zeynel:





  On Oct 31, 9:55 am, Alf P. Steinbach al...@start.no wrote:
  * Zeynel:

  On Oct 31, 9:23 am, Alf P. Steinbach al...@start.no wrote:
  * Zeynel:
  Hello,
  I've been studying the official tutorial, so far it's been fun, but
  today I ran into a problem with the write(). So, I open the file pw
  and write hello and read:
  f = open(pw, r+)
  f.write(hello)
  f.read()
  But read() returns a bunch of what looks like meta code:
  ont': 1, 'center_insert_even\xc4\x00K\x02\xe8\xe1[\x02z\x8e
  \xa5\x02\x0b
  \x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
   0'QUEUE'\np1\n
  (S'exec' 
  What am I doing wrong? Thank you.
  After the 'write' the current position in the file is after the hello, 
  so
  reading will read further content from there.
  The following works (disclaimer: I'm utter newbie in Python, and didn't 
  consult
  the documentation, and it's the first time I've seen the Python 'open'):
  f = open(pw, r+)
  f.write( hello )
  f.seek( 0 )  # Go back to start of file
  f.read()
  f.close()
  Cheers  hth.,
  - Alf
  Thanks, but it didn't work for me. I still get the meta file. Although
  I see that hello is there.
  Just a thought: try w+ instead of r+.

  Because if you do

     print( open.__doc__ )

  as I recall it said something about w truncating the file?

  Cheers  hth.,

  - Alf

  No :) I still got the same thing.

 Hm. Now I had to look in the docs because I thought I'd given bad advice.

 Doc of 'open' says:

    The mode 'w+' opens and truncates the file to 0 bytes, while 'r+' opens 
 the
     file without truncation.

 So with 'w+' the only way to get garbage is if 'read' reads beyond the end of
 file, or 'open' doesn't conform to the documentation.

 Testing with Python 3.1.1 under Windows XP Pro:

 example
   f = open( zilly, w+ )
   f.write( 
 garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage )
 63
   f.close()
   f = open( zilly, r )
   f.read()
 'garbagegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
   f.close()
   f.open( zilly, r+ )
 Traceback (most recent call last):
    File stdin, line 1, in module
 AttributeError: '_io.TextIOWrapper' object has no attribute 'open'
   open( zilly, r+ )
 _io.TextIOWrapper name='zilly' encoding='cp1252'
   f = open( zilly, r+ )
   f.write( hello )
 5
   f.seek( 0 )
 0
   f.read()
 'hellogegarbagegarbagegarbagegarbagegarbagegarbagegarbagegarbage'
   f.close()
   f = open( zilly, w+ )
   f.write( hello )
 5
   f.seek( 0 )
 0
   f.read()
 'hello'
   f.close()
  
 /example

 The w+ works here. Even if I made a typing mistake and apparently left the
 file open in the middle there.

 Cheers  hth.,

 - Alf

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Terry Reedy

Zeynel wrote:

On Oct 31, 10:40 am, Alf P. Steinbach al...@start.no wrote:
Thanks! This works. But I need to close the file before read and open
it again with r, otherwise I get the garbage again. Can you give me
the link where you got this in documentation:

The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
opens the

file without truncation.


Only place i could find it was in this bug report: 
http://bugs.python.org/issue5061


LibRef / builtin functions /open
The entry is a full page.

Get familiar with this section and the Built-in Types section.

Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: problem with read() write()

2009-10-31 Thread Zeynel
On Oct 31, 3:11 pm, Terry Reedy tjre...@udel.edu wrote:

Great, thanks.

 Zeynel wrote:
  On Oct 31, 10:40 am, Alf P. Steinbach al...@start.no wrote:
  Thanks! This works. But I need to close the file before read and open
  it again with r, otherwise I get the garbage again. Can you give me
  the link where you got this in documentation:

  The mode 'w+' opens and truncates the file to 0 bytes, while 'r+'
  opens the
      file without truncation.

  Only place i could find it was in this bug 
  report:http://bugs.python.org/issue5061

 LibRef / builtin functions /open
 The entry is a full page.

 Get familiar with this section and the Built-in Types section.

 Terry Jan Reedy

-- 
http://mail.python.org/mailman/listinfo/python-list