ruby-changes:41875
From: nobu <ko1@a...>
Date: Fri, 26 Feb 2016 14:40:54 +0900 (JST)
Subject: [ruby-changes:41875] nobu:r53949 (trunk): numeric.c: wrong type step should raise TypeError
nobu 2016-02-26 14:41:37 +0900 (Fri, 26 Feb 2016) New Revision: 53949 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53949 Log: numeric.c: wrong type step should raise TypeError * numeric.c (num_step_scan_args): comparison String with Numeric should raise TypeError. it is an invalid type, but not a mismatch the number of arguments. [ruby-core:62430] [Bug #9810] Modified files: trunk/ChangeLog trunk/numeric.c trunk/test/ruby/test_float.rb trunk/test/ruby/test_numeric.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 53948) +++ ChangeLog (revision 53949) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Feb 26 14:40:48 2016 Nobuyoshi Nakada <nobu@r...> + + * numeric.c (num_step_scan_args): comparison String with Numeric + should raise TypeError. it is an invalid type, but not a + mismatch the number of arguments. [ruby-core:62430] [Bug #9810] + Fri Feb 26 14:39:39 2016 Nobuyoshi Nakada <nobu@r...> * doc/extension.rdoc, doc/extension.ja.rdoc: add editor local Index: test/ruby/test_numeric.rb =================================================================== --- test/ruby/test_numeric.rb (revision 53948) +++ test/ruby/test_numeric.rb (revision 53949) @@ -262,8 +262,8 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L262 assert_raise(ArgumentError) { 1.step(10, 1, 0).size } assert_raise(ArgumentError) { 1.step(10, 0) { } } assert_raise(ArgumentError) { 1.step(10, 0).size } - assert_raise(ArgumentError) { 1.step(10, "1") { } } - assert_raise(ArgumentError) { 1.step(10, "1").size } + assert_raise(TypeError) { 1.step(10, "1") { } } + assert_raise(TypeError) { 1.step(10, "1").size } assert_raise(TypeError) { 1.step(10, nil) { } } assert_raise(TypeError) { 1.step(10, nil).size } assert_nothing_raised { 1.step(by: 0, to: nil) } Index: test/ruby/test_float.rb =================================================================== --- test/ruby/test_float.rb (revision 53948) +++ test/ruby/test_float.rb (revision 53949) @@ -601,7 +601,7 @@ class TestFloat < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_float.rb#L601 end def test_num2dbl - assert_raise(ArgumentError) do + assert_raise(TypeError) do 1.0.step(2.0, "0.5") {} end assert_raise(TypeError) do Index: numeric.c =================================================================== --- numeric.c (revision 53948) +++ numeric.c (revision 53949) @@ -2081,6 +2081,34 @@ ruby_num_interval_step_size(VALUE from, https://github.com/ruby/ruby/blob/trunk/numeric.c#L2081 } } +static VALUE +num_step_compare_with_zero(VALUE num) +{ + VALUE zero = INT2FIX(0); + return rb_check_funcall(num, '>', 1, &zero); +} + +static int +num_step_negative_p(VALUE num) +{ + const ID mid = '<'; + VALUE r; + + if (FIXNUM_P(num)) { + if (method_basic_p(rb_cFixnum)) + return (SIGNED_VALUE)num < 0; + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + if (method_basic_p(rb_cBignum)) + return BIGNUM_NEGATIVE_P(num); + } + r = rb_rescue(num_step_compare_with_zero, num, coerce_rescue_quiet, Qnil); + if (r == Qundef) { + coerce_failed(num, INT2FIX(0)); + } + return !RTEST(r); +} + static int num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step) { @@ -2115,7 +2143,7 @@ num_step_scan_args(int argc, const VALUE https://github.com/ruby/ruby/blob/trunk/numeric.c#L2143 if (NIL_P(*step)) { *step = INT2FIX(1); } - desc = !positive_int_p(*step); + desc = num_step_negative_p(*step); if (NIL_P(*to)) { *to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/