ruby-changes:23340
From: drbrain <ko1@a...>
Date: Thu, 19 Apr 2012 08:48:38 +0900 (JST)
Subject: [ruby-changes:23340] drbrain:r35391 (trunk): * io.c (io_readpartial): Document the output buffer parameter is
drbrain 2012-04-19 08:48:27 +0900 (Thu, 19 Apr 2012) New Revision: 35391 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35391 Log: * io.c (io_readpartial): Document the output buffer parameter is overwritten with the read contents even when non-empty. Patch by yu nobuoka. [ruby-trunk - Bug #6285] * io.c (io_read_nonblock): ditto. * io.c (io_read): ditto. * io.c (rb_io_sysread): ditto. * io.c (argf_read): ditto. * io.c (argf_readpartial): ditto. * ext/stringio/stringio.c (strio_read): ditto. * test/ruby/test_argf.rb (class TestArgf): Add test for existing behavior of read outbuf. * test/ruby/test_io.rb (class TestIO): ditto. * test/stringio/test_stringio.rb (class TestStringIO): ditto. Modified files: trunk/ChangeLog trunk/ext/stringio/stringio.c trunk/io.c trunk/test/ruby/test_argf.rb trunk/test/ruby/test_io.rb trunk/test/stringio/test_stringio.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 35390) +++ ChangeLog (revision 35391) @@ -1,3 +1,19 @@ +Thu Apr 19 08:47:54 2012 Eric Hodel <drbrain@s...> + + * io.c (io_readpartial): Document the output buffer parameter is + overwritten with the read contents even when non-empty. + Patch by yu nobuoka. [ruby-trunk - Bug #6285] + * io.c (io_read_nonblock): ditto. + * io.c (io_read): ditto. + * io.c (rb_io_sysread): ditto. + * io.c (argf_read): ditto. + * io.c (argf_readpartial): ditto. + * ext/stringio/stringio.c (strio_read): ditto. + * test/ruby/test_argf.rb (class TestArgf): Add test for existing + behavior of read outbuf. + * test/ruby/test_io.rb (class TestIO): ditto. + * test/stringio/test_stringio.rb (class TestStringIO): ditto. + Wed Apr 18 22:58:55 2012 Nobuyoshi Nakada <nobu@r...> * configure.in (DOT, DOXYGEN): use AC_CHECK_PROGS instead of Index: io.c =================================================================== --- io.c (revision 35390) +++ io.c (revision 35391) @@ -2281,6 +2281,8 @@ * It doesn't block if some data available. * If the optional <i>outbuf</i> argument is present, * it must reference a String, which will receive the data. + * The <i>outbuf</i> will contain only the received data after the method call + * even if it is not empty at the beginning. * It raises <code>EOFError</code> on end of file. * * readpartial is designed for streams such as pipe, socket, tty, etc. @@ -2350,6 +2352,8 @@ * * If the optional <i>outbuf</i> argument is present, * it must reference a String, which will receive the data. + * The <i>outbuf</i> will contain only the received data after the method call + * even if it is not empty at the beginning. * * read_nonblock just calls the read(2) system call. * It causes all errors the read(2) system call causes: Errno::EWOULDBLOCK, Errno::EINTR, etc. @@ -2483,7 +2487,7 @@ /* * call-seq: - * ios.read([length [, buffer]]) -> string, buffer, or nil + * ios.read([length [, outbuf]]) -> string, outbuf, or nil * * Reads <i>length</i> bytes from the I/O stream. * @@ -2503,8 +2507,10 @@ * * If <i>length</i> is zero, it returns <code>""</code>. * - * If the optional <i>buffer</i> argument is present, it must reference + * If the optional <i>outbuf</i> argument is present, it must reference * a String, which will receive the data. + * The <i>outbuf</i> will contain only the received data after the method call + * even if it is not empty at the beginning. * * At end of file, it returns <code>nil</code> or <code>""</code> * depend on <i>length</i>. @@ -4282,6 +4288,8 @@ * that read from <em>ios</em> or you may get unpredictable results. * If the optional <i>outbuf</i> argument is present, it must reference * a String, which will receive the data. + * The <i>outbuf</i> will contain only the received data after the method call + * even if it is not empty at the beginning. * Raises <code>SystemCallError</code> on error and * <code>EOFError</code> at end of file. * @@ -10333,7 +10341,7 @@ /* * call-seq: - * ARGF.read([length [, buffer]]) -> string, buffer, or nil + * ARGF.read([length [, outbuf]]) -> string, outbuf, or nil * * Reads _length_ bytes from ARGF. The files named on the command line * are concatenated and treated as a single file by this method, so when @@ -10350,8 +10358,10 @@ * * If _length_ is zero, it returns _""_. * - * If the optional _buffer_ argument is present, it must reference a String, + * If the optional _outbuf_ argument is present, it must reference a String, * which will receive the data. + * The <i>outbuf</i> will contain only the received data after the method call + * even if it is not empty at the beginning. * * For example: * @@ -10437,7 +10447,10 @@ * Reads at most _maxlen_ bytes from the ARGF stream. It blocks only if * +ARGF+ has no data immediately available. If the optional _outbuf_ * argument is present, it must reference a String, which will receive the - * data. It raises <code>EOFError</code> on end of file. + * data. + * The <i>outbuf</i> will contain only the received data after the method call + * even if it is not empty at the beginning. + * It raises <code>EOFError</code> on end of file. * * +readpartial+ is designed for streams such as pipes, sockets, and ttys. It * blocks only when no data is immediately available. This means that it Index: ext/stringio/stringio.c =================================================================== --- ext/stringio/stringio.c (revision 35390) +++ ext/stringio/stringio.c (revision 35391) @@ -1210,7 +1210,7 @@ /* * call-seq: - * strio.read([length [, buffer]]) -> string, buffer, or nil + * strio.read([length [, outbuf]]) -> string, outbuf, or nil * * See IO#read. */ Index: test/ruby/test_argf.rb =================================================================== --- test/ruby/test_argf.rb (revision 35390) +++ test/ruby/test_argf.rb (revision 35391) @@ -446,6 +446,16 @@ end end + def test_read2_with_not_empty_buffer + ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| + s = "0123456789" + ARGF.read(8, s) + p s + SRC + assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) + end + end + def test_read3 ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| nil while ARGF.gets @@ -463,6 +473,8 @@ loop do s << ARGF.readpartial(1) t = ""; ARGF.readpartial(1, t); s << t + # not empty buffer + u = "abcdef"; ARGF.readpartial(1, u); s << u end rescue EOFError puts s Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 35390) +++ test/ruby/test_io.rb (revision 35391) @@ -991,6 +991,16 @@ } end + def test_readpartial_with_not_empty_buffer + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.readpartial(5, s = "01234567") + assert_equal("foob", s) + end) + end + def test_readpartial_buffer_error with_pipe do |r, w| s = "" @@ -1026,6 +1036,16 @@ end end + def test_read_with_not_empty_buffer + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.read(nil, s = "01234567") + assert_equal("foob", s) + end) + end + def test_read_buffer_error with_pipe do |r, w| s = "" @@ -1047,6 +1067,17 @@ end) end + def test_read_nonblock_with_not_empty_buffer + skip "IO#read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.read_nonblock(5, s = "01234567") + assert_equal("foob", s) + end) + end + def test_read_nonblock_error return if !have_nonblock? skip "IO#read_nonblock is not supported on file/pipe." if /mswin|bccwin|mingw/ =~ RUBY_PLATFORM @@ -1417,6 +1448,16 @@ end end + def test_sysread_with_not_empty_buffer + pipe(proc do |w| + w.write "foob" + w.close + end, proc do |r| + r.sysread( 5, s = "01234567" ) + assert_equal( "foob", s ) + end) + end + def test_flag t = make_tempfile Index: test/stringio/test_stringio.rb =================================================================== --- test/stringio/test_stringio.rb (revision 35390) +++ test/stringio/test_stringio.rb (revision 35391) @@ -426,6 +426,11 @@ s = "" f.read(nil, s) assert_equal("\u3042\u3044", s, bug5207) + f.rewind + # not empty buffer + s = "0123456789" + f.read(nil, s) + assert_equal("\u3042\u3044", s) end def test_readpartial @@ -435,6 +440,10 @@ assert_equal("\u3042\u3044", f.readpartial) f.rewind assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size)) + f.rewind + # not empty buffer + s = '0123456789' + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size, s)) end def test_read_nonblock @@ -444,6 +453,10 @@ assert_equal("\u3042\u3044", f.read_nonblock) f.rewind assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size)) + f.rewind + # not empty buffer + s = '0123456789' + assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size, s)) end def test_size -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/