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

ruby-changes:44521

From: knu <ko1@a...>
Date: Sat, 5 Nov 2016 22:53:42 +0900 (JST)
Subject: [ruby-changes:44521] knu:r56594 (trunk): IPAddr#== and IPAddr#<=> no longer raise an exception if coercion fails

knu	2016-11-05 22:53:38 +0900 (Sat, 05 Nov 2016)

  New Revision: 56594

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

  Log:
    IPAddr#== and IPAddr#<=> no longer raise an exception if coercion fails
    
    * lib/ipaddr.rb (IPAddr#==): If coercion fails, return false
      instead of passing through the exception. [ruby-core:77451]
      [Bug #12799]
    
    * lib/ipaddr.rb (IPAddr#<=>): If coercion fails, return nil
      instead of passing through the exception. [ruby-core:77451]
      [Bug #12799]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/lib/ipaddr.rb
    trunk/test/test_ipaddr.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56593)
+++ ChangeLog	(revision 56594)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Nov  5 22:50:13 2016  Akinori MUSHA  <knu@i...>
+
+	* lib/ipaddr.rb (IPAddr#==): If coercion fails, return false
+	  instead of passing through the exception. [ruby-core:77451]
+	  [Bug #12799]
+
+	* lib/ipaddr.rb (IPAddr#<=>): If coercion fails, return nil
+	  instead of passing through the exception. [ruby-core:77451]
+	  [Bug #12799]
+
 Sat Nov  5 22:11:33 2016  Kazuki Tsujimoto  <kazuki@c...>
 
 	* vm_trace.c (tracepoint_attr_callee_id, rb_tracearg_callee_id):
Index: NEWS
===================================================================
--- NEWS	(revision 56593)
+++ NEWS	(revision 56594)
@@ -181,6 +181,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L181
 
   * Add a liberal_parsing option. [Feature #11839]
 
+* IPAddr
+
+  * IPAddr#== and IPAddr#<=> no longer raise an exception if coercion fails.
+    [Bug #12799]
+
 * Logger
 
 	* Allow specifying logger parameters in constructor such
Index: test/test_ipaddr.rb
===================================================================
--- test/test_ipaddr.rb	(revision 56593)
+++ test/test_ipaddr.rb	(revision 56594)
@@ -169,6 +169,8 @@ class TC_Operator < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_ipaddr.rb#L169
     @a = IPAddr.new("3ffe:505:2::/48")
     @b = IPAddr.new("0:0:0:1::")
     @c = IPAddr.new(IN6MASK32)
+    @inconvertible_range = 1..5
+    @inconvertible_string = "sometext"
   end
   alias set_up setup
 
@@ -220,6 +222,13 @@ class TC_Operator < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_ipaddr.rb#L222
     assert_equal(false, @a == IPAddr.new("3ffe:505:3::"))
     assert_equal(true, @a != IPAddr.new("3ffe:505:3::"))
     assert_equal(false, @a != IPAddr.new("3ffe:505:2::"))
+    assert_equal(false, @a == @inconvertible_range)
+    assert_equal(false, @a == @inconvertible_string)
+  end
+
+  def test_compare
+    assert_equal(nil, @a <=> @inconvertible_range)
+    assert_equal(nil, @a <=> @inconvertible_string)
   end
 
   def test_mask
Index: lib/ipaddr.rb
===================================================================
--- lib/ipaddr.rb	(revision 56593)
+++ lib/ipaddr.rb	(revision 56594)
@@ -149,7 +149,10 @@ class IPAddr https://github.com/ruby/ruby/blob/trunk/lib/ipaddr.rb#L149
   # Returns true if two ipaddrs are equal.
   def ==(other)
     other = coerce_other(other)
-    return @family == other.family && @addr == other.to_i
+  rescue
+    false
+  else
+    @family == other.family && @addr == other.to_i
   end
 
   # Returns a new ipaddr built by masking IP address with the given
@@ -335,10 +338,10 @@ class IPAddr https://github.com/ruby/ruby/blob/trunk/lib/ipaddr.rb#L338
   # Compares the ipaddr with another.
   def <=>(other)
     other = coerce_other(other)
-
-    return nil if other.family != @family
-
-    return @addr <=> other.to_i
+  rescue
+    nil
+  else
+    @addr <=> other.to_i if other.family == @family
   end
   include Comparable
 

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

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