mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53949 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a12e8fd2ed
commit
60dad06d53
4 changed files with 38 additions and 4 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Fri Feb 26 14:40:48 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Fri Feb 26 14:39:39 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* doc/extension.rdoc, doc/extension.ja.rdoc: add editor local
|
* doc/extension.rdoc, doc/extension.ja.rdoc: add editor local
|
||||||
|
|
30
numeric.c
30
numeric.c
|
@ -2081,6 +2081,34 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
static int
|
||||||
num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step)
|
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 *argv, VALUE *to, VALUE *step)
|
||||||
if (NIL_P(*step)) {
|
if (NIL_P(*step)) {
|
||||||
*step = INT2FIX(1);
|
*step = INT2FIX(1);
|
||||||
}
|
}
|
||||||
desc = !positive_int_p(*step);
|
desc = num_step_negative_p(*step);
|
||||||
if (NIL_P(*to)) {
|
if (NIL_P(*to)) {
|
||||||
*to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
|
*to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
|
||||||
}
|
}
|
||||||
|
|
|
@ -601,7 +601,7 @@ class TestFloat < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_num2dbl
|
def test_num2dbl
|
||||||
assert_raise(ArgumentError) do
|
assert_raise(TypeError) do
|
||||||
1.0.step(2.0, "0.5") {}
|
1.0.step(2.0, "0.5") {}
|
||||||
end
|
end
|
||||||
assert_raise(TypeError) do
|
assert_raise(TypeError) do
|
||||||
|
|
|
@ -262,8 +262,8 @@ class TestNumeric < Test::Unit::TestCase
|
||||||
assert_raise(ArgumentError) { 1.step(10, 1, 0).size }
|
assert_raise(ArgumentError) { 1.step(10, 1, 0).size }
|
||||||
assert_raise(ArgumentError) { 1.step(10, 0) { } }
|
assert_raise(ArgumentError) { 1.step(10, 0) { } }
|
||||||
assert_raise(ArgumentError) { 1.step(10, 0).size }
|
assert_raise(ArgumentError) { 1.step(10, 0).size }
|
||||||
assert_raise(ArgumentError) { 1.step(10, "1") { } }
|
assert_raise(TypeError) { 1.step(10, "1") { } }
|
||||||
assert_raise(ArgumentError) { 1.step(10, "1").size }
|
assert_raise(TypeError) { 1.step(10, "1").size }
|
||||||
assert_raise(TypeError) { 1.step(10, nil) { } }
|
assert_raise(TypeError) { 1.step(10, nil) { } }
|
||||||
assert_raise(TypeError) { 1.step(10, nil).size }
|
assert_raise(TypeError) { 1.step(10, nil).size }
|
||||||
assert_nothing_raised { 1.step(by: 0, to: nil) }
|
assert_nothing_raised { 1.step(by: 0, to: nil) }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue