From: brian ford Date: 2011-09-01T04:03:37+09:00 Subject: [ruby-core:39215] Re: [Ruby 1.9 - Bug #5237] IO.copy_stream calls #read on an object infinitely many times Hi, On Tue, Aug 30, 2011 at 8:48 PM, Motohiro KOSAKI wrote: > > Issue #5237 has been updated by Motohiro KOSAKI. > > > If you are reading following paragraph, > >> * �At end of file, it returns nil or "" >> * �depend on length. >> * �ios.read() and >> * �ios.read(nil) returns "". >> * �ios.read(positive-integer) returns nil. > > you have to read following paragraph too, I think. > >> * �If length is a positive integer, >> * �it try to read length bytes without any conversion (binary mode). >> * �It returns nil or a string whose length is 1 to length bytes. >> * �nil means it met EOF at beginning. > > *AT BEGINNING* > > Thus, positive length destination buffer and returning nil are exclusive. Now, I don't think > we need doc fix. > > but I'm not talking about a behavior change. I have no strong opinion. It's another story. I'm waiting akr-san's response. I'm not following your explanation. The return value, whether nil or anything else, including an empty string or any string (one byte or not) will NOT cause the copy_stream method to return. It will loop infinitely unless the buffer that is passed in is set to "". In fact, I can return *anything* from #read as long as I set the buffer to "". This is bizarre. At the least, the rdoc should note that to participate in this API, #read must set the buffer to "" to signal EOF. Still, in my opinion, that is a bad API. The return value should be the sole way to signal EOF given that the arguments to #read are optional. sasha:rubinius2.0 brian$ cat copy_stream_bug.rb class A def initialize @count = 0 @read = 0 end def read(bytes, buffer) puts buffer puts "read: #{@count += 1}" @read += 1 case @read when 1 buffer.replace "this is" when 2 buffer << " a test" else buffer.replace "" return A.new end end end a = A.new IO.copy_stream a, "copy_stream_test" sasha:rubinius2.0 brian$ ruby -v copy_stream_bug.rb ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0] read: 1 this is read: 2 this is a test read: 3 Thanks, Brian