Problem with .next() method adding junk characters.

R

Rainy

Hi,

I tried searching for this and did not find this issue. I only looked
at about dozen hits, I apologize if this is covered somewhere and I
missed it. Without much further ado, here's the thing (Win, Py2.5):

Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
f.next()
IOError: [Errno 9] Bad file descriptor'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
....many more lines of junk...'

I'm not actually trying to do something particular, I'm making snippets
of example code for all functions in LibRef and I ran into this, and
I'm just curious as to what's happening. I understand that you're not
supposed to call .next on a file open for writing. But I don't know why
and how it does what happened here; besides, I've seen the same thing
happen before when I was doing something else with file
open/write/close, but I don't remember the specifics.
 
J

John Machin

Rainy said:
Hi,

I tried searching for this and did not find this issue. I only looked
at about dozen hits, I apologize if this is covered somewhere and I
missed it. Without much further ado, here's the thing (Win, Py2.5):

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

This *should* complain that the file is not open for reading. What you
see is an accidental error. message. When I tried it, I got no error,
but it printed a few hundred bytes of garbage.
In both your case and mine, it has also written a load of junk to the
file!
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
...many more lines of junk...'

Junk was written to the file earlier.
I understand that you're not
supposed to call .next on a file open for writing.

Indeed. However if you mess up, Python is supposed to give you a
meaningful error message and not write gibberish to your file.

Please report it as a bug.

Cheers,
John
 
R

Rainy

John said:
Rainy said:
Hi,

I tried searching for this and did not find this issue. I only looked
at about dozen hits, I apologize if this is covered somewhere and I
missed it. Without much further ado, here's the thing (Win, Py2.5):
f = open('test', 'w')
f.fileno() 4
f.write('1\n')
f.write('2\n3\n4\n')
f.next()

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

This *should* complain that the file is not open for reading. What you
see is an accidental error. message. When I tried it, I got no error,
but it printed a few hundred bytes of garbage.
In both your case and mine, it has also written a load of junk to the
file!
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
...many more lines of junk...'

Junk was written to the file earlier.
I understand that you're not
supposed to call .next on a file open for writing.

Indeed. However if you mess up, Python is supposed to give you a
meaningful error message and not write gibberish to your file.

Please report it as a bug.

Cheers,
John

Thanks for the reply, I reported it..

-Rainy
 
F

Fredrik Lundh

Rainy said:
I'm just curious as to what's happening. I understand that you're not
supposed to call .next on a file open for writing. But I don't know why
and how it does what happened here; besides, I've seen the same thing
happen before when I was doing something else with file
open/write/close, but I don't remember the specifics.

C's stdio library require you to call "flush" when switching between reading and
writing; if you don't, the result is undefined.

</F>
 
P

Paul McGuire

Fredrik Lundh said:
C's stdio library require you to call "flush" when switching between
reading and
writing; if you don't, the result is undefined.

</F>

Sure enough, following the OP's original sequence, with an intervening flush
between the writes and next, leaves the file in the expected state:
Traceback (most recent call last):
Traceback (most recent call last):

I would guess then that the likely extent of any fix to this "bug" would be
documentation to the effect of Fredrik's last comment above.

-- Paul
 
R

Rainy

Paul said:
Sure enough, following the OP's original sequence, with an intervening flush
between the writes and next, leaves the file in the expected state:

Traceback (most recent call last):

Traceback (most recent call last):


I would guess then that the likely extent of any fix to this "bug" would be
documentation to the effect of Fredrik's last comment above.

-- Paul

Thanks.. I get it now. Should I close the bug report then?
 
T

Terry Reedy

Rainy said:
Thanks.. I get it now. Should I close the bug report then?

Either that, or change it to a doc bug and suggest the addition to the
beginning of the second paragraph of
"When switching from reading or writing (or vice versa), call flush(), or
the result will be undefined."

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top