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

ruby-changes:48297

From: nobu <ko1@a...>
Date: Wed, 25 Oct 2017 10:40:20 +0900 (JST)
Subject: [ruby-changes:48297] nobu:r60411 (trunk): range.c: check if exclude_end? is defined

nobu	2017-10-25 10:40:15 +0900 (Wed, 25 Oct 2017)

  New Revision: 60411

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

  Log:
    range.c: check if exclude_end? is defined
    
    * range.c (rb_range_values): should raise TypeError if necessary
      method is not defined, not NoMethodError, when trying to tell if
      the object is a Range and extract info.
      [ruby-core:83541] [Bug #14048]

  Modified files:
    trunk/range.c
    trunk/test/ruby/test_enum.rb
    trunk/test/ruby/test_range.rb
Index: range.c
===================================================================
--- range.c	(revision 60410)
+++ range.c	(revision 60411)
@@ -987,11 +987,22 @@ rb_range_values(VALUE range, VALUE *begp https://github.com/ruby/ruby/blob/trunk/range.c#L987
 	excl = EXCL(range);
     }
     else {
+	VALUE x;
+#if 0
+	/* TODO: fix spec/mspec/lib/mspec/mocks/mock.rb:Mock.verify_call */
+	b = rb_check_funcall(range, id_beg, 0, 0);
+	if (b == Qundef) return (int)Qfalse;
+	e = rb_check_funcall(range, id_end, 0, 0);
+	if (e == Qundef) return (int)Qfalse;
+#else
 	if (!rb_respond_to(range, id_beg)) return (int)Qfalse;
 	if (!rb_respond_to(range, id_end)) return (int)Qfalse;
 	b = rb_funcall(range, id_beg, 0);
 	e = rb_funcall(range, id_end, 0);
-	excl = RTEST(rb_funcall(range, rb_intern("exclude_end?"), 0));
+#endif
+	x = rb_check_funcall(range, rb_intern("exclude_end?"), 0, 0);
+	if (x == Qundef) return (int)Qfalse;
+	excl = RTEST(x);
     }
     *begp = b;
     *endp = e;
Index: test/ruby/test_range.rb
===================================================================
--- test/ruby/test_range.rb	(revision 60410)
+++ test/ruby/test_range.rb	(revision 60411)
@@ -387,7 +387,7 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L387
     assert_raise(TypeError) { [][o] }
     class << o; attr_accessor :end end
     o.end = 0
-    assert_raise(NoMethodError) { [][o] }
+    assert_raise(TypeError) { [][o] }
     def o.exclude_end=(v) @exclude_end = v end
     def o.exclude_end?() @exclude_end end
     o.exclude_end = false
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 60410)
+++ test/ruby/test_enum.rb	(revision 60411)
@@ -966,6 +966,19 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L966
     assert_int_equal(5, (2..0).sum(5))
     assert_int_equal(2, (2..2).sum)
     assert_int_equal(42, (2...2).sum(42))
+
+    not_a_range = Class.new do
+      include Enumerable # Defines the `#sum` method
+      def each
+        yield 2
+        yield 4
+        yield 6
+      end
+
+      def begin; end
+      def end; end
+    end
+    assert_equal(12, not_a_range.new.sum)
   end
 
   def test_uniq

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

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