[前][次][番号順一覧][スレッド一覧]

ruby-changes:9668

From: akr <ko1@a...>
Date: Wed, 31 Dec 2008 16:20:52 +0900 (JST)
Subject: [ruby-changes:9668] Ruby:r21209 (trunk): * io.c (copy_stream_body): don't check to_io because

akr	2008-12-31 16:19:24 +0900 (Wed, 31 Dec 2008)

  New Revision: 21209

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21209

  Log:
    * io.c (copy_stream_body): don't check to_io because
      Zlib::GzipWriter#to_io returns the underlying IO.

  Modified files:
    trunk/ChangeLog
    trunk/io.c
    trunk/test/ruby/test_io.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 21208)
+++ ChangeLog	(revision 21209)
@@ -1,3 +1,8 @@
+Wed Dec 31 15:45:18 2008  Tanaka Akira  <akr@f...>
+
+	* io.c (copy_stream_body): don't check to_io because
+	  Zlib::GzipWriter#to_io returns the underlying IO.
+
 Wed Dec 31 14:52:33 2008  Tanaka Akira  <akr@f...>
 
 	* ext/openssl/ossl_digest.c (GetDigestPtr): use StringValueCStr
Index: io.c
===================================================================
--- io.c	(revision 21208)
+++ io.c	(revision 21209)
@@ -7702,13 +7702,12 @@
 
     if (stp->src == argf ||
         !(TYPE(stp->src) == T_FILE ||
-          rb_respond_to(stp->src, rb_intern("to_io")) ||
           TYPE(stp->src) == T_STRING ||
           rb_respond_to(stp->src, rb_intern("to_path")))) {
         src_fd = -1;
     }
     else {
-        src_io = rb_check_convert_type(stp->src, T_FILE, "IO", "to_io");
+        src_io = TYPE(stp->src) == T_FILE ? stp->src : Qnil;
         if (NIL_P(src_io)) {
             VALUE args[2];
             int oflags = O_RDONLY;
@@ -7730,13 +7729,12 @@
 
     if (stp->dst == argf ||
         !(TYPE(stp->dst) == T_FILE ||
-          rb_respond_to(stp->dst, rb_intern("to_io")) ||
           TYPE(stp->dst) == T_STRING ||
           rb_respond_to(stp->dst, rb_intern("to_path")))) {
         dst_fd = -1;
     }
     else {
-        dst_io = rb_check_convert_type(stp->dst, T_FILE, "IO", "to_io");
+        dst_io = TYPE(stp->dst) == T_FILE ? stp->dst : Qnil;
         if (NIL_P(dst_io)) {
             VALUE args[3];
             int oflags = O_WRONLY|O_CREAT|O_TRUNC;
Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 21208)
+++ test/ruby/test_io.rb	(revision 21209)
@@ -567,6 +567,69 @@
     }
   end
 
+  class Rot13IO
+    def initialize(io)
+      @io = io
+    end
+
+    def readpartial(*args)
+      ret = @io.readpartial(*args)
+      ret.tr!('a-zA-Z', 'n-za-mN-ZA-M')
+      ret
+    end
+
+    def write(str)
+      @io.write(str.tr('a-zA-Z', 'n-za-mN-ZA-M'))
+    end
+
+    def to_io
+      @io
+    end
+  end
+
+  def test_copy_stream_io_to_rot13
+    mkcdtmpdir {
+      File.open("bar", "w") {|f| f << "vex" }
+      File.open("bar") {|src|
+        File.open("baz", "w") {|dst0|
+          dst = Rot13IO.new(dst0)
+          ret = IO.copy_stream(src, dst, 3)
+          assert_equal(3, ret)
+        }
+        assert_equal("irk", File.read("baz"))
+      }
+    }
+  end
+
+  def test_copy_stream_rot13_to_io
+    mkcdtmpdir {
+      File.open("bar", "w") {|f| f << "flap" }
+      File.open("bar") {|src0|
+        src = Rot13IO.new(src0)
+        File.open("baz", "w") {|dst|
+          ret = IO.copy_stream(src, dst, 4)
+          assert_equal(4, ret)
+        }
+      }
+      assert_equal("sync", File.read("baz"))
+    }
+  end
+
+  def test_copy_stream_rot13_to_rot13
+    mkcdtmpdir {
+      File.open("bar", "w") {|f| f << "bin" }
+      File.open("bar") {|src0|
+        src = Rot13IO.new(src0)
+        File.open("baz", "w") {|dst0|
+          dst = Rot13IO.new(dst0)
+          ret = IO.copy_stream(src, dst, 3)
+          assert_equal(3, ret)
+        }
+      }
+      assert_equal("bin", File.read("baz"))
+    }
+  end
+
   def test_copy_stream_strio_flush
     with_pipe {|r, w|
       w.sync = false

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]