ruby-changes:48553
From: nobu <ko1@a...>
Date: Mon, 6 Nov 2017 08:32:55 +0900 (JST)
Subject: [ruby-changes:48553] nobu:r60668 (trunk): enum.c: check argument first
nobu 2017-11-06 08:32:50 +0900 (Mon, 06 Nov 2017) New Revision: 60668 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60668 Log: enum.c: check argument first * enum.c (enum_cycle_size): check an argument before the size of the receiver, if it is given. Modified files: trunk/enum.c trunk/test/ruby/test_enumerator.rb trunk/test/ruby/test_lazy_enumerator.rb Index: enum.c =================================================================== --- enum.c (revision 60667) +++ enum.c (revision 60668) @@ -2840,18 +2840,19 @@ cycle_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ar https://github.com/ruby/ruby/blob/trunk/enum.c#L2840 static VALUE enum_cycle_size(VALUE self, VALUE args, VALUE eobj) { - long mul; + long mul = 0; VALUE n = Qnil; - VALUE size = enum_size(self, args, 0); - - if (size == Qnil) return Qnil; - if (FIXNUM_ZERO_P(size)) return size; + VALUE size; if (args && (RARRAY_LEN(args) > 0)) { n = RARRAY_AREF(args, 0); + if (!NIL_P(n)) mul = NUM2LONG(n); } - if (n == Qnil) return DBL2NUM(INFINITY); - mul = NUM2LONG(n); + + size = enum_size(self, args, 0); + if (NIL_P(size) || FIXNUM_ZERO_P(size)) return size; + + if (NIL_P(n)) return DBL2NUM(INFINITY); if (mul <= 0) return INT2FIX(0); n = LONG2FIX(mul); return rb_funcallv(size, '*', 1, &n); Index: test/ruby/test_lazy_enumerator.rb =================================================================== --- test/ruby/test_lazy_enumerator.rb (revision 60667) +++ test/ruby/test_lazy_enumerator.rb (revision 60668) @@ -507,7 +507,9 @@ EOS https://github.com/ruby/ruby/blob/trunk/test/ruby/test_lazy_enumerator.rb#L507 def size; 0; end include Enumerable end - assert_equal 0, obj.lazy.cycle.size + lazy = obj.lazy + assert_equal 0, lazy.cycle.size + assert_raise(TypeError) {lazy.cycle("").size} end def test_map_zip Index: test/ruby/test_enumerator.rb =================================================================== --- test/ruby/test_enumerator.rb (revision 60667) +++ test/ruby/test_enumerator.rb (revision 60668) @@ -590,6 +590,8 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enumerator.rb#L590 assert_equal 126, @sized.cycle(3).size assert_equal Float::INFINITY, [].to_enum { 42 }.cycle.size assert_equal 0, [].to_enum { 0 }.cycle.size + + assert_raise(TypeError) {[].to_enum { 0 }.cycle("").size} end def test_size_for_loops -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/