469,358 Members | 1,610 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Strange loop behavior

Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.

--
Arimaz SA
Av. du 24 Janvier 11
Ateliers de la Ville de Renens, Atelier 5
1020 Renens, Switzerland
www.mydeskfriend.com
Mob: +41-(0)79-539-0069

Mar 26 '08 #1
4 1214
Gabriel Rossetti schrieb:
Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.
But you used a superfluous repr. Look at this:
>>repr("")
"''"
>>repr("") == ""
False
>>>
So - get rid of the useless repr, then things should work.
Regarding the behavior in the debugger: that's an artifact of the
debugger that it doesn't step into that line, it doesn't change the way
the code works.

Diez
Mar 26 '08 #2
Gabriel Rossetti wrote:
I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.
d may be an empty string, but repr(d) isn't:
>>d = ""
print repr(d)
''
>>print d
Remove the two repr() calls and you should be OK.

Peter
Mar 26 '08 #3
On Mar 26, 8:35*am, Gabriel Rossetti
<gabriel.rosse...@mydeskfriend.comwrote:
Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

* * d = repr(f.read(DEFAULT_BUFFER_SIZE))
* * while d != "":
* * * * file_str.write(d)
* * * * d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.
No, it isn't the empty string, it is the printable representation of
the empty string (because you wrap your f.read() calls in repr()),
which is the string "''":
>>print "''"
''

Why do you wrap f.read(...) in the repr() function? If you remove the
two repr() I suspect your code will work.

OTOH do you know that the read() method of file objects does what you
want? You could simply write:

file_str = f.read()

Or are there some other factors that prevent you from doing this?

--
Arnaud

Mar 26 '08 #4
Arnaud Delobelle wrote:
On Mar 26, 8:35 am, Gabriel Rossetti
<gabriel.rosse...@mydeskfriend.comwrote:
>Hello,

I wrote a program that reads data from a file and puts it in a string,
the problem is that it loops infinitely and that's not wanted, here is
the code :

d = repr(f.read(DEFAULT_BUFFER_SIZE))
while d != "":
file_str.write(d)
d = repr(f.read(DEFAULT_BUFFER_SIZE))

I also tried writing the while's condition like so : len(d) 0, but
that doesn't change anything. I tried step-by-step debugging using
PyDev(eclipse plugin) and I noticed this, once the while was read once,
it is never re-read, basically, the looping does happen, but just in
between the two lines in the loop's body/block, it never goes on the
while and thus never verifies the condition and thus loops forever. I
had been using psyco (a sort of JIT for python) and so I uninstalled it
and restarted eclipse and I still get the same thing. This looks like
some bug, but I may be wrong, does anybody understand what's going on here?

Thanks,
Gabriel

PS
And yes I checked, the "d" variable is en empty string at some point, so
the looping should stop.

No, it isn't the empty string, it is the printable representation of
the empty string (because you wrap your f.read() calls in repr()),
which is the string "''":
>>print "''"
''

Why do you wrap f.read(...) in the repr() function? If you remove the
two repr() I suspect your code will work.

OTOH do you know that the read() method of file objects does what you
want? You could simply write:

file_str = f.read()

Or are there some other factors that prevent you from doing this?

--
Arnaud

Ok, like I mentioned in my second msg, I had put repr() there because I
was getting errors because the ascii range (128) was exceeded, I tried
cheating and telling it it was unicode but that didn't work, so I tried
repr() and it gave me a string rep. of my data, that was ok. After that
I tried using StringIO instead of what I had before, a list of strings
that I later joined with an empty string. I just re-tried removing the
repr() and it works with the StringIO version.

Thanks for all your answers,
Gabriel
Mar 26 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by simon place | last post: by
reply views Thread by John Hunter | last post: by
3 posts views Thread by curi42 | last post: by
4 posts views Thread by hall | last post: by
11 posts views Thread by Marlene Stebbins | last post: by
2 posts views Thread by Alex | last post: by
2 posts views Thread by maxim khesin | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.