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

ruby-changes:46236

From: nobu <ko1@a...>
Date: Fri, 14 Apr 2017 19:03:48 +0900 (JST)
Subject: [ruby-changes:46236] nobu:r58351 (trunk): IPSocket#inspect

nobu	2017-04-14 19:03:43 +0900 (Fri, 14 Apr 2017)

  New Revision: 58351

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58351

  Log:
    IPSocket#inspect

  Modified files:
    trunk/ext/socket/ipsocket.c
    trunk/test/socket/test_tcp.rb
    trunk/test/socket/test_udp.rb
Index: test/socket/test_tcp.rb
===================================================================
--- test/socket/test_tcp.rb	(revision 58350)
+++ test/socket/test_tcp.rb	(revision 58351)
@@ -8,6 +8,15 @@ end https://github.com/ruby/ruby/blob/trunk/test/socket/test_tcp.rb#L8
 
 
 class TestSocket_TCPSocket < Test::Unit::TestCase
+  def test_inspect
+    TCPServer.open("localhost", 0) {|server|
+      assert_match(/AF_INET/, server.inspect)
+      TCPSocket.open("localhost", server.addr[1]) {|client|
+        assert_match(/AF_INET/, client.inspect)
+      }
+    }
+  end
+
   def test_initialize_failure
     # These addresses are chosen from TEST-NET-1, TEST-NET-2, and TEST-NET-3.
     # [RFC 5737]
Index: test/socket/test_udp.rb
===================================================================
--- test/socket/test_udp.rb	(revision 58350)
+++ test/socket/test_udp.rb	(revision 58351)
@@ -15,6 +15,17 @@ class TestSocket_UDPSocket < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/socket/test_udp.rb#L15
     assert_nothing_raised { UDPSocket.open(:AF_INET) {} }
   end
 
+  def test_inspect
+    UDPSocket.open() {|sock|
+      assert_match(/AF_INET\b/, sock.inspect)
+    }
+    if Socket.const_defined?(:AF_INET6)
+      UDPSocket.open(Socket::AF_INET6) {|sock|
+        assert_match(/AF_INET6\b/, sock.inspect)
+      }
+    end
+  end
+
   def test_connect
     s = UDPSocket.new
     host = Object.new
Index: ext/socket/ipsocket.c
===================================================================
--- ext/socket/ipsocket.c	(revision 58350)
+++ ext/socket/ipsocket.c	(revision 58351)
@@ -191,6 +191,43 @@ rsock_revlookup_flag(VALUE revlookup, in https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L191
 
 /*
  * call-seq:
+ *   ipsocket.inspect   -> string
+ *
+ * Return a string describing this IPSocket object.
+ */
+static VALUE
+ip_inspect(VALUE sock)
+{
+    VALUE str = rb_call_super(0, 0);
+    rb_io_t *fptr = RFILE(sock)->fptr;
+    union_sockaddr addr;
+    socklen_t len = (socklen_t)sizeof addr;
+    ID id;
+    if (fptr && fptr->fd >= 0 &&
+	getsockname(fptr->fd, &addr.addr, &len) >= 0 &&
+	(id = rsock_intern_family(addr.addr.sa_family)) != 0) {
+	VALUE family = rb_id2str(id);
+	char hbuf[1024], pbuf[1024];
+	long slen = RSTRING_LEN(str);
+	const char last = (slen > 1 && RSTRING_PTR(str)[slen - 1] == '>') ?
+	    (--slen, '>') : 0;
+	str = rb_str_subseq(str, 0, slen);
+	rb_str_cat_cstr(str, ", ");
+	rb_str_append(str, family);
+	if (!rb_getnameinfo(&addr.addr, len, hbuf, sizeof(hbuf),
+			    pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
+	    rb_str_cat_cstr(str, ", ");
+	    rb_str_cat_cstr(str, hbuf);
+	    rb_str_cat_cstr(str, ", ");
+	    rb_str_cat_cstr(str, pbuf);
+	}
+	if (last) rb_str_cat(str, &last, 1);
+    }
+    return str;
+}
+
+/*
+ * call-seq:
  *   ipsocket.addr([reverse_lookup]) => [address_family, port, hostname, numeric_address]
  *
  * Returns the local address as an array which contains
@@ -332,6 +369,7 @@ rsock_init_ipsocket(void) https://github.com/ruby/ruby/blob/trunk/ext/socket/ipsocket.c#L369
      * IPSocket is the super class of TCPSocket and UDPSocket.
      */
     rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
+    rb_define_method(rb_cIPSocket, "inspect", ip_inspect, 0);
     rb_define_method(rb_cIPSocket, "addr", ip_addr, -1);
     rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
     rb_define_method(rb_cIPSocket, "recvfrom", ip_recvfrom, -1);

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

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