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

ruby-changes:9676

From: akr <ko1@a...>
Date: Thu, 1 Jan 2009 00:05:39 +0900 (JST)
Subject: [ruby-changes:9676] Ruby:r21216 (trunk): * ext/socket/socket.c (sock_s_socketpair): yield if a block is given.

akr	2009-01-01 00:05:16 +0900 (Thu, 01 Jan 2009)

  New Revision: 21216

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

  Log:
    * ext/socket/socket.c (sock_s_socketpair): yield if a block is given.
      (io_call_close): defined.
      (io_close): defined.
      (pair_yield): defined.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/socket.c
    trunk/test/socket/test_unix.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 21215)
+++ ChangeLog	(revision 21216)
@@ -1,3 +1,10 @@
+Wed Dec 31 23:37:17 2008  Tanaka Akira  <akr@f...>
+
+	* ext/socket/socket.c (sock_s_socketpair): yield if a block is given.
+	  (io_call_close): defined.
+	  (io_close): defined.
+	  (pair_yield): defined.
+
 Wed Dec 31 19:35:57 2008  Yuki Sonoda (Yugui)  <yugui@y...>
 
 	* spec/README: follows the change of directory structure in rubyspec.
Index: ext/socket/socket.c
===================================================================
--- ext/socket/socket.c	(revision 21215)
+++ ext/socket/socket.c	(revision 21216)
@@ -2385,11 +2385,30 @@
 }
 
 static VALUE
+io_call_close(VALUE io)
+{
+    return rb_funcall(io, rb_intern("close"), 0, 0);
+}
+
+static VALUE
+io_close(VALUE io)
+{
+    return rb_rescue(io_call_close, io, 0, 0);
+}
+
+static VALUE
+pair_yield(VALUE pair)
+{
+    return rb_ensure(rb_yield, pair, io_close, rb_ary_entry(pair, 1));
+}
+
+static VALUE
 sock_s_socketpair(VALUE klass, VALUE domain, VALUE type, VALUE protocol)
 {
 #if defined HAVE_SOCKETPAIR
     int d, t, p, sp[2];
     int ret;
+    VALUE s1, s2, r;
 
     setup_domain_and_type(domain, &d, type, &t);
     p = NUM2INT(protocol);
@@ -2402,8 +2421,13 @@
 	rb_sys_fail("socketpair(2)");
     }
 
-    return rb_assoc_new(init_sock(rb_obj_alloc(klass), sp[0]),
-			init_sock(rb_obj_alloc(klass), sp[1]));
+    s1 = init_sock(rb_obj_alloc(klass), sp[0]);
+    s2 = init_sock(rb_obj_alloc(klass), sp[1]);
+    r = rb_assoc_new(s1, s2);
+    if (rb_block_given_p()) {
+        return rb_ensure(pair_yield, r, io_close, s1);
+    }
+    return r;
 #else
     rb_notimplement();
 #endif
Index: test/socket/test_unix.rb
===================================================================
--- test/socket/test_unix.rb	(revision 21215)
+++ test/socket/test_unix.rb	(revision 21216)
@@ -146,4 +146,24 @@
     assert_equal("a", s1.read(1))
   end
 
+  def test_socket_pair_with_block
+    pair = nil
+    ret = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) {|s1, s2|
+      pair = [s1, s2]
+      :return_value
+    }
+    assert_equal(:return_value, ret)
+    assert_kind_of(Socket, pair[0])
+    assert_kind_of(Socket, pair[1])
+  end
+
+  def test_unix_socket_pair_with_block
+    pair = nil
+    UNIXSocket.pair {|s1, s2|
+      pair = [s1, s2]
+    }
+    assert_kind_of(UNIXSocket, pair[0])
+    assert_kind_of(UNIXSocket, pair[1])
+  end
+
 end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM

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

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